blob: 2b261a6b8137462308f8422c1c8a404148f3fb06 [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 /**
Yuta HIGUCHI06586272014-11-25 14:27:03 -080049 * Returns a thread factory that produces threads with MIN_PRIORITY.
50 *
51 * @param factory backing ThreadFactory
52 * @return thread factory
53 */
54 public static ThreadFactory minPriority(ThreadFactory factory) {
55 return new ThreadFactoryBuilder()
56 .setThreadFactory(factory)
57 .setPriority(Thread.MIN_PRIORITY)
58 .build();
59 }
60
61 /**
tom782a7cf2014-09-11 23:58:38 -070062 * Converts a string from hex to long.
63 *
64 * @param string hex number in string form; sans 0x
65 * @return long value
66 */
67 public static long fromHex(String string) {
68 return UnsignedLongs.parseUnsignedLong(string, 16);
69 }
70
71 /**
72 * Converts a long value to hex string; 16 wide and sans 0x.
73 *
74 * @param value long value
75 * @return hex string
76 */
77 public static String toHex(long value) {
78 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
79 }
80
81 /**
82 * Converts a long value to hex string; 16 wide and sans 0x.
83 *
84 * @param value long value
85 * @param width string width; zero padded
86 * @return hex string
87 */
88 public static String toHex(long value, int width) {
89 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
90 }
tomf110fff2014-09-26 00:38:18 -070091
92 /**
93 * Suspends the current thread for a specified number of millis.
94 *
95 * @param ms number of millis
96 */
97 public static void delay(int ms) {
98 try {
99 Thread.sleep(ms);
100 } catch (InterruptedException e) {
101 throw new RuntimeException("Interrupted", e);
102 }
103 }
104
tom53efab52014-10-07 17:43:48 -0700105 /**
106 * Slurps the contents of a file into a list of strings, one per line.
107 *
108 * @param path file path
109 * @return file contents
110 */
111 public static List<String> slurp(File path) {
Ray Milkey705d9bc2014-11-18 08:19:00 -0800112 try {
113 BufferedReader br = new BufferedReader(
114 new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
115
tom53efab52014-10-07 17:43:48 -0700116 List<String> lines = new ArrayList<>();
117 String line;
118 while ((line = br.readLine()) != null) {
119 lines.add(line);
120 }
121 return lines;
122
123 } catch (IOException e) {
124 return null;
125 }
126 }
127
tom5f38b3a2014-08-27 23:50:54 -0700128}