public abstract class Tools
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> |
allOf(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)
Returns a new CompletableFuture completed with a list of computed values
when all of the given CompletableFuture complete.
|
static <T> java.util.concurrent.CompletableFuture<T> |
allOf(java.util.List<java.util.concurrent.CompletableFuture<T>> futures,
java.util.function.BinaryOperator<T> reducer,
T emptyValue)
Returns a new CompletableFuture completed by reducing a list of computed values
when all of the given CompletableFuture complete.
|
static byte[] |
byteBuffertoArray(java.nio.ByteBuffer buffer)
Returns the contents of
ByteBuffer as byte array. |
static void |
copyDirectory(java.io.File src,
java.io.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(java.lang.String src,
java.lang.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 java.time.OffsetDateTime |
defaultOffsetDataTime(long epochMillis)
Creates OffsetDateTime instance from epoch milliseconds,
using system default time zone.
|
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> java.util.Set<T> |
emptyIsNotFound(java.util.Set<T> item,
java.lang.String message)
Returns the specified set if the set is not null and not empty;
otherwise throws a not found exception.
|
static <T> java.util.concurrent.CompletableFuture<T> |
exceptionalFuture(java.lang.Throwable t)
Returns a future that is completed exceptionally.
|
static <T> java.util.concurrent.CompletableFuture<T> |
firstOf(java.util.List<java.util.concurrent.CompletableFuture<T>> futures,
Match<T> positiveResultMatcher,
T negativeResult)
Returns a new CompletableFuture completed by with the first positive result from a list of
input CompletableFutures.
|
static long |
fromHex(java.lang.String string)
Converts a string from hex to long.
|
static <T> T |
futureGetOrElse(java.util.concurrent.Future<T> future,
long timeout,
java.util.concurrent.TimeUnit timeUnit,
T defaultValue)
Returns the future value when complete or if future
completes exceptionally returns the defaultValue.
|
static <T> T |
futureGetOrElse(java.util.concurrent.Future<T> future,
T defaultValue)
Returns the future value when complete or if future
completes exceptionally returns the defaultValue.
|
static java.lang.String |
get(java.util.Dictionary<?,?> properties,
java.lang.String propertyName)
Get property as a string value.
|
static byte[] |
getBytesUtf8(java.lang.String input)
Returns the UTF-8 encoded byte[] representation of a String.
|
static java.lang.Integer |
getIntegerProperty(java.util.Dictionary<?,?> properties,
java.lang.String propertyName)
Get Integer property from the propertyName
Return null if propertyName is not found.
|
static int |
getIntegerProperty(java.util.Dictionary<?,?> properties,
java.lang.String propertyName,
int defaultValue)
Get Integer property from the propertyName
Return default value if propertyName is not found.
|
static java.lang.Long |
getLongProperty(java.util.Dictionary<?,?> properties,
java.lang.String propertyName)
Get Long property from the propertyName
Return null if propertyName is not found.
|
static java.util.concurrent.ThreadFactory |
groupedThreads(java.lang.String groupName,
java.lang.String pattern)
Returns a thread factory that produces threads named according to the
supplied name pattern and from the specified thread-group.
|
static java.util.concurrent.ThreadFactory |
groupedThreads(java.lang.String groupName,
java.lang.String pattern,
org.slf4j.Logger logger)
Returns a thread factory that produces threads named according to the
supplied name pattern and from the specified thread-group.
|
static boolean |
isNullOrEmpty(java.util.Collection collection)
Returns true if the collection is null or is empty.
|
static java.lang.Boolean |
isPropertyEnabled(java.util.Dictionary<?,?> properties,
java.lang.String propertyName)
Check property name is defined and set to true.
|
static boolean |
isPropertyEnabled(java.util.Dictionary<?,?> properties,
java.lang.String propertyName,
boolean defaultValue)
Check property name is defined as set to true.
|
static java.util.concurrent.ThreadFactory |
maxPriority(java.util.concurrent.ThreadFactory factory)
Returns a thread factory that produces threads with MAX_PRIORITY.
|
static java.util.concurrent.ThreadFactory |
minPriority(java.util.concurrent.ThreadFactory factory)
Returns a thread factory that produces threads with MIN_PRIORITY.
|
static java.util.concurrent.ThreadFactory |
namedThreads(java.lang.String pattern)
Returns a thread factory that produces threads named according to the
supplied name pattern.
|
static <T> T |
nullIsIllegal(T item,
java.lang.String message)
Returns the specified item if that item is not null; otherwise throws
bad argument exception.
|
static <T> T |
nullIsNotFound(T item,
java.lang.String message)
Returns the specified item if that item is not null; otherwise throws
not found exception.
|
static <T> java.util.concurrent.CompletableFuture<T> |
orderedFuture(java.util.concurrent.CompletableFuture<T> future,
java.util.concurrent.Executor orderedExecutor,
java.util.concurrent.Executor threadPoolExecutor)
Returns a future that's completed using the given
orderedExecutor if the future is not blocked or the
given threadPoolExecutor if the future is blocked. |
static void |
randomDelay(int ms)
Suspends the current thread for a random number of millis between 0 and
the indicated limit.
|
static void |
removeDirectory(java.io.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(java.lang.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,
java.lang.Class<? extends java.lang.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,
java.lang.Class<? extends java.lang.Throwable> exceptionClass,
int maxRetries,
int maxDelayBetweenRetries)
Returns a Supplier that retries execution on failure.
|
static <T> java.util.stream.Stream<T> |
stream(java.lang.Iterable<T> it)
Converts an iterable to a stream.
|
static <T> java.util.stream.Stream<T> |
stream(java.util.Optional<? extends T> optional)
Converts an optional to a stream.
|
static java.lang.String |
timeAgo(long unixTime)
Returns a human friendly time ago string for a specified system time.
|
static java.lang.String |
toHex(long value)
Converts a long value to hex string; 16 wide and sans 0x.
|
static java.lang.String |
toHex(long value,
int width)
Converts a long value to hex string; 16 wide and sans 0x.
|
static java.lang.String |
toHexWithPrefix(long value)
Returns a string encoding in hex of the given long value with prefix
'0x'.
|
static java.lang.String |
toStringUtf8(byte[] input)
Returns the String representation of UTF-8 encoded byte[].
|
public static java.util.concurrent.ThreadFactory namedThreads(java.lang.String pattern)
pattern
- name patternpublic static java.util.concurrent.ThreadFactory groupedThreads(java.lang.String groupName, java.lang.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 java.util.concurrent.ThreadFactory groupedThreads(java.lang.String groupName, java.lang.String pattern, org.slf4j.Logger logger)
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. If a logger is specified, it will use the logger to
print out the exception if it has any.groupName
- group name in slash-delimited format to indicate hierarchypattern
- name patternlogger
- loggerpublic static java.util.concurrent.ThreadFactory minPriority(java.util.concurrent.ThreadFactory factory)
factory
- backing ThreadFactorypublic static java.util.concurrent.ThreadFactory maxPriority(java.util.concurrent.ThreadFactory factory)
factory
- backing ThreadFactorypublic static boolean isNullOrEmpty(java.util.Collection collection)
collection
- collection to testpublic static <T> T nullIsNotFound(T item, java.lang.String message)
T
- item typeitem
- item to checkmessage
- not found messageItemNotFoundException
- if item is nullpublic static <T> java.util.Set<T> emptyIsNotFound(java.util.Set<T> item, java.lang.String message)
T
- Set item typeitem
- set to checkmessage
- not found messageItemNotFoundException
- if set is null or emptypublic static <T> T nullIsIllegal(T item, java.lang.String message)
T
- item typeitem
- item to checkmessage
- not found messagejava.lang.IllegalArgumentException
- if item is nullpublic static long fromHex(java.lang.String string)
string
- hex number in string form; sans 0xpublic static java.lang.String toHex(long value)
value
- long valuepublic static java.lang.String toHex(long value, int width)
value
- long valuewidth
- string width; zero paddedpublic static java.lang.String toHexWithPrefix(long value)
value
- long value to encode as hex stringpublic static byte[] getBytesUtf8(java.lang.String input)
input
- input stringpublic static java.lang.String toStringUtf8(byte[] input)
input
- input byte arraypublic static byte[] copyOf(byte[] original)
original
- inputpublic static java.lang.String get(java.util.Dictionary<?,?> properties, java.lang.String propertyName)
properties
- properties to be looked uppropertyName
- the name of the property to look uppublic static java.lang.Integer getIntegerProperty(java.util.Dictionary<?,?> properties, java.lang.String propertyName)
properties
- properties to be looked uppropertyName
- the name of the property to look uppublic static int getIntegerProperty(java.util.Dictionary<?,?> properties, java.lang.String propertyName, int defaultValue)
properties
- properties to be looked uppropertyName
- the name of the property to look updefaultValue
- the default value that to be assignedpublic static java.lang.Boolean isPropertyEnabled(java.util.Dictionary<?,?> properties, java.lang.String propertyName)
properties
- properties to be looked uppropertyName
- the name of the property to look uppublic static boolean isPropertyEnabled(java.util.Dictionary<?,?> properties, java.lang.String propertyName, boolean defaultValue)
properties
- properties to be looked uppropertyName
- the name of the property to look updefaultValue
- the default value that to be assignedpublic static void delay(int ms)
ms
- number of millispublic static java.lang.Long getLongProperty(java.util.Dictionary<?,?> properties, java.lang.String propertyName)
properties
- properties to be looked uppropertyName
- the name of the property to look uppublic static <U,V> java.util.function.Function<U,V> retryable(java.util.function.Function<U,V> base, java.lang.Class<? extends java.lang.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, java.lang.Class<? extends java.lang.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 void removeDirectory(java.lang.String path) throws java.io.IOException
path
- directory to be removedjava.io.IOException
- if unable to remove contentspublic static void removeDirectory(java.io.File dir) throws java.io.IOException
dir
- directory to be removedjava.io.IOException
- if unable to remove contentspublic static java.lang.String timeAgo(long unixTime)
unixTime
- system time in millispublic static void copyDirectory(java.lang.String src, java.lang.String dst) throws java.io.IOException
src
- directory to be copieddst
- destination directory to be removedjava.io.IOException
- if unable to remove contentspublic static void copyDirectory(java.io.File src, java.io.File dst) throws java.io.IOException
src
- directory to be copieddst
- destination directory to be removedjava.io.IOException
- if unable to remove contentspublic static <T> T futureGetOrElse(java.util.concurrent.Future<T> future, T defaultValue)
T
- future value typefuture
- futuredefaultValue
- default valuepublic static <T> T futureGetOrElse(java.util.concurrent.Future<T> future, long timeout, java.util.concurrent.TimeUnit timeUnit, T defaultValue)
T
- future value typefuture
- futuretimeout
- time to wait for successful completiontimeUnit
- time unitdefaultValue
- default valuepublic static <T> java.util.concurrent.CompletableFuture<T> exceptionalFuture(java.lang.Throwable t)
T
- future value typet
- exceptionpublic static <T> java.util.concurrent.CompletableFuture<T> orderedFuture(java.util.concurrent.CompletableFuture<T> future, java.util.concurrent.Executor orderedExecutor, java.util.concurrent.Executor threadPoolExecutor)
orderedExecutor
if the future is not blocked or the
given threadPoolExecutor
if the future is blocked.
This method allows futures to maintain single-thread semantics via the provided orderedExecutor
while
ensuring user code can block without blocking completion of futures. When the returned future or any of its
descendants is blocked on a CompletableFuture.get()
or CompletableFuture.join()
call, completion
of the returned future will be done using the provided threadPoolExecutor
.
T
- future value typefuture
- the future to convert into an asynchronous futureorderedExecutor
- the ordered executor with which to attempt to complete the futurethreadPoolExecutor
- the backup executor with which to complete blocked futuresexecutor
once the provided
future
is completepublic static <T> java.util.concurrent.CompletableFuture<java.util.List<T>> allOf(java.util.List<java.util.concurrent.CompletableFuture<T>> futures)
T
- value type of CompletableFuturefutures
- the CompletableFuturespublic static <T> java.util.concurrent.CompletableFuture<T> allOf(java.util.List<java.util.concurrent.CompletableFuture<T>> futures, java.util.function.BinaryOperator<T> reducer, T emptyValue)
T
- value type of CompletableFuturefutures
- the CompletableFuturesreducer
- reducer for computing the resultemptyValue
- zero value to be returned if the input future list is emptypublic static <T> java.util.concurrent.CompletableFuture<T> firstOf(java.util.List<java.util.concurrent.CompletableFuture<T>> futures, Match<T> positiveResultMatcher, T negativeResult)
T
- value type of CompletableFuturefutures
- the input list of CompletableFuturespositiveResultMatcher
- matcher to identify a positive resultnegativeResult
- value to complete with if none of the futures complete with a positive resultpublic static byte[] byteBuffertoArray(java.nio.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(java.lang.Iterable<T> it)
T
- type if itemit
- iterable to convertpublic static <T> java.util.stream.Stream<T> stream(java.util.Optional<? extends T> optional)
T
- type of enclosed valueoptional
- optional to convertpublic static java.time.OffsetDateTime defaultOffsetDataTime(long epochMillis)
epochMillis
- to convert