blob: e908300f1d7b4d1f6741eb127049d21d34a5d02c [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
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080018import static org.slf4j.LoggerFactory.getLogger;
19
tom53efab52014-10-07 17:43:48 -070020import java.io.BufferedReader;
21import java.io.File;
Ray Milkey705d9bc2014-11-18 08:19:00 -080022import java.io.FileInputStream;
tom53efab52014-10-07 17:43:48 -070023import java.io.IOException;
Ray Milkey705d9bc2014-11-18 08:19:00 -080024import java.io.InputStreamReader;
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080025import java.lang.Thread.UncaughtExceptionHandler;
Ray Milkey705d9bc2014-11-18 08:19:00 -080026import java.nio.charset.StandardCharsets;
tom53efab52014-10-07 17:43:48 -070027import java.util.ArrayList;
28import java.util.List;
tom5f38b3a2014-08-27 23:50:54 -070029import java.util.concurrent.ThreadFactory;
30
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080031import org.slf4j.Logger;
32
Ray Milkey705d9bc2014-11-18 08:19:00 -080033import com.google.common.base.Strings;
34import com.google.common.primitives.UnsignedLongs;
35import com.google.common.util.concurrent.ThreadFactoryBuilder;
36
tom5f38b3a2014-08-27 23:50:54 -070037public abstract class Tools {
38
39 private Tools() {
40 }
41
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080042 private static final Logger TOOLS_LOG = getLogger(Tools.class);
43
tom5f38b3a2014-08-27 23:50:54 -070044 /**
45 * Returns a thread factory that produces threads named according to the
46 * supplied name pattern.
47 *
48 * @param pattern name pattern
49 * @return thread factory
50 */
51 public static ThreadFactory namedThreads(String pattern) {
Yuta HIGUCHI683e9782014-11-25 17:26:36 -080052 return new ThreadFactoryBuilder()
53 .setNameFormat(pattern)
54 // FIXME remove UncaughtExceptionHandler before release
55 .setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
56
57 @Override
58 public void uncaughtException(Thread t, Throwable e) {
59 TOOLS_LOG.error("Uncaught exception on {}", t.getName(), e);
60 }
61 }).build();
tom5f38b3a2014-08-27 23:50:54 -070062 }
63
tom782a7cf2014-09-11 23:58:38 -070064 /**
Yuta HIGUCHI06586272014-11-25 14:27:03 -080065 * Returns a thread factory that produces threads with MIN_PRIORITY.
66 *
67 * @param factory backing ThreadFactory
68 * @return thread factory
69 */
70 public static ThreadFactory minPriority(ThreadFactory factory) {
71 return new ThreadFactoryBuilder()
72 .setThreadFactory(factory)
73 .setPriority(Thread.MIN_PRIORITY)
74 .build();
75 }
76
77 /**
tom782a7cf2014-09-11 23:58:38 -070078 * Converts a string from hex to long.
79 *
80 * @param string hex number in string form; sans 0x
81 * @return long value
82 */
83 public static long fromHex(String string) {
84 return UnsignedLongs.parseUnsignedLong(string, 16);
85 }
86
87 /**
88 * Converts a long value to hex string; 16 wide and sans 0x.
89 *
90 * @param value long value
91 * @return hex string
92 */
93 public static String toHex(long value) {
94 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
95 }
96
97 /**
98 * Converts a long value to hex string; 16 wide and sans 0x.
99 *
100 * @param value long value
101 * @param width string width; zero padded
102 * @return hex string
103 */
104 public static String toHex(long value, int width) {
105 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
106 }
tomf110fff2014-09-26 00:38:18 -0700107
108 /**
109 * Suspends the current thread for a specified number of millis.
110 *
111 * @param ms number of millis
112 */
113 public static void delay(int ms) {
114 try {
115 Thread.sleep(ms);
116 } catch (InterruptedException e) {
117 throw new RuntimeException("Interrupted", e);
118 }
119 }
120
tom53efab52014-10-07 17:43:48 -0700121 /**
122 * Slurps the contents of a file into a list of strings, one per line.
123 *
124 * @param path file path
125 * @return file contents
126 */
127 public static List<String> slurp(File path) {
Ray Milkey705d9bc2014-11-18 08:19:00 -0800128 try {
129 BufferedReader br = new BufferedReader(
130 new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
131
tom53efab52014-10-07 17:43:48 -0700132 List<String> lines = new ArrayList<>();
133 String line;
134 while ((line = br.readLine()) != null) {
135 lines.add(line);
136 }
137 return lines;
138
139 } catch (IOException e) {
140 return null;
141 }
142 }
143
tom5f38b3a2014-08-27 23:50:54 -0700144}