public abstract class Tools extends Object
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
byteBuffertoArray(ByteBuffer buffer)
Returns the contents of
ByteBuffer as byte array. |
static void |
copyDirectory(File src,
File dst)
Copies the specified directory path. Use with great caution since
no attempt is made to check for symbolic links, which could result in
copy of unintended files.
|
static void |
copyDirectory(String src,
String dst)
Copies the specified directory path. Use with great caution since
no attempt is made to check for symbolic links, which could result in
copy of unintended files.
|
static byte[] |
copyOf(byte[] original)
Returns a copy of the input byte array.
|
static void |
delay(int ms)
Suspends the current thread for a specified number of millis.
|
static void |
delay(int ms,
int nanos)
Suspends the current thread for a specified number of millis and nanos.
|
static <T> CompletableFuture<T> |
exceptionalFuture(Throwable t)
Returns a future that is completed exceptionally.
|
static long |
fromHex(String string)
Converts a string from hex to long.
|
static <T> T |
futureGetOrElse(Future<T> future,
long timeout,
TimeUnit timeUnit,
T defaultValue)
Returns the future value when complete or if future
completes exceptionally returns the defaultValue.
|
static <T> T |
futureGetOrElse(Future<T> future,
T defaultValue)
Returns the future value when complete or if future
completes exceptionally returns the defaultValue.
|
static String |
get(Dictionary<?,?> properties,
String propertyName)
Get property as a string value.
|
static ThreadFactory |
groupedThreads(String groupName,
String pattern)
Returns a thread factory that produces threads named according to the
supplied name pattern and from the specified thread-group.
|
static boolean |
isNullOrEmpty(Collection collection)
Returns true if the collection is null or is empty.
|
static ThreadFactory |
minPriority(ThreadFactory factory)
Returns a thread factory that produces threads with MIN_PRIORITY.
|
static ThreadFactory |
namedThreads(String pattern)
Returns a thread factory that produces threads named according to the
supplied name pattern.
|
static <T> T |
nullIsIllegal(T item,
String message)
Returns the specified item if that item is not null; otherwise throws
bad argument exception.
|
static <T> T |
nullIsNotFound(T item,
String message)
Returns the specified item if that item is not null; otherwise throws
not found exception.
|
static void |
randomDelay(int ms)
Suspends the current thread for a random number of millis between 0 and
the indicated limit.
|
static void |
removeDirectory(File dir)
Purges the specified directory path. Use with great caution since
no attempt is made to check for symbolic links, which could result in
deletion of unintended files.
|
static void |
removeDirectory(String path)
Purges the specified directory path. Use with great caution since
no attempt is made to check for symbolic links, which could result in
deletion of unintended files.
|
static <U,V> java.util.function.Function<U,V> |
retryable(java.util.function.Function<U,V> base,
Class<? extends Throwable> exceptionClass,
int maxRetries,
int maxDelayBetweenRetries)
Returns a function that retries execution on failure.
|
static <V> java.util.function.Supplier<V> |
retryable(java.util.function.Supplier<V> base,
Class<? extends Throwable> exceptionClass,
int maxRetries,
int maxDelayBetweenRetries)
Returns a Supplier that retries execution on failure.
|
static List<String> |
slurp(File path)
Slurps the contents of a file into a list of strings, one per line.
|
static <T> java.util.stream.Stream<T> |
stream(Iterable<T> it)
Converts an iterable to a stream.
|
static String |
timeAgo(long unixTime)
Returns a human friendly time ago string for a specified system time.
|
static String |
toHex(long value)
Converts a long value to hex string; 16 wide and sans 0x.
|
static String |
toHex(long value,
int width)
Converts a long value to hex string; 16 wide and sans 0x.
|
public static ThreadFactory namedThreads(String pattern)
pattern - name patternpublic static ThreadFactory groupedThreads(String groupName, String pattern)
onos/intent. The thread names will be produced by converting
the thread group name into dash-delimited format and pre-pended to the
specified pattern.groupName - group name in slash-delimited format to indicate hierarchypattern - name patternpublic static ThreadFactory minPriority(ThreadFactory factory)
factory - backing ThreadFactorypublic static boolean isNullOrEmpty(Collection collection)
collection - collection to testpublic static <T> T nullIsNotFound(T item,
String message)
T - item typeitem - item to checkmessage - not found messageItemNotFoundException - if item is nullpublic static <T> T nullIsIllegal(T item,
String message)
T - item typeitem - item to checkmessage - not found messageIllegalArgumentException - if item is nullpublic static long fromHex(String string)
string - hex number in string form; sans 0xpublic static String toHex(long value)
value - long valuepublic static String toHex(long value, int width)
value - long valuewidth - string width; zero paddedpublic static byte[] copyOf(byte[] original)
original - inputpublic static String get(Dictionary<?,?> properties, String propertyName)
properties - properties to be looked uppropertyName - the name of the property to look uppublic static void delay(int ms)
ms - number of millispublic static <U,V> java.util.function.Function<U,V> retryable(java.util.function.Function<U,V> base, Class<? extends Throwable> exceptionClass, int maxRetries, int maxDelayBetweenRetries)
U - type of function inputV - type of function outputbase - base functionexceptionClass - type of exception for which to retrymaxRetries - max number of retries before giving upmaxDelayBetweenRetries - max delay between successive retries. The actual delay is randomly picked from
the interval (0, maxDelayBetweenRetries]public static <V> java.util.function.Supplier<V> retryable(java.util.function.Supplier<V> base, Class<? extends Throwable> exceptionClass, int maxRetries, int maxDelayBetweenRetries)
V - type of supplied resultbase - base supplierexceptionClass - type of exception for which to retrymaxRetries - max number of retries before giving upmaxDelayBetweenRetries - max delay between successive retries. The actual delay is randomly picked from
the interval (0, maxDelayBetweenRetries]public static void randomDelay(int ms)
ms - max number of millispublic static void delay(int ms,
int nanos)
ms - number of millisnanos - number of nanospublic static List<String> slurp(File path)
path - file pathpublic static void removeDirectory(String path) throws IOException
path - directory to be removedIOException - if unable to remove contentspublic static void removeDirectory(File dir) throws IOException
dir - directory to be removedIOException - if unable to remove contentspublic static String timeAgo(long unixTime)
unixTime - system time in millispublic static void copyDirectory(String src, String dst) throws IOException
src - directory to be copieddst - destination directory to be removedIOException - if unable to remove contentspublic static void copyDirectory(File src, File dst) throws IOException
src - directory to be copieddst - destination directory to be removedIOException - if unable to remove contentspublic static <T> T futureGetOrElse(Future<T> future, T defaultValue)
T - future value typefuture - futuredefaultValue - default valuepublic static <T> T futureGetOrElse(Future<T> future, long timeout, TimeUnit timeUnit, T defaultValue)
T - future value typefuture - futuretimeout - time to wait for successful completiontimeUnit - time unitdefaultValue - default valuepublic static <T> CompletableFuture<T> exceptionalFuture(Throwable t)
T - future value typet - exceptionpublic static byte[] byteBuffertoArray(ByteBuffer buffer)
ByteBuffer as byte array.
WARNING: There is a performance cost due to array copy when using this method.
buffer - byte bufferpublic static <T> java.util.stream.Stream<T> stream(Iterable<T> it)
T - type if itemit - iterable to convertCopyright © 2015. All rights reserved.