java.lang.Object
net.pincette.util.Util
General purpose utility functions.
- Author:
- Werner Donné
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
static class
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
R> Function<T, R> accumulate
(BiFunctionWithException<R, T, R> fn, R initialValue) This returns a function that accumulates the results.Produces a stream with all paths frompath
up to the root path, which is just thedelimiter
.static <T> Util.AutoCloseWrapper<T>
autoClose
(SupplierWithException<T> resource, ConsumerWithException<T> close) Wraps a resource in anAutoCloseable
.static String
canonicalPath
(String path, String delimiter) Converts a path with "." and ".." segments into a resolved canonical path.static <T,
R, V> Function<T, R> static byte[]
compress
(byte[] b) Compresses using GZIP.static Properties
copy
(Properties properties) Returs a copy ofproperties
.countingIterator
(Iterator<T> iterator) Returns an iterator over pairs where the first element is the result of the given iterator and the second the zero-based position in the list.static byte[]
decompress
(byte[] b) Decompresses using GZIP.static void
doForever
(RunnableWithException runnable) Executesrunnable
infinitely.static void
doForever
(RunnableWithException runnable, Consumer<Exception> error) Executesrunnable
infinitely.static void
doUntil
(SupplierWithException<Boolean> supplier) static void
doUntil
(SupplierWithException<Boolean> supplier, Consumer<Exception> error) Executessupplier
until it returnstrue
.static boolean
equalsOneOf
(Object o, Object... values) Returnstrue
if the given object equals one of the given values.static <T> Consumer<ConsumerWithException<T>>
from
(T value) Allows to write things likefrom(value).accept(v -> {...})
.getLastSegment
(String path, String delimiter) Returns the last segment of the path.static String
Returns the parent path ofpath
.getSegments
(String path, String delimiter) Returns the segments, but without the empty strings that can be generated by leading, trailing or consecutive delimiters.getSegments
(String path, Pattern delimiter) Returns the segments, but without the empty strings that can be generated by leading, trailing or consecutive delimiters.static String
static boolean
static boolean
static boolean
static boolean
static boolean
static boolean
static boolean
static boolean
static boolean
static <T> Iterator<T>
matcherIterator
(Matcher matcher, Function<Matcher, T> generator) static void
must
(boolean o) Throws an unchecked exception ifo
is false.static <T> T
static <T> T
must
(T o, Predicate<T> predicate, ConsumerWithException<T> report) Throws an unchecked exception if thepredicate
is not met and returnso
otherwise.static void
nop()
Does nothing.static <T> void
nop
(T arg) Does nothing.static String
static <T> Optional<T>
pathSearch
(Map<String, ? extends T> map, String path) static <T> Optional<T>
pathSearch
(Map<String, ? extends T> map, String[] path) static <T> Optional<T>
pathSearch
(Map<String, ? extends T> map, String[] path, Function<T, ?> evaluator) static <T> Optional<T>
pathSearch
(Map<String, ? extends T> map, String path, Function<T, ?> evaluator) Thepath
is a dot-separated string.static <T> Optional<T>
pathSearch
(Map<String, ? extends T> map, List<String> path) static <T> Optional<T>
Themap
is searched using thepath
.static void
readLineConfig
(Path path) readLineConfig
(Stream<String> lines) Returns lines from a stream of strings.static char[]
repeat
(char c, int count) static String
Replaces all matches ofpattern
ins
with the strings generated byreplacer
.static String
replaceParameters
(String s, Map<String, String> parameters) Replaces all occurrences of strings delimited by "${" and "}".static String
replaceParameters
(String s, Map<String, String> parameters, char leftBrace, char rightBrace) Replaces all occurrences of strings delimited by $leftBrace
andrightBrace
.static String
replaceParameters
(String s, Map<String, String> parameters, char leftBrace, char rightBrace, Set<String> leave) Replaces all occurrences of strings delimited by $leftBrace
andrightBrace
.static String
Replaces all occurrences of strings delimited by "${" and "}".resolveFile
(File baseDirectory, String path) static void
static Iterator<CharSequence>
segmentIterator
(String s, String delimiter) Iterates over the segments ins
that are separated by the literal stringdelimiter
.static Iterator<CharSequence>
segmentIterator
(String s, Pattern delimiter) Iterates over the segments ins
that are separated by the regular expressiondelimiter
.static Stream<CharSequence>
Returns a stream of the segments ins
that are separated by the literal stringdelimiter
.static Stream<CharSequence>
Returns a stream of the segments ins
that are separated by the regular expressiondelimiter
.static Properties
set
(Properties properties, String key, String value) Returns a new properties object with the new value.static String[]
Returns an array of the segments ins
that are separated by the literal stringdelimiter
.static String[]
Returns an array of the segments ins
that are separated by the regular expressiondelimiter
.Returns a pair of strings ins
that are separated by the literal stringdelimiter
.Returns a pair of strings ins
that are separated by the regular expressiondelimiter
.static <T,
R> Function<FunctionWithException<T, R>, R> to
(T value) Allows to write things liketo(value).apply(v -> ...)
.static char[]
toHex
(byte[] bytes) static boolean
static boolean
tryToDo
(RunnableWithException run, Consumer<Exception> error) static CompletionStage<Void>
tryToDoForever
(RunnableWithException run, Duration retryInterval) static CompletionStage<Void>
tryToDoForever
(RunnableWithException run, Duration retryInterval, Consumer<Exception> onException) Repeats the given function until it no longer throws an exception.static CompletionStage<Void>
tryToDoForever
(SupplierWithException<CompletionStage<Void>> run, Duration retryInterval) static CompletionStage<Void>
tryToDoForever
(SupplierWithException<CompletionStage<Void>> run, Duration retryInterval, Consumer<Exception> onException) Repeats the given function until it no longer throws an exception.static void
static boolean
static <T> boolean
tryToDoWith
(SupplierWithException<T> resource, ConsumerWithException<T> fn) static <T> boolean
tryToDoWith
(SupplierWithException<T> resource, ConsumerWithException<T> fn, Consumer<Exception> error) static <T> boolean
tryToDoWith
(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn) static <T> boolean
tryToDoWith
(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn, Consumer<Exception> error) static <T> boolean
tryToDoWithRethrow
(SupplierWithException<T> resource, ConsumerWithException<T> fn) static <T> boolean
tryToDoWithRethrow
(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn) static <T> boolean
tryToDoWithSilent
(SupplierWithException<T> resource, ConsumerWithException<T> fn) static <T> boolean
tryToDoWithSilent
(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn) static <T> Optional<T>
tryToGet
(SupplierWithException<T> run) static <T> Optional<T>
tryToGet
(SupplierWithException<T> run, Function<Exception, T> error) static <T> CompletionStage<T>
tryToGetForever
(SupplierWithException<CompletionStage<T>> run, Duration retryInterval) static <T> CompletionStage<T>
tryToGetForever
(SupplierWithException<CompletionStage<T>> run, Duration retryInterval, Consumer<Exception> onException) When the supplied completion stage completes withnull
it is also considered to be a failure and a retry will be performed.static <T> Optional<T>
tryToGetRethrow
(SupplierWithException<T> run) static <T> Optional<T>
tryToGetSilent
(SupplierWithException<T> run) static <T,
R> Optional<R> tryToGetWith
(SupplierWithException<T> resource, FunctionWithException<T, R> fn) static <T,
R> Optional<R> tryToGetWith
(SupplierWithException<T> resource, FunctionWithException<T, R> fn, Function<Exception, R> error) Tries to calculate a result with a resource that should implementAutoCloseable
.static <T,
R> Optional<R> tryToGetWith
(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn) static <T,
R> Optional<R> tryToGetWith
(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn, Function<Exception, R> error) static <T,
R> Optional<R> tryToGetWithRethrow
(SupplierWithException<T> resource, FunctionWithException<T, R> fn) static <T,
R> Optional<R> tryToGetWithRethrow
(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn) static <T,
R> Optional<R> tryToGetWithSilent
(SupplierWithException<T> resource, FunctionWithException<T, R> fn) static <T,
R> Optional<R> tryToGetWithSilent
(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn) static <T> CompletionStage<T>
waitFor
(Supplier<CompletionStage<Optional<T>>> condition, Duration interval) static <T> CompletionStage<Optional<T>>
waitFor
(Supplier<CompletionStage<Optional<T>>> condition, BooleanSupplier progress, Duration interval, Duration progressTimeout) static Supplier<CompletionStage<Optional<Boolean>>>
waitForCondition
(Supplier<CompletionStage<Boolean>> condition)
-
Method Details
-
accumulate
This returns a function that accumulates the results. Each call will receive the result of the previous call.- Type Parameters:
T
- the value type.R
- the result type.- Parameters:
fn
- the original function.initialValue
- the accumulated value for the first call.- Returns:
- The generated function.
- Since:
- 1.6.8
-
allPaths
Produces a stream with all paths frompath
up to the root path, which is just thedelimiter
. A trailingdelimiter
will be discarded.- Parameters:
path
- the path that will be decomposed.delimiter
- the literal string that separates the path segments.- Returns:
- The stream with the generated paths.
-
autoClose
public static <T> Util.AutoCloseWrapper<T> autoClose(SupplierWithException<T> resource, ConsumerWithException<T> close) Wraps a resource in anAutoCloseable
.- Type Parameters:
T
- the resource type.- Parameters:
resource
- the given resource.close
- the function that is called with the resource.- Returns:
- The wrapped resource.
-
canonicalPath
Converts a path with "." and ".." segments into a resolved canonical path.- Parameters:
path
- the original path.delimiter
- the path delimiter.- Returns:
- The canonical path.
- Since:
- 1.8.1
-
compose
-
compress
public static byte[] compress(byte[] b) Compresses using GZIP.- Parameters:
b
- the array to be compressed.- Returns:
- The compressed array.
-
copy
Returs a copy ofproperties
.- Parameters:
properties
- the given properties.- Returns:
- The copy of the properties object.
- Since:
- 1.8
-
countingIterator
Returns an iterator over pairs where the first element is the result of the given iterator and the second the zero-based position in the list.- Type Parameters:
T
- the element type.- Parameters:
iterator
- the given iterator.- Returns:
- The new iterator.
-
decompress
public static byte[] decompress(byte[] b) Decompresses using GZIP.- Parameters:
b
- the array to be decompressed.- Returns:
- The decompressed array.
-
doForever
Executesrunnable
infinitely.- Parameters:
runnable
- a function that may throw an exception, which will be rethrown.
-
doForever
Executesrunnable
infinitely.- Parameters:
runnable
- a function that may throw an exception, which will be rethrown.error
- the function that deals with an exception.
-
doUntil
-
doUntil
Executessupplier
until it returnstrue
.- Parameters:
supplier
- a function that may throw an exception, which will be rethrown.error
- the function that deals with an exception.- Since:
- 2.0.1
-
equalsOneOf
Returnstrue
if the given object equals one of the given values.- Parameters:
o
- the given object.values
- the values to compare with.- Returns:
- The comparison result.
-
from
Allows to write things likefrom(value).accept(v -> {...})
. This way you don't need to declare a variable for the value.- Type Parameters:
T
- the type of the given value.- Parameters:
value
- the given value.- Returns:
- The function that accepts a function to consume the value.
-
getLastSegment
Returns the last segment of the path.- Parameters:
path
- the given path.delimiter
- the string that separates the segments.- Returns:
- The optional segment.
-
getParent
Returns the parent path ofpath
.- Parameters:
path
- the given path.delimiter
- the string that separates the segments.- Returns:
- The parent path.
-
getSegments
Returns the segments, but without the empty strings that can be generated by leading, trailing or consecutive delimiters.- Parameters:
path
- the path that is split in segments.delimiter
- the string that separates the segments.- Returns:
- The segment stream.
-
getSegments
Returns the segments, but without the empty strings that can be generated by leading, trailing or consecutive delimiters.- Parameters:
path
- the path that is split in segments.delimiter
- the regular expression that separates the segments.- Returns:
- The segment stream.
-
getStackTrace
-
isDate
-
isDouble
-
isEmail
-
isFloat
-
isInstant
-
isInteger
-
isLong
-
isUUID
-
isUri
-
loadProperties
-
matcherIterator
-
must
public static void must(boolean o) Throws an unchecked exception ifo
is false.- Parameters:
o
- the object to test and return.- Since:
- 1.9.2
-
must
-
must
Throws an unchecked exception if thepredicate
is not met and returnso
otherwise.- Type Parameters:
T
- the type of the object.- Parameters:
o
- the object to test and return.predicate
- the predicate to be met.report
- the function that is called to report the failed predicate.- Returns:
- Returns
o
.
-
nop
public static void nop()Does nothing. -
nop
public static <T> void nop(T arg) Does nothing.- Type Parameters:
T
- the type of the ignored argument.- Parameters:
arg
- the ignored argument.
-
padWith
-
pathSearch
-
pathSearch
public static <T> Optional<T> pathSearch(Map<String, ? extends T> map, String path, Function<T, ?> evaluator) Thepath
is a dot-separated string.- Type Parameters:
T
- the value type.- Parameters:
map
- the map that is searched.path
- the path that is used for the search.evaluator
- used in the evaluation of expressions.- Returns:
- The optional value.
-
pathSearch
-
pathSearch
-
pathSearch
-
pathSearch
public static <T> Optional<T> pathSearch(Map<String, ? extends T> map, List<String> path, Function<T, ?> evaluator) Themap
is searched using thepath
. For segments in the path which represent an array an bracket enclosed expression may follow the name. In that case the array is assumed to contain maps. An optionalevaluator
can convert values which are found in those maps. The expressions should evaluate either to a boolean or a zero-based offset. The arithmatic operators "+", "-", "*" and "/" may be used to combine fields and literals. The generic typeT
is the type for the leafs of the tree.- Type Parameters:
T
- the value type.- Parameters:
map
- the map that is searched.path
- the path that is used for the search.evaluator
- used in the evaluation of expressions.- Returns:
- The optional value.
-
printStackTrace
-
readLineConfig
- Throws:
IOException
-
readLineConfig
-
readLineConfig
- Throws:
IOException
-
readLineConfig
Returns lines from a stream of strings. Comments start with the "#" character and are removed from the result. Lines can be split over multiple lines with the "\" character. The pieces will be assembled into a single line.- Parameters:
lines
- the given stream of lines.- Returns:
- The generated lines.
-
repeat
public static char[] repeat(char c, int count) -
replaceAll
Replaces all matches ofpattern
ins
with the strings generated byreplacer
.- Parameters:
s
- the input string.pattern
- the pattern to match.replacer
- the replacement generator.- Returns:
- The transformed string.
-
replaceParameters
Replaces all occurrences of strings delimited by "${" and "}".- Parameters:
s
- the string that is to transformed.parameters
- the parameters used for the replacement.- Returns:
- The transformed string.
-
replaceParameters
Replaces all occurrences of strings delimited by "${" and "}". When the name in such a string is inleave
no replacement is done.- Parameters:
s
- the string that is to transformed.parameters
- the parameters used for the replacement.leave
- the names that are excluded from replacement.- Returns:
- The transformed string.
-
replaceParameters
public static String replaceParameters(String s, Map<String, String> parameters, char leftBrace, char rightBrace) Replaces all occurrences of strings delimited by $leftBrace
andrightBrace
.- Parameters:
s
- the string that is to transformed.parameters
- the parameters used for the replacement.leftBrace
- the left delimiter.rightBrace
- the right delimiter.- Returns:
- The transformed string.
-
replaceParameters
public static String replaceParameters(String s, Map<String, String> parameters, char leftBrace, char rightBrace, Set<String> leave) Replaces all occurrences of strings delimited by $leftBrace
andrightBrace
. When the name in such a string is inleave
no replacement is done. When the name contains a colon then the actual name precedes that colon. The string after the colon is the default value for the replacement.- Parameters:
s
- the string that is to transformed.parameters
- the parameters used for the replacement.leftBrace
- the left delimiter.rightBrace
- the right delimiter.leave
- the names that are excluded from replacement.- Returns:
- The transformed string.
-
resolveFile
-
rethrow
-
segmentIterator
Iterates over the segments ins
that are separated by the regular expressiondelimiter
. The segment strings may be empty.- Parameters:
s
- the given string.delimiter
- the regular expression that separates the segments.- Returns:
- The segment iterator.
- Since:
- 2.0.4
-
segmentIterator
Iterates over the segments ins
that are separated by the literal stringdelimiter
. The segment strings may be empty.- Parameters:
s
- the given string.delimiter
- the literal string that separates the segments.- Returns:
- The segment iterator.
- Since:
- 2.0.4
-
segments
Returns a stream of the segments ins
that are separated by the literal stringdelimiter
. The segment strings may be empty.- Parameters:
s
- the given string.delimiter
- the literal string that separates the segments.- Returns:
- The segment stream.
- Since:
- 2.0.4
-
segments
Returns a stream of the segments ins
that are separated by the regular expressiondelimiter
. The segment strings may be empty.- Parameters:
s
- the given string.delimiter
- the regular expression that separates the segments.- Returns:
- The segment stream.
- Since:
- 2.0.4
-
set
Returns a new properties object with the new value.- Parameters:
properties
- the given properties.key
- the key.value
- the value.- Returns:
- The new properties object.
- Since:
- 1.8
-
split
Returns an array of the segments ins
that are separated by the literal stringdelimiter
. Empty strings are left out.- Parameters:
s
- the given string.delimiter
- the literal string that separates the segments.- Returns:
- The segment array.
- Since:
- 2.1.1
-
split
Returns an array of the segments ins
that are separated by the regular expressiondelimiter
. Empty strings are left out.- Parameters:
s
- the given string.delimiter
- the regular expression that separates the segments.- Returns:
- The segment array.
- Since:
- 2.1.1
-
splitPair
Returns a pair of strings ins
that are separated by the literal stringdelimiter
. Empty strings are left out. If there is no delimiter the second value of the pair will benull
.- Parameters:
s
- the given string.delimiter
- the literal string that separates the segments.- Returns:
- The pair.
- Since:
- 2.1.1
-
splitPair
Returns a pair of strings ins
that are separated by the regular expressiondelimiter
. Empty strings are left out. If there is no delimiter the second value of the pair will benull
.- Parameters:
s
- the given string.delimiter
- the literal string that separates the segments.- Returns:
- The pair.
- Since:
- 2.1.1
-
to
Allows to write things liketo(value).apply(v -> ...)
. This way you don't need to declare a variable for the value.- Type Parameters:
T
- the type of the given value.R
- the type of the returned value.- Parameters:
value
- the given value.- Returns:
- The function that accepts a function to apply to the value.
-
toHex
public static char[] toHex(byte[] bytes) -
tryToDo
-
tryToDo
-
tryToDoForever
public static CompletionStage<Void> tryToDoForever(RunnableWithException run, Duration retryInterval) -
tryToDoForever
public static CompletionStage<Void> tryToDoForever(RunnableWithException run, Duration retryInterval, Consumer<Exception> onException) Repeats the given function until it no longer throws an exception.- Parameters:
run
- the given function.retryInterval
- the time between retries.onException
- an optional exception handler.- Returns:
- The completion stage that completes when the given function succeeds.
- Since:
- 2.0.1
-
tryToDoForever
public static CompletionStage<Void> tryToDoForever(SupplierWithException<CompletionStage<Void>> run, Duration retryInterval) -
tryToDoForever
public static CompletionStage<Void> tryToDoForever(SupplierWithException<CompletionStage<Void>> run, Duration retryInterval, Consumer<Exception> onException) Repeats the given function until it no longer throws an exception.- Parameters:
run
- the given function.retryInterval
- the time between retries.onException
- an optional exception handler.- Returns:
- The completion stage that completes when the given function succeeds.
- Since:
- 2.0.1
-
tryToDoRethrow
-
tryToDoSilent
-
tryToDoWith
public static <T> boolean tryToDoWith(SupplierWithException<T> resource, ConsumerWithException<T> fn) -
tryToDoWith
public static <T> boolean tryToDoWith(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn) -
tryToDoWith
public static <T> boolean tryToDoWith(SupplierWithException<T> resource, ConsumerWithException<T> fn, Consumer<Exception> error) -
tryToDoWith
public static <T> boolean tryToDoWith(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn, Consumer<Exception> error) -
tryToDoWithRethrow
public static <T> boolean tryToDoWithRethrow(SupplierWithException<T> resource, ConsumerWithException<T> fn) -
tryToDoWithRethrow
public static <T> boolean tryToDoWithRethrow(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn) -
tryToDoWithSilent
public static <T> boolean tryToDoWithSilent(SupplierWithException<T> resource, ConsumerWithException<T> fn) -
tryToDoWithSilent
public static <T> boolean tryToDoWithSilent(Util.AutoCloseWrapper<T> resource, ConsumerWithException<T> fn) -
tryToGet
-
tryToGet
-
tryToGetForever
public static <T> CompletionStage<T> tryToGetForever(SupplierWithException<CompletionStage<T>> run, Duration retryInterval) -
tryToGetForever
public static <T> CompletionStage<T> tryToGetForever(SupplierWithException<CompletionStage<T>> run, Duration retryInterval, Consumer<Exception> onException) When the supplied completion stage completes withnull
it is also considered to be a failure and a retry will be performed.- Type Parameters:
T
- the object type.- Parameters:
run
- the supplier of the completion stage.retryInterval
- the time between retries.onException
- an optional exception handler.- Returns:
- The completion stage.
-
tryToGetRethrow
-
tryToGetSilent
-
tryToGetWith
public static <T,R> Optional<R> tryToGetWith(SupplierWithException<T> resource, FunctionWithException<T, R> fn) -
tryToGetWith
public static <T,R> Optional<R> tryToGetWith(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn) -
tryToGetWith
public static <T,R> Optional<R> tryToGetWith(SupplierWithException<T> resource, FunctionWithException<T, R> fn, Function<Exception, R> error) Tries to calculate a result with a resource that should implementAutoCloseable
.- Type Parameters:
T
- the resource type.R
- the result type.- Parameters:
resource
- the function that produces the resource.fn
- the function that calculates the result with the resource. It may returnnull
.error
- the exception handler.- Returns:
- The optional value.
-
tryToGetWith
public static <T,R> Optional<R> tryToGetWith(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn, Function<Exception, R> error) -
tryToGetWithRethrow
public static <T,R> Optional<R> tryToGetWithRethrow(SupplierWithException<T> resource, FunctionWithException<T, R> fn) -
tryToGetWithRethrow
public static <T,R> Optional<R> tryToGetWithRethrow(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn) -
tryToGetWithSilent
public static <T,R> Optional<R> tryToGetWithSilent(SupplierWithException<T> resource, FunctionWithException<T, R> fn) -
tryToGetWithSilent
public static <T,R> Optional<R> tryToGetWithSilent(Util.AutoCloseWrapper<T> resource, FunctionWithException<T, R> fn) -
waitFor
public static <T> CompletionStage<T> waitFor(Supplier<CompletionStage<Optional<T>>> condition, Duration interval) -
waitFor
public static <T> CompletionStage<Optional<T>> waitFor(Supplier<CompletionStage<Optional<T>>> condition, BooleanSupplier progress, Duration interval, Duration progressTimeout) -
waitForCondition
public static Supplier<CompletionStage<Optional<Boolean>>> waitForCondition(Supplier<CompletionStage<Boolean>> condition)
-