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