blob: 049604576243e8eada8f7f3b9b093843f17a0fe2 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom5f38b3a2014-08-27 23:50:54 -070019package org.onlab.util;
20
tom782a7cf2014-09-11 23:58:38 -070021import com.google.common.base.Strings;
22import com.google.common.primitives.UnsignedLongs;
tom5f38b3a2014-08-27 23:50:54 -070023import com.google.common.util.concurrent.ThreadFactoryBuilder;
24
tom53efab52014-10-07 17:43:48 -070025import java.io.BufferedReader;
26import java.io.File;
27import java.io.FileReader;
28import java.io.IOException;
29import java.util.ArrayList;
30import java.util.List;
tom5f38b3a2014-08-27 23:50:54 -070031import java.util.concurrent.ThreadFactory;
32
33public abstract class Tools {
34
35 private Tools() {
36 }
37
38 /**
39 * Returns a thread factory that produces threads named according to the
40 * supplied name pattern.
41 *
42 * @param pattern name pattern
43 * @return thread factory
44 */
45 public static ThreadFactory namedThreads(String pattern) {
46 return new ThreadFactoryBuilder().setNameFormat(pattern).build();
47 }
48
tom782a7cf2014-09-11 23:58:38 -070049 /**
50 * Converts a string from hex to long.
51 *
52 * @param string hex number in string form; sans 0x
53 * @return long value
54 */
55 public static long fromHex(String string) {
56 return UnsignedLongs.parseUnsignedLong(string, 16);
57 }
58
59 /**
60 * Converts a long value to hex string; 16 wide and sans 0x.
61 *
62 * @param value long value
63 * @return hex string
64 */
65 public static String toHex(long value) {
66 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
67 }
68
69 /**
70 * Converts a long value to hex string; 16 wide and sans 0x.
71 *
72 * @param value long value
73 * @param width string width; zero padded
74 * @return hex string
75 */
76 public static String toHex(long value, int width) {
77 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
78 }
tomf110fff2014-09-26 00:38:18 -070079
80 /**
81 * Suspends the current thread for a specified number of millis.
82 *
83 * @param ms number of millis
84 */
85 public static void delay(int ms) {
86 try {
87 Thread.sleep(ms);
88 } catch (InterruptedException e) {
89 throw new RuntimeException("Interrupted", e);
90 }
91 }
92
tom53efab52014-10-07 17:43:48 -070093 /**
94 * Slurps the contents of a file into a list of strings, one per line.
95 *
96 * @param path file path
97 * @return file contents
98 */
99 public static List<String> slurp(File path) {
100 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
101 List<String> lines = new ArrayList<>();
102 String line;
103 while ((line = br.readLine()) != null) {
104 lines.add(line);
105 }
106 return lines;
107
108 } catch (IOException e) {
109 return null;
110 }
111 }
112
tom5f38b3a2014-08-27 23:50:54 -0700113}