tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 1 | package org.onlab.util; |
| 2 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 3 | import com.google.common.base.Strings; |
| 4 | import com.google.common.primitives.UnsignedLongs; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 5 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 6 | |
| 7 | import java.util.concurrent.ThreadFactory; |
| 8 | |
| 9 | public 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 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 25 | /** |
| 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 | } |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 55 | } |