Class Util

java.lang.Object
de.arstwo.twotil.Util

public class Util extends Object
Colletion of various utility functions that fit nowhere else.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> boolean
    allMatch(Predicate<T> validator, T... values)
    Returns true if all values pass the given validator.
    static <T> Predicate<T>
    AND(Predicate<T>... predicates)
    Creates a new predicate that is equivalent to AND together the given predicates.
    static <T> boolean
    anyFail(Predicate<T> validator, T... values)
    Returns true if at least one value fails the given validator.
    static <T> boolean
    anyMatch(Predicate<T> validator, T... values)
    Returns true if at least one value passes the given validator.
    static <T> Comparator<T>
    compareDatesASC(Function<T,Date> dateGetFunc)
    Creates a comparator that sorts data based on their associated dates ascending.
    static <T> Comparator<T>
    compareDatesDESC(Function<T,Date> dateGetFunc)
    Creates a comparator that sorts data based on their associated dates descending.
    static <T> T
    first(Predicate<T> validator, T... values)
    Returns the first element that passes the given validator.
    static <T> T
    firstNonBlank(T... values)
    Returns the first non-blank element.
    static <T> T
    firstNonEmpty(T... values)
    Returns the first non-empty element.
    static <T> T
    firstNonNull(T... values)
    Returns the first non-null element.
    static <T, E extends Throwable>
    T
    firstOrThrow(Predicate<T> validator, Supplier<E> throwable, T... values)
    Returns the first element that passes the given validator, otherwise throw the supplied throwable.
    static List<Class<?>>
    Creates a list of all classes in a given package using the current ClassLoader.
    static <T> T
    getOrDefault(T optionalValue, Supplier<T> generator)
    Functional convenience function to return the given value, or a default value if it is null.
    static <T, E extends Throwable>
    T
    getOrDefaultExceptional(T optionalValue, ThrowingSupplier<T,E> generator)
    Functional convenience function to return the given value, or a default value if it is null.
    static String
    Gets the current stack trace as a string for debugging.
    static <K, V> Map<K,List<V>>
    groupList(List<V> list, Function<V,K> keyMapper)
    Convenience function to group a list of items by a specific function and return a map.
    static <K, V> Map<K,NavigableSet<V>>
    groupListSorted(List<V> list, Function<V,K> keyMapper, Comparator<V> comparator)
    Convenience function to group a list of items by a specific function and return a map.
    static <V, K> Map<V,List<K>>
    inverseMap(Map<K,V> map)
    Creates the inverse of a Map, as in: returns a map where all values point to the corresponding keys that link to them in the original map.
    static <T> boolean
    isBlank(T t)
    Tests whether or not something is either null or contains no relevant content.
    static <T> boolean
    isEmpty(T t)
    Tests whether or not something is either null or contains no content at all.
    static <T> boolean
     
    static <T> boolean
     
    static String
    joinNonBlank(String delimiter, String... parts)
    Joins all non-blank items together.
    static <T, R> R
    mapIfPresent(T optionalValue, Function<T,R> mapper)
    Functional convenience function to map the given value if present, or otherwise return null.
    static <T, R, E extends Throwable>
    R
    mapIfPresentExceptional(T optionalValue, ThrowingFunction<T,R,E> mapper)
    Functional convenience function to map the given value if present, or otherwise return null.
    static <T, R> List<R>
    mapList(Collection<T> collection, Function<T,R> mapper)
    Convenience function to map a whole collection with a given mapper and returns the result in a new list.
    static <T, R> Set<R>
    mapSet(Collection<T> collection, Function<T,R> mapper)
    Convenience function to map a whole collection with a given mapper and returns the result in a new set.
    static <T> boolean
    noneMatch(Predicate<T> validator, T... values)
    Returns true if no value passes the given validator.
    static <T> Predicate<T>
    OR(Predicate<T>... predicates)
    Creates a new predicate that is equivalent to OR together the given predicates.
    static Date
    startOfMonth(Date inputDate)
    Returns the 1st day at 0:00 of the month of a given date.
    static Date
    Converts a LocalDate into a Date by applying the systems default timezone.
    static LocalDate
    Converts a Date into a LocalDate by applying the systems default timezone.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • first

      public static <T> T first(Predicate<T> validator, T... values)
      Returns the first element that passes the given validator.
    • firstOrThrow

      public static <T, E extends Throwable> T firstOrThrow(Predicate<T> validator, Supplier<E> throwable, T... values) throws E
      Returns the first element that passes the given validator, otherwise throw the supplied throwable.
      Throws:
      E
    • firstNonNull

      public static <T> T firstNonNull(T... values)
      Returns the first non-null element.
    • firstNonEmpty

      public static <T> T firstNonEmpty(T... values)
      Returns the first non-empty element.
    • firstNonBlank

      public static <T> T firstNonBlank(T... values)
      Returns the first non-blank element.
    • noneMatch

      public static <T> boolean noneMatch(Predicate<T> validator, T... values)
      Returns true if no value passes the given validator.
    • allMatch

      public static <T> boolean allMatch(Predicate<T> validator, T... values)
      Returns true if all values pass the given validator.
    • anyMatch

      public static <T> boolean anyMatch(Predicate<T> validator, T... values)
      Returns true if at least one value passes the given validator.
    • anyFail

      public static <T> boolean anyFail(Predicate<T> validator, T... values)
      Returns true if at least one value fails the given validator.
    • isEmpty

      public static <T> boolean isEmpty(T t)
      Tests whether or not something is either null or contains no content at all.
    • isNotEmpty

      public static <T> boolean isNotEmpty(T t)
    • isBlank

      public static <T> boolean isBlank(T t)
      Tests whether or not something is either null or contains no relevant content.

      A String is blank if it contains nothing or only whitespace.
      A Colection is blank if it contains nothing or only blank elements.
      An Array is blank if it contains nothing, or - in case of an array of Objects - all objects contained are blank. Arrays of primitive data types do not check individual elements, as there is no definition what a blank value is for e.g. a float.

    • isNotBlank

      public static <T> boolean isNotBlank(T t)
    • AND

      public static <T> Predicate<T> AND(Predicate<T>... predicates)
      Creates a new predicate that is equivalent to AND together the given predicates.
    • OR

      public static <T> Predicate<T> OR(Predicate<T>... predicates)
      Creates a new predicate that is equivalent to OR together the given predicates.
    • getOrDefault

      public static <T> T getOrDefault(T optionalValue, Supplier<T> generator)
      Functional convenience function to return the given value, or a default value if it is null.
    • getOrDefaultExceptional

      public static <T, E extends Throwable> T getOrDefaultExceptional(T optionalValue, ThrowingSupplier<T,E> generator) throws E
      Functional convenience function to return the given value, or a default value if it is null. Compatible with methods that might throw an exception.
      Throws:
      E
    • mapIfPresent

      public static <T, R> R mapIfPresent(T optionalValue, Function<T,R> mapper)
      Functional convenience function to map the given value if present, or otherwise return null.
    • mapIfPresentExceptional

      public static <T, R, E extends Throwable> R mapIfPresentExceptional(T optionalValue, ThrowingFunction<T,R,E> mapper) throws E
      Functional convenience function to map the given value if present, or otherwise return null. Compatible with methods that might throw an exception.
      Throws:
      E
    • joinNonBlank

      public static String joinNonBlank(String delimiter, String... parts)
      Joins all non-blank items together.
    • getStackTrace

      public static String getStackTrace()
      Gets the current stack trace as a string for debugging.
    • mapList

      public static <T, R> List<R> mapList(Collection<T> collection, Function<T,R> mapper)
      Convenience function to map a whole collection with a given mapper and returns the result in a new list.
    • mapSet

      public static <T, R> Set<R> mapSet(Collection<T> collection, Function<T,R> mapper)
      Convenience function to map a whole collection with a given mapper and returns the result in a new set.
    • toDate

      public static Date toDate(LocalDate ld)
      Converts a LocalDate into a Date by applying the systems default timezone.
    • toLocalDate

      public static LocalDate toLocalDate(Date d)
      Converts a Date into a LocalDate by applying the systems default timezone.

      Safely detects sql Dates that somehow forgot to implement toInstant.

    • startOfMonth

      public static Date startOfMonth(Date inputDate)
      Returns the 1st day at 0:00 of the month of a given date.
    • inverseMap

      public static <V, K> Map<V,List<K>> inverseMap(Map<K,V> map)
      Creates the inverse of a Map, as in: returns a map where all values point to the corresponding keys that link to them in the original map.
    • groupList

      public static <K, V> Map<K,List<V>> groupList(List<V> list, Function<V,K> keyMapper)
      Convenience function to group a list of items by a specific function and return a map.
      Type Parameters:
      K - any
      V - any
      Parameters:
      list - list of items to group
      keyMapper - a method that determines which key shall be associated with a given list item
      Returns:
      the grouped map
    • groupListSorted

      public static <K, V> Map<K,NavigableSet<V>> groupListSorted(List<V> list, Function<V,K> keyMapper, Comparator<V> comparator)
      Convenience function to group a list of items by a specific function and return a map. In addition the individual items in each list are sorted by the given comparator.
      Type Parameters:
      K - any
      V - any
      Parameters:
      list - list of items to group
      keyMapper - a method that determines which key shall be associated with a given list item
      comparator - comparator for sorting
      Returns:
      the grouped map with sorted list members
    • compareDatesASC

      public static <T> Comparator<T> compareDatesASC(Function<T,Date> dateGetFunc)
      Creates a comparator that sorts data based on their associated dates ascending.
      Type Parameters:
      T - any
      Parameters:
      dateGetFunc - function that retrieves the dates associated with a given data element
      Returns:
      a comparator that sorts the data by date ascending.
    • compareDatesDESC

      public static <T> Comparator<T> compareDatesDESC(Function<T,Date> dateGetFunc)
      Creates a comparator that sorts data based on their associated dates descending.
      Type Parameters:
      T - any
      Parameters:
      dateGetFunc - function that retrieves the dates associated with a given data element
      Returns:
      a comparator that sorts the data by date descending.
    • getAllClassesFromPackage

      public static List<Class<?>> getAllClassesFromPackage(String sourcePackage) throws IOException
      Creates a list of all classes in a given package using the current ClassLoader.
      Throws:
      IOException