Class Util

java.lang.Object
net.pincette.util.Util

public class Util extends Object
General purpose utility functions.
Author:
Werner Donné
  • Method Details

    • accumulate

      public static <T, R> Function<T,R> accumulate(BiFunctionWithException<R,T,R> fn, R initialValue)
      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

      public static Stream<String> allPaths(String path, String delimiter)
      Produces a stream with all paths from path up to the root path, which is just the delimiter. A trailing delimiter 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 an AutoCloseable.
      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

      public static String canonicalPath(String path, String delimiter)
      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

      public static <T, R, V> Function<T,R> compose(Function<T,V> fn1, Function<V,R> fn2)
    • compress

      public static byte[] compress(byte[] b)
      Compresses using GZIP.
      Parameters:
      b - the array to be compressed.
      Returns:
      The compressed array.
    • copy

      public static Properties copy(Properties properties)
      Returs a copy of properties.
      Parameters:
      properties - the given properties.
      Returns:
      The copy of the properties object.
      Since:
      1.8
    • countingIterator

      public static <T> Iterator<Pair<T,Integer>> 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.
      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

      public static void doForever(RunnableWithException runnable)
      Executes runnable infinitely.
      Parameters:
      runnable - a function that may throw an exception, which will be rethrown.
    • doForever

      public static void doForever(RunnableWithException runnable, Consumer<Exception> error)
      Executes runnable infinitely.
      Parameters:
      runnable - a function that may throw an exception, which will be rethrown.
      error - the function that deals with an exception.
    • doUntil

      public static void doUntil(SupplierWithException<Boolean> supplier)
    • doUntil

      public static void doUntil(SupplierWithException<Boolean> supplier, Consumer<Exception> error)
      Executes supplier until it returns true.
      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

      public static boolean equalsOneOf(Object o, Object... values)
      Returns true 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

      public static <T> Consumer<ConsumerWithException<T>> from(T value)
      Allows to write things like from(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

      public static Optional<String> getLastSegment(String path, String delimiter)
      Returns the last segment of the path.
      Parameters:
      path - the given path.
      delimiter - the string that separates the segments.
      Returns:
      The optional segment.
    • getParent

      public static String getParent(String path, String delimiter)
      Returns the parent path of path.
      Parameters:
      path - the given path.
      delimiter - the string that separates the segments.
      Returns:
      The parent path.
    • getSegments

      public static Stream<String> getSegments(String path, String delimiter)
      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

      public static Stream<String> getSegments(String path, Pattern delimiter)
      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

      public static String getStackTrace(Throwable e)
    • isDate

      public static boolean isDate(String s)
    • isDouble

      public static boolean isDouble(String s)
    • isEmail

      public static boolean isEmail(String s)
    • isFloat

      public static boolean isFloat(String s)
    • isInstant

      public static boolean isInstant(String s)
    • isInteger

      public static boolean isInteger(String s)
    • isLong

      public static boolean isLong(String s)
    • isUUID

      public static boolean isUUID(String s)
    • isUri

      public static boolean isUri(String s)
    • loadProperties

      public static Map<String,String> loadProperties(Supplier<InputStream> in)
    • matcherIterator

      public static <T> Iterator<T> matcherIterator(Matcher matcher, Function<Matcher,T> generator)
    • must

      public static void must(boolean o)
      Throws an unchecked exception if o is false.
      Parameters:
      o - the object to test and return.
      Since:
      1.9.2
    • must

      public static <T> T must(T o, Predicate<T> predicate)
    • must

      public static <T> T must(T o, Predicate<T> predicate, ConsumerWithException<T> report)
      Throws an unchecked exception if the predicate is not met and returns o 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

      public static String padWith(String s, char c, int size)
    • pathSearch

      public static <T> Optional<T> pathSearch(Map<String,? extends T> map, String path)
    • pathSearch

      public static <T> Optional<T> pathSearch(Map<String,? extends T> map, String path, Function<T,?> evaluator)
      The path 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

      public static <T> Optional<T> pathSearch(Map<String,? extends T> map, String[] path)
    • pathSearch

      public static <T> Optional<T> pathSearch(Map<String,? extends T> map, String[] path, Function<T,?> evaluator)
    • pathSearch

      public static <T> Optional<T> pathSearch(Map<String,? extends T> map, List<String> path)
    • pathSearch

      public static <T> Optional<T> pathSearch(Map<String,? extends T> map, List<String> path, Function<T,?> evaluator)
      The map is searched using the path. 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 optional evaluator 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 type T 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

      public static void printStackTrace(Throwable e)
    • readLineConfig

      public static Stream<String> readLineConfig(InputStream in) throws IOException
      Throws:
      IOException
    • readLineConfig

      public static Stream<String> readLineConfig(BufferedReader in)
    • readLineConfig

      public static Stream<String> readLineConfig(Path path) throws IOException
      Throws:
      IOException
    • readLineConfig

      public static Stream<String> readLineConfig(Stream<String> lines)
      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

      public static String replaceAll(String s, Pattern pattern, Function<Matcher,String> replacer)
      Replaces all matches of pattern in s with the strings generated by replacer.
      Parameters:
      s - the input string.
      pattern - the pattern to match.
      replacer - the replacement generator.
      Returns:
      The transformed string.
    • replaceParameters

      public static String replaceParameters(String s, Map<String,String> parameters)
      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

      public static String replaceParameters(String s, Map<String,String> parameters, Set<String> leave)
      Replaces all occurrences of strings delimited by "${" and "}". When the name in such a string is in leave 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 and rightBrace .
      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 and rightBrace . When the name in such a string is in leave 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

      public static Optional<File> resolveFile(File baseDirectory, String path)
    • rethrow

      public static void rethrow(Throwable t)
    • segmentIterator

      public static Iterator<CharSequence> segmentIterator(String s, Pattern delimiter)
      Iterates over the segments in s that are separated by the regular expression delimiter. 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

      public static Iterator<CharSequence> segmentIterator(String s, String delimiter)
      Iterates over the segments in s that are separated by the literal string delimiter. 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

      public static Stream<CharSequence> segments(String s, String delimiter)
      Returns a stream of the segments in s that are separated by the literal string delimiter. 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

      public static Stream<CharSequence> segments(String s, Pattern delimiter)
      Returns a stream of the segments in s that are separated by the regular expression delimiter. 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

      public static Properties set(Properties properties, String key, String value)
      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

      public static String[] split(String s, String delimiter)
      Returns an array of the segments in s that are separated by the literal string delimiter. 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

      public static String[] split(String s, Pattern delimiter)
      Returns an array of the segments in s that are separated by the regular expression delimiter. 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

      public static Pair<String,String> splitPair(String s, String delimiter)
      Returns a pair of strings in s that are separated by the literal string delimiter. Empty strings are left out. If there is no delimiter the second value of the pair will be null.
      Parameters:
      s - the given string.
      delimiter - the literal string that separates the segments.
      Returns:
      The pair.
      Since:
      2.1.1
    • splitPair

      public static Pair<String,String> splitPair(String s, Pattern delimiter)
      Returns a pair of strings in s that are separated by the regular expression delimiter. Empty strings are left out. If there is no delimiter the second value of the pair will be null.
      Parameters:
      s - the given string.
      delimiter - the literal string that separates the segments.
      Returns:
      The pair.
      Since:
      2.1.1
    • to

      public static <T, R> Function<FunctionWithException<T,R>,R> to(T value)
      Allows to write things like to(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

      public static boolean tryToDo(RunnableWithException run)
    • tryToDo

      public static boolean tryToDo(RunnableWithException run, Consumer<Exception> error)
    • 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

      public static void tryToDoRethrow(RunnableWithException run)
    • tryToDoSilent

      public static boolean tryToDoSilent(RunnableWithException run)
    • 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

      public static <T> Optional<T> tryToGet(SupplierWithException<T> run)
    • tryToGet

      public static <T> Optional<T> tryToGet(SupplierWithException<T> run, Function<Exception,T> error)
    • 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 with null 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

      public static <T> Optional<T> tryToGetRethrow(SupplierWithException<T> run)
    • tryToGetSilent

      public static <T> Optional<T> tryToGetSilent(SupplierWithException<T> run)
    • 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 implement AutoCloseable.
      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 return null .
      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)