Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 20 | import java.io.BufferedReader; |
| 21 | import java.io.File; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 22 | import java.io.FileInputStream; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 23 | import java.io.IOException; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 24 | import java.io.InputStreamReader; |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 25 | import java.lang.Thread.UncaughtExceptionHandler; |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 26 | import java.nio.charset.StandardCharsets; |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 29 | import java.util.concurrent.ThreadFactory; |
| 30 | |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | |
Ray Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 33 | import com.google.common.base.Strings; |
| 34 | import com.google.common.primitives.UnsignedLongs; |
| 35 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 36 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 37 | public abstract class Tools { |
| 38 | |
| 39 | private Tools() { |
| 40 | } |
| 41 | |
Yuta HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 42 | private static final Logger TOOLS_LOG = getLogger(Tools.class); |
| 43 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 44 | /** |
| 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 HIGUCHI | 683e978 | 2014-11-25 17:26:36 -0800 | [diff] [blame] | 52 | 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(); |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 62 | } |
| 63 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 64 | /** |
Yuta HIGUCHI | 0658627 | 2014-11-25 14:27:03 -0800 | [diff] [blame] | 65 | * 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 | /** |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 78 | * 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 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 107 | |
| 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 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 121 | /** |
| 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 Milkey | 705d9bc | 2014-11-18 08:19:00 -0800 | [diff] [blame] | 128 | try { |
| 129 | BufferedReader br = new BufferedReader( |
| 130 | new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8)); |
| 131 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 132 | 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 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 144 | } |