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 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
| 19 | import com.google.common.primitives.UnsignedLongs; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 20 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 21 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 22 | import java.io.BufferedReader; |
| 23 | import java.io.File; |
| 24 | import java.io.FileReader; |
| 25 | import java.io.IOException; |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 28 | import java.util.concurrent.ThreadFactory; |
| 29 | |
| 30 | public abstract class Tools { |
| 31 | |
| 32 | private Tools() { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns a thread factory that produces threads named according to the |
| 37 | * supplied name pattern. |
| 38 | * |
| 39 | * @param pattern name pattern |
| 40 | * @return thread factory |
| 41 | */ |
| 42 | public static ThreadFactory namedThreads(String pattern) { |
| 43 | return new ThreadFactoryBuilder().setNameFormat(pattern).build(); |
| 44 | } |
| 45 | |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Converts a string from hex to long. |
| 48 | * |
| 49 | * @param string hex number in string form; sans 0x |
| 50 | * @return long value |
| 51 | */ |
| 52 | public static long fromHex(String string) { |
| 53 | return UnsignedLongs.parseUnsignedLong(string, 16); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 58 | * |
| 59 | * @param value long value |
| 60 | * @return hex string |
| 61 | */ |
| 62 | public static String toHex(long value) { |
| 63 | return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0'); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Converts a long value to hex string; 16 wide and sans 0x. |
| 68 | * |
| 69 | * @param value long value |
| 70 | * @param width string width; zero padded |
| 71 | * @return hex string |
| 72 | */ |
| 73 | public static String toHex(long value, int width) { |
| 74 | return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0'); |
| 75 | } |
tom | f110fff | 2014-09-26 00:38:18 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Suspends the current thread for a specified number of millis. |
| 79 | * |
| 80 | * @param ms number of millis |
| 81 | */ |
| 82 | public static void delay(int ms) { |
| 83 | try { |
| 84 | Thread.sleep(ms); |
| 85 | } catch (InterruptedException e) { |
| 86 | throw new RuntimeException("Interrupted", e); |
| 87 | } |
| 88 | } |
| 89 | |
tom | 53efab5 | 2014-10-07 17:43:48 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Slurps the contents of a file into a list of strings, one per line. |
| 92 | * |
| 93 | * @param path file path |
| 94 | * @return file contents |
| 95 | */ |
| 96 | public static List<String> slurp(File path) { |
| 97 | try (BufferedReader br = new BufferedReader(new FileReader(path))) { |
| 98 | List<String> lines = new ArrayList<>(); |
| 99 | String line; |
| 100 | while ((line = br.readLine()) != null) { |
| 101 | lines.add(line); |
| 102 | } |
| 103 | return lines; |
| 104 | |
| 105 | } catch (IOException e) { |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 110 | } |