blob: c5162f66b9f432e08494cea757aa4e21473fc535 [file] [log] [blame]
tom5f38b3a2014-08-27 23:50:54 -07001package org.onlab.util;
2
tom782a7cf2014-09-11 23:58:38 -07003import com.google.common.base.Strings;
4import com.google.common.primitives.UnsignedLongs;
tom5f38b3a2014-08-27 23:50:54 -07005import com.google.common.util.concurrent.ThreadFactoryBuilder;
6
7import java.util.concurrent.ThreadFactory;
8
9public abstract class Tools {
10
11 private Tools() {
12 }
13
14 /**
15 * Returns a thread factory that produces threads named according to the
16 * supplied name pattern.
17 *
18 * @param pattern name pattern
19 * @return thread factory
20 */
21 public static ThreadFactory namedThreads(String pattern) {
22 return new ThreadFactoryBuilder().setNameFormat(pattern).build();
23 }
24
tom782a7cf2014-09-11 23:58:38 -070025 /**
26 * Converts a string from hex to long.
27 *
28 * @param string hex number in string form; sans 0x
29 * @return long value
30 */
31 public static long fromHex(String string) {
32 return UnsignedLongs.parseUnsignedLong(string, 16);
33 }
34
35 /**
36 * Converts a long value to hex string; 16 wide and sans 0x.
37 *
38 * @param value long value
39 * @return hex string
40 */
41 public static String toHex(long value) {
42 return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
43 }
44
45 /**
46 * Converts a long value to hex string; 16 wide and sans 0x.
47 *
48 * @param value long value
49 * @param width string width; zero padded
50 * @return hex string
51 */
52 public static String toHex(long value, int width) {
53 return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
54 }
tomf110fff2014-09-26 00:38:18 -070055
56 /**
57 * Suspends the current thread for a specified number of millis.
58 *
59 * @param ms number of millis
60 */
61 public static void delay(int ms) {
62 try {
63 Thread.sleep(ms);
64 } catch (InterruptedException e) {
65 throw new RuntimeException("Interrupted", e);
66 }
67 }
68
tom5f38b3a2014-08-27 23:50:54 -070069}