Package de.arstwo.twotil
Class Util
java.lang.Object
de.arstwo.twotil.Util
Colletion of various utility functions that fit nowhere else.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleanReturns true if all values pass the given validator.static <T> Predicate<T> Creates a new predicate that is equivalent to AND together the given predicates.static <T> booleanReturns true if at least one value fails the given validator.static <T> booleanReturns 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> TReturns the first element that passes the given validator.static <T> TfirstNonBlank(T... values) Returns the first non-blank element.static <T> TfirstNonEmpty(T... values) Returns the first non-empty element.static <T> TfirstNonNull(T... values) Returns the first non-null element.static <T,E extends Throwable>
TfirstOrThrow(Predicate<T> validator, Supplier<E> throwable, T... values) Returns the first element that passes the given validator, otherwise throw the supplied throwable.getAllClassesFromPackage(String sourcePackage) Creates a list of all classes in a given package using the current ClassLoader.static <T> TgetOrDefault(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>
TgetOrDefaultExceptional(T optionalValue, ThrowingSupplier<T, E> generator) Functional convenience function to return the given value, or a default value if it is null.static StringGets the current stack trace as a string for debugging.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.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> booleanisBlank(T t) Tests whether or not something is either null or contains no relevant content.static <T> booleanisEmpty(T t) Tests whether or not something is either null or contains no content at all.static <T> booleanisNotBlank(T t) static <T> booleanisNotEmpty(T t) static StringjoinNonBlank(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>
RmapIfPresentExceptional(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> Convenience function to map a whole list with a given mapper and returns the result in a new list.static <T> booleanReturns true if no value passes the given validator.static <T> Predicate<T> Creates a new predicate that is equivalent to OR together the given predicates.static DatestartOfMonth(Date inputDate) Returns the 1st day at 0:00 of the month of a given date.static DateConverts a LocalDate into a Date by applying the systems default timezone.static LocalDatetoLocalDate(Date d) Converts a Date into a LocalDate by applying the systems default timezone.
-
Method Details
-
first
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
Returns true if no value passes the given validator. -
allMatch
Returns true if all values pass the given validator. -
anyMatch
Returns true if at least one value passes the given validator. -
anyFail
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
Creates a new predicate that is equivalent to AND together the given predicates. -
OR
Creates a new predicate that is equivalent to OR together the given predicates. -
getOrDefault
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 EFunctional 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
Functional convenience function to map the given value if present, or otherwise return null. -
mapIfPresentExceptional
public static <T,R, R mapIfPresentExceptionalE extends Throwable> (T optionalValue, ThrowingFunction<T, R, throws EE> mapper) 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
Joins all non-blank items together. -
getStackTrace
Gets the current stack trace as a string for debugging. -
mapList
Convenience function to map a whole list with a given mapper and returns the result in a new list. -
toDate
Converts a LocalDate into a Date by applying the systems default timezone. -
toLocalDate
Converts a Date into a LocalDate by applying the systems default timezone.Safely detects sql Dates that somehow forgot to implement toInstant.
-
startOfMonth
Returns the 1st day at 0:00 of the month of a given date. -
inverseMap
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
Convenience function to group a list of items by a specific function and return a map.- Type Parameters:
K- anyV- any- Parameters:
list- list of items to groupkeyMapper- 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- anyV- any- Parameters:
list- list of items to groupkeyMapper- a method that determines which key shall be associated with a given list itemcomparator- comparator for sorting- Returns:
- the grouped map with sorted list members
-
compareDatesASC
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
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
Creates a list of all classes in a given package using the current ClassLoader.- Throws:
IOException
-