blob: b6a76d0d1ba978609bd441d2a2820cce4bb01cdc [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
tom53efab52014-10-07 17:43:48 -070018import java.io.BufferedReader;
19import java.io.File;
Ray Milkey705d9bc2014-11-18 08:19:00 -080020import java.io.FileInputStream;
tom53efab52014-10-07 17:43:48 -070021import java.io.IOException;
Ray Milkey705d9bc2014-11-18 08:19:00 -080022import java.io.InputStreamReader;
23import java.nio.charset.StandardCharsets;
tom53efab52014-10-07 17:43:48 -070024import java.util.ArrayList;
25import java.util.List;
tom5f38b3a2014-08-27 23:50:54 -070026import java.util.concurrent.ThreadFactory;
27
Ray Milkey705d9bc2014-11-18 08:19:00 -080028import com.google.common.base.Strings;
29import com.google.common.primitives.UnsignedLongs;
30import com.google.common.util.concurrent.ThreadFactoryBuilder;
31
tom5f38b3a2014-08-27 23:50:54 -070032public abstract class Tools {
33
34 private Tools() {
35 }
36
37 /**
38 * Returns a thread factory that produces threads named according to the
39 * supplied name pattern.
40 *
41 * @param pattern name pattern
42 * @return thread factory
43 */
44 public static ThreadFactory namedThreads(String pattern) {
45 return new ThreadFactoryBuilder().setNameFormat(pattern).build();
46 }
47
tom782a7cf2014-09-11 23:58:38 -070048 /**
49 * Converts a string from hex to long.
50 *
51 * @param string hex number in string form; sans 0x
52 * @return long value
53 */
54 public static long fromHex(String string) {
55 return UnsignedLongs.parseUnsignedLong(string, 16);
56 }
57
58 /**
59 * Converts a long value to hex string; 16 wide and sans 0x.
60 *
61 * @param value long value
62 * @return hex string
63 */
64 public static String toHex(long value) {
65 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
66 }
67
68 /**
69 * Converts a long value to hex string; 16 wide and sans 0x.
70 *
71 * @param value long value
72 * @param width string width; zero padded
73 * @return hex string
74 */
75 public static String toHex(long value, int width) {
76 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
77 }
tomf110fff2014-09-26 00:38:18 -070078
79 /**
80 * Suspends the current thread for a specified number of millis.
81 *
82 * @param ms number of millis
83 */
84 public static void delay(int ms) {
85 try {
86 Thread.sleep(ms);
87 } catch (InterruptedException e) {
88 throw new RuntimeException("Interrupted", e);
89 }
90 }
91
tom53efab52014-10-07 17:43:48 -070092 /**
93 * Slurps the contents of a file into a list of strings, one per line.
94 *
95 * @param path file path
96 * @return file contents
97 */
98 public static List<String> slurp(File path) {
Ray Milkey705d9bc2014-11-18 08:19:00 -080099 try {
100 BufferedReader br = new BufferedReader(
101 new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
102
tom53efab52014-10-07 17:43:48 -0700103 List<String> lines = new ArrayList<>();
104 String line;
105 while ((line = br.readLine()) != null) {
106 lines.add(line);
107 }
108 return lines;
109
110 } catch (IOException e) {
111 return null;
112 }
113 }
114
tom5f38b3a2014-08-27 23:50:54 -0700115}