blob: 5e63b84eb0cbe2329344451b63c89ca07e5fb522 [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 }
tom5f38b3a2014-08-27 23:50:54 -070055}