Class GenericUtils

    • Field Detail

      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
      • EMPTY_CHAR_ARRAY

        public static final char[] EMPTY_CHAR_ARRAY
      • EMPTY_STRING_ARRAY

        public static final String[] EMPTY_STRING_ARRAY
      • EMPTY_OBJECT_ARRAY

        public static final Object[] EMPTY_OBJECT_ARRAY
      • EMPTY_BOOLEAN_ARRAY

        public static final boolean[] EMPTY_BOOLEAN_ARRAY
      • NULL

        public static final Object NULL
        A value indicating a null value - to be used as a placeholder where nulls are not allowed
    • Method Detail

      • trimToEmpty

        public static String trimToEmpty​(String s)
      • replaceWhitespaceAndTrim

        public static String replaceWhitespaceAndTrim​(String s)
      • replace

        public static String replace​(String text,
                                     String repl,
                                     String with,
                                     int max)

        Replace a String with another String inside a larger String, for the first max values of the search String.

        A null reference passed to this method is a no-op.

        Parameters:
        text - text to search and replace in
        repl - String to search for
        with - String to replace with
        max - maximum number of values to replace, or -1 if no maximum
        Returns:
        the text with any replacements processed
      • hashCode

        public static int hashCode​(String s)
        Parameters:
        s - The String value to calculate the hash code on - may be null/empty in which case a value of zero is returned
        Returns:
        The calculated hash code
        See Also:
        hashCode(String, Boolean)
      • hashCode

        public static int hashCode​(String s,
                                   Boolean useUppercase)
        Parameters:
        s - The String value to calculate the hash code on - may be null/empty in which case a value of zero is returned
        useUppercase - Whether to convert the string to uppercase, lowercase or not at all:
        Returns:
        The calculated hash code
      • safeCompare

        public static int safeCompare​(String s1,
                                      String s2,
                                      boolean caseSensitive)
      • isEmpty

        public static boolean isEmpty​(CharSequence cs)
      • isNotEmpty

        public static boolean isNotEmpty​(CharSequence cs)
      • indexOf

        public static int indexOf​(CharSequence cs,
                                  char c)
      • lastIndexOf

        public static int lastIndexOf​(CharSequence cs,
                                      char c)
      • split

        public static String[] split​(String s,
                                     char ch)
      • join

        public static <T> String join​(T[] values,
                                      char ch)
      • size

        public static int size​(Collection<?> c)
      • isEmpty

        public static boolean isEmpty​(Collection<?> c)
      • isNotEmpty

        public static boolean isNotEmpty​(Collection<?> c)
      • equals

        public static <T> boolean equals​(Collection<T> c1,
                                         Collection<T> c2)
        Type Parameters:
        T - Generic element type
        Parameters:
        c1 - First collection
        c2 - Second collection
        Returns:
        true if the following holds:
        • Same size - Note: null collections are consider equal to empty ones
        • First collection contains all elements of second one and vice versa
      • length

        @SafeVarargs
        public static <T> int length​(T... a)
      • isEmpty

        public static <T> boolean isEmpty​(Iterable<? extends T> iter)
      • isNotEmpty

        public static <T> boolean isNotEmpty​(Iterable<? extends T> iter)
      • isEmpty

        public static <T> boolean isEmpty​(Iterator<? extends T> iter)
      • isNotEmpty

        public static <T> boolean isNotEmpty​(Iterator<? extends T> iter)
      • isEmpty

        public static <T> boolean isEmpty​(T[] a)
      • length

        public static int length​(char[] chars)
      • isEmpty

        public static boolean isEmpty​(char[] chars)
      • compare

        public static int compare​(char[] c1,
                                  char[] c2)
        Compares 2 character arrays - Note: null and empty are considered equal
        Parameters:
        c1 - 1st array
        c2 - 2nd array
        Returns:
        Negative is 1st array comes first in lexicographical order, positive if 2nd array comes first and zero if equal
      • of

        public static <E extends Enum<E>> Set<E> of​(Collection<? extends E> values)
      • findFirstDifferentValueIndex

        public static <T> int findFirstDifferentValueIndex​(List<? extends T> c1,
                                                           List<? extends T> c2)
      • findFirstDifferentValueIndex

        public static <T> int findFirstDifferentValueIndex​(List<? extends T> c1,
                                                           List<? extends T> c2,
                                                           UnaryEquator<? super T> equator)
      • findFirstDifferentValueIndex

        public static <T> int findFirstDifferentValueIndex​(Iterable<? extends T> c1,
                                                           Iterable<? extends T> c2)
      • findFirstDifferentValueIndex

        public static <T> int findFirstDifferentValueIndex​(Iterable<? extends T> c1,
                                                           Iterable<? extends T> c2,
                                                           UnaryEquator<? super T> equator)
      • findFirstDifferentValueIndex

        public static <T> int findFirstDifferentValueIndex​(Iterator<? extends T> i1,
                                                           Iterator<? extends T> i2)
      • findFirstDifferentValueIndex

        public static <T> int findFirstDifferentValueIndex​(Iterator<? extends T> i1,
                                                           Iterator<? extends T> i2,
                                                           UnaryEquator<? super T> equator)
      • containsAny

        public static <T> boolean containsAny​(Collection<? extends T> coll,
                                              Iterable<? extends T> values)
      • forEach

        public static <T> void forEach​(Iterable<? extends T> values,
                                       Consumer<? super T> consumer)
      • map

        public static <T,​U> List<U> map​(Collection<? extends T> values,
                                              Function<? super T,​? extends U> mapper)
      • stream

        public static <T> Stream<T> stream​(Iterable<T> values)
      • unmodifiableList

        @SafeVarargs
        public static <T> List<T> unmodifiableList​(T... values)
      • unmodifiableList

        public static <T> List<T> unmodifiableList​(Collection<? extends T> values)
      • unmodifiableList

        public static <T> List<T> unmodifiableList​(Stream<T> values)
      • asSortedSet

        @SafeVarargs
        public static <V> NavigableSet<V> asSortedSet​(Comparator<? super V> comp,
                                                      V... values)
        Type Parameters:
        V - The element type
        Parameters:
        comp - The (non-null) Comparator to use
        values - The values to be added (ignored if null)
        Returns:
        A NavigableSet containing the values (if any) sorted using the provided comparator
      • asSortedSet

        public static <V> NavigableSet<V> asSortedSet​(Comparator<? super V> comp,
                                                      Collection<? extends V> values)
        Type Parameters:
        V - The element type
        Parameters:
        comp - The (non-null) Comparator to use
        values - The values to be added (ignored if null/empty)
        Returns:
        A NavigableSet containing the values (if any) sorted using the provided comparator
      • findFirstMatchingMember

        @SafeVarargs
        public static <T> T findFirstMatchingMember​(Predicate<? super T> acceptor,
                                                    T... values)
      • findFirstMatchingMember

        public static <T> T findFirstMatchingMember​(Predicate<? super T> acceptor,
                                                    Collection<? extends T> values)
      • selectMatchingMembers

        @SafeVarargs
        public static <T> List<T> selectMatchingMembers​(Predicate<? super T> acceptor,
                                                        T... values)
        Returns a list of all the values that were accepted by a predicate
        Type Parameters:
        T - The type of value being evaluated
        Parameters:
        acceptor - The Predicate to consult whether a member is selected
        values - The values to be scanned
        Returns:
        A List of all the values that were accepted by the predicate
      • selectMatchingMembers

        public static <T> List<T> selectMatchingMembers​(Predicate<? super T> acceptor,
                                                        Collection<? extends T> values)
        Returns a list of all the values that were accepted by a predicate
        Type Parameters:
        T - The type of value being evaluated
        Parameters:
        acceptor - The Predicate to consult whether a member is selected
        values - The values to be scanned
        Returns:
        A List of all the values that were accepted by the predicate
      • stripDelimiters

        public static CharSequence stripDelimiters​(CharSequence s,
                                                   char delim)
        Parameters:
        s - The CharSequence to be checked
        delim - The expected delimiter
        Returns:
        If the sequence contains the delimiter on both ends, then it is are stripped, otherwise nothing is done
      • supplierOf

        public static <T> Supplier<T> supplierOf​(T value)
        Wraps a value into a Supplier
        Type Parameters:
        T - Type of value being supplied
        Parameters:
        value - The value to be supplied
        Returns:
        The supplier wrapper
      • iteratorOf

        public static <T> Iterator<T> iteratorOf​(Iterable<T> iterable)
        Resolves to an always non-null iterator
        Type Parameters:
        T - Type of value being iterated
        Parameters:
        iterable - The Iterable instance
        Returns:
        A non-null iterator which may be empty if no iterable instance or no iterator returned from it
        See Also:
        iteratorOf(Iterator)
      • downcast

        public static <B,​D extends B> Function<D,​B> downcast()
        Type Parameters:
        B - Generic base class
        D - Generic child class
        Returns:
        An identity Function that returns its input child class as a base class
      • head

        public static <T> T head​(Iterable<? extends T> it)
        Returns the first element in iterable - it has some optimization for List-s Deque-s and SortedSets.
        Type Parameters:
        T - Type of element
        Parameters:
        it - The Iterable instance - ignored if null/empty
        Returns:
        first element by iteration or null if none available
      • iteratorOf

        public static <T> Iterator<T> iteratorOf​(Iterator<T> iter)
        Resolves to an always non-null iterator
        Type Parameters:
        T - Type of value being iterated
        Parameters:
        iter - The Iterator instance
        Returns:
        A non-null iterator which may be empty if no iterator instance
        See Also:
        Collections.emptyIterator()
      • wrapIterable

        public static <U,​V> Iterable<V> wrapIterable​(Iterable<? extends U> iter,
                                                           Function<? super U,​? extends V> mapper)
      • wrapIterator

        public static <U,​V> Iterator<V> wrapIterator​(Iterable<? extends U> iter,
                                                           Function<? super U,​? extends V> mapper)
      • wrapIterator

        public static <U,​V> Iterator<V> wrapIterator​(Iterator<? extends U> iter,
                                                           Function<? super U,​? extends V> mapper)
      • selectNextMatchingValue

        public static <T> T selectNextMatchingValue​(Iterator<?> values,
                                                    Class<T> type)
        Type Parameters:
        T - Generic return type
        Parameters:
        values - The source values - ignored if null
        type - The (never @code null) type of values to select - any value whose type is assignable to this type will be selected by the iterator.
        Returns:
        The first value that matches the specified type - null if none found
      • multiIterableSuppliers

        public static <T> Iterable<T> multiIterableSuppliers​(Iterable<? extends Supplier<? extends Iterable<? extends T>>> providers)
        Wraps a group of Suppliers of Iterable instances into a "unified" Iterable of their values, in the same order as the suppliers - i.e., once the values from a specific supplier are exhausted, the next one is consulted, and so on, until all suppliers have been consulted
        Type Parameters:
        T - Type of value being iterated
        Parameters:
        providers - The providers - ignored if null (i.e., return an empty iterable instance)
        Returns:
        The wrapping instance
      • memoizeLock

        public static <T> Supplier<T> memoizeLock​(Supplier<? extends T> delegate)
        The delegate Suppliers get() method is called exactly once and the result is cached.
        Type Parameters:
        T - Generic type of supplied value
        Parameters:
        delegate - The actual Supplier
        Returns:
        The memoized Supplier
      • isPositive

        public static boolean isPositive​(Duration d)
        Check if a duration is positive
        Parameters:
        d - the duration
        Returns:
        true if the duration is greater than zero
      • isNegativeOrNull

        public static boolean isNegativeOrNull​(Duration d)
        Check if a duration is negative or zero
        Parameters:
        d - the duration
        Returns:
        true if the duration is negative or zero