Class GenericUtils


  • public final class GenericUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GenericUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<T> asList​(T... values)  
      static <T> java.util.Set<T> asSet​(T... values)  
      static <V extends java.lang.Comparable<V>>
      java.util.NavigableSet<V>
      asSortedSet​(java.util.Collection<? extends V> values)  
      static <V> java.util.NavigableSet<V> asSortedSet​(java.util.Comparator<? super V> comp, java.util.Collection<? extends V> values)  
      static <V> java.util.NavigableSet<V> asSortedSet​(java.util.Comparator<? super V> comp, V... values)  
      static <V extends java.lang.Comparable<V>>
      java.util.NavigableSet<V>
      asSortedSet​(V... values)  
      static int compare​(char[] c1, char[] c2)
      Compares 2 character arrays - Note: null and empty are considered equal
      static <T> boolean containsAny​(java.util.Collection<? extends T> coll, java.lang.Iterable<? extends T> values)  
      static <B,​D extends B>
      java.util.function.Function<D,​B>
      downcast()  
      static <T> boolean equals​(java.util.Collection<T> c1, java.util.Collection<T> c2)  
      static java.util.List<java.lang.String> filterToNotBlank​(java.util.List<java.lang.String> values)  
      static <T> int findFirstDifferentValueIndex​(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2)  
      static <T> int findFirstDifferentValueIndex​(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, UnaryEquator<? super T> equator)  
      static <T> int findFirstDifferentValueIndex​(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2)  
      static <T> int findFirstDifferentValueIndex​(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, UnaryEquator<? super T> equator)  
      static <T> int findFirstDifferentValueIndex​(java.util.List<? extends T> c1, java.util.List<? extends T> c2)  
      static <T> int findFirstDifferentValueIndex​(java.util.List<? extends T> c1, java.util.List<? extends T> c2, UnaryEquator<? super T> equator)  
      static <T> T findFirstMatchingMember​(java.util.function.Predicate<? super T> acceptor, java.util.Collection<? extends T> values)  
      static <T> T findFirstMatchingMember​(java.util.function.Predicate<? super T> acceptor, T... values)  
      static <T> void forEach​(java.lang.Iterable<? extends T> values, java.util.function.Consumer<? super T> consumer)  
      static int hashCode​(java.lang.String s)  
      static int hashCode​(java.lang.String s, java.lang.Boolean useUppercase)  
      static <T> T head​(java.lang.Iterable<? extends T> it)
      Returns the first element in iterable - it has some optimization for List-s Deque-s and SortedSets.
      static int indexOf​(java.lang.CharSequence cs, char c)  
      static boolean isBlank​(java.lang.CharSequence cs)
      Checks if a CharSequence is empty (""), null or whitespace only.
      static boolean isEmpty​(char[] chars)  
      static boolean isEmpty​(java.lang.CharSequence cs)  
      static <T> boolean isEmpty​(java.lang.Iterable<? extends T> iter)  
      static boolean isEmpty​(java.util.Collection<?> c)  
      static <T> boolean isEmpty​(java.util.Iterator<? extends T> iter)  
      static <T> boolean isEmpty​(T[] a)  
      static boolean isNegativeOrNull​(java.time.Duration d)
      Check if a duration is negative or zero
      static boolean isNotBlank​(java.lang.CharSequence cs)  
      static boolean isNotEmpty​(java.lang.CharSequence cs)  
      static <T> boolean isNotEmpty​(java.lang.Iterable<? extends T> iter)  
      static boolean isNotEmpty​(java.util.Collection<?> c)  
      static <T> boolean isNotEmpty​(java.util.Iterator<? extends T> iter)  
      static boolean isPositive​(java.time.Duration d)
      Check if a duration is positive
      static <T> java.util.Iterator<T> iteratorOf​(java.lang.Iterable<T> iterable)
      Resolves to an always non-null iterator
      static <T> java.util.Iterator<T> iteratorOf​(java.util.Iterator<T> iter)
      Resolves to an always non-null iterator
      static java.lang.String join​(java.lang.Iterable<?> iter, char ch)  
      static java.lang.String join​(java.lang.Iterable<?> iter, java.lang.CharSequence sep)  
      static java.lang.String join​(java.util.Iterator<?> iter, char ch)  
      static java.lang.String join​(java.util.Iterator<?> iter, java.lang.CharSequence sep)  
      static <T> java.lang.String join​(T[] values, char ch)  
      static <T> java.lang.String join​(T[] values, java.lang.CharSequence sep)  
      static int lastIndexOf​(java.lang.CharSequence cs, char c)  
      static int length​(char[] chars)  
      static int length​(java.lang.CharSequence cs)  
      static <T> int length​(T... a)  
      static <T,​U>
      java.util.List<U>
      map​(java.util.Collection<? extends T> values, java.util.function.Function<? super T,​? extends U> mapper)  
      static <T,​U>
      java.util.NavigableSet<U>
      mapSort​(java.util.Collection<? extends T> values, java.util.function.Function<? super T,​? extends U> mapper, java.util.Comparator<? super U> comparator)  
      static <T> java.util.function.Supplier<T> memoizeLock​(java.util.function.Supplier<? extends T> delegate)
      The delegate Suppliers get() method is called exactly once and the result is cached.
      static <T> java.lang.Iterable<T> multiIterableSuppliers​(java.lang.Iterable<? extends java.util.function.Supplier<? extends java.lang.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
      static <E extends java.lang.Enum<E>>
      java.util.Set<E>
      of​(E... values)  
      static <E extends java.lang.Enum<E>>
      java.util.Set<E>
      of​(java.util.Collection<? extends E> values)  
      static java.lang.String replace​(java.lang.String text, java.lang.String repl, java.lang.String with, int max)
      Replace a String with another String inside a larger String, for the first max values of the search String.
      static java.lang.String replaceWhitespaceAndTrim​(java.lang.String s)  
      static int safeCompare​(java.lang.String s1, java.lang.String s2, boolean caseSensitive)  
      static <T> java.util.List<T> selectMatchingMembers​(java.util.function.Predicate<? super T> acceptor, java.util.Collection<? extends T> values)
      Returns a list of all the values that were accepted by a predicate
      static <T> java.util.List<T> selectMatchingMembers​(java.util.function.Predicate<? super T> acceptor, T... values)
      Returns a list of all the values that were accepted by a predicate
      static <T> T selectNextMatchingValue​(java.util.Iterator<?> values, java.lang.Class<T> type)  
      static int size​(java.util.Collection<?> c)  
      static java.lang.String[] split​(java.lang.String s, char ch)  
      static <T> java.util.stream.Stream<T> stream​(java.lang.Iterable<T> values)  
      static java.lang.CharSequence stripDelimiters​(java.lang.CharSequence s, char delim)  
      static java.lang.CharSequence stripQuotes​(java.lang.CharSequence s)  
      static <T> java.util.function.Supplier<T> supplierOf​(T value)
      Wraps a value into a Supplier
      static <T> java.util.stream.Collector<T,​?,​java.util.NavigableSet<T>> toSortedSet​(java.util.Comparator<? super T> comparator)  
      static java.lang.String trimToEmpty​(java.lang.String s)  
      static <T> java.util.List<T> unmodifiableList​(java.util.Collection<? extends T> values)  
      static <T> java.util.List<T> unmodifiableList​(java.util.stream.Stream<T> values)  
      static <T> java.util.List<T> unmodifiableList​(T... values)  
      static <U,​V>
      java.lang.Iterable<V>
      wrapIterable​(java.lang.Iterable<? extends U> iter, java.util.function.Function<? super U,​? extends V> mapper)  
      static <U,​V>
      java.util.Iterator<V>
      wrapIterator​(java.lang.Iterable<? extends U> iter, java.util.function.Function<? super U,​? extends V> mapper)  
      static <U,​V>
      java.util.Iterator<V>
      wrapIterator​(java.util.Iterator<? extends U> iter, java.util.function.Function<? super U,​? extends V> mapper)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 java.lang.String[] EMPTY_STRING_ARRAY
      • EMPTY_OBJECT_ARRAY

        public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
      • EMPTY_BOOLEAN_ARRAY

        public static final boolean[] EMPTY_BOOLEAN_ARRAY
      • NULL

        public static final java.lang.Object NULL
        A value indicating a null value - to be used as a placeholder where nulls are not allowed
      • CASE_SENSITIVE_ORDER

        public static final java.util.Comparator<java.lang.String> CASE_SENSITIVE_ORDER
        The complement of String.CASE_INSENSITIVE_ORDER
    • Constructor Detail

      • GenericUtils

        private GenericUtils()
    • Method Detail

      • trimToEmpty

        public static java.lang.String trimToEmpty​(java.lang.String s)
      • replaceWhitespaceAndTrim

        public static java.lang.String replaceWhitespaceAndTrim​(java.lang.String s)
      • replace

        public static java.lang.String replace​(java.lang.String text,
                                               java.lang.String repl,
                                               java.lang.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​(java.lang.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​(java.lang.String s,
                                   java.lang.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:
        • null - no conversion
        • Boolean.TRUE - get hash code of uppercase
        • Boolean.FALSE - get hash code of lowercase
        Returns:
        The calculated hash code
      • safeCompare

        public static int safeCompare​(java.lang.String s1,
                                      java.lang.String s2,
                                      boolean caseSensitive)
      • length

        public static int length​(java.lang.CharSequence cs)
      • isEmpty

        public static boolean isEmpty​(java.lang.CharSequence cs)
      • isNotEmpty

        public static boolean isNotEmpty​(java.lang.CharSequence cs)
      • isBlank

        public static boolean isBlank​(java.lang.CharSequence cs)

        Checks if a CharSequence is empty (""), null or whitespace only.

        Whitespace is defined by Character.isWhitespace(char).

         GenericUtils.isBlank(null)      = true
         GenericUtils.isBlank("")        = true
         GenericUtils.isBlank(" ")       = true
         GenericUtils.isBlank("bob")     = false
         GenericUtils.isBlank("  bob  ") = false
         
        Parameters:
        cs - the CharSequence to check, may be null
        Returns:
        true if the CharSequence is null, empty or whitespace only
      • isNotBlank

        public static boolean isNotBlank​(java.lang.CharSequence cs)
      • filterToNotBlank

        public static java.util.List<java.lang.String> filterToNotBlank​(java.util.List<java.lang.String> values)
      • indexOf

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

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

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

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

        public static java.lang.String join​(java.lang.Iterable<?> iter,
                                            char ch)
      • join

        public static java.lang.String join​(java.util.Iterator<?> iter,
                                            char ch)
      • join

        public static <T> java.lang.String join​(T[] values,
                                                java.lang.CharSequence sep)
      • join

        public static java.lang.String join​(java.lang.Iterable<?> iter,
                                            java.lang.CharSequence sep)
      • join

        public static java.lang.String join​(java.util.Iterator<?> iter,
                                            java.lang.CharSequence sep)
      • size

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

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

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

        public static <T> boolean equals​(java.util.Collection<T> c1,
                                         java.util.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​(java.lang.Iterable<? extends T> iter)
      • isNotEmpty

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

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

        public static <T> boolean isNotEmpty​(java.util.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

        @SafeVarargs
        public static <E extends java.lang.Enum<E>> java.util.Set<E> of​(E... values)
      • of

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

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

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

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

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

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

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

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

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

        public static <T,​U> java.util.List<U> map​(java.util.Collection<? extends T> values,
                                                        java.util.function.Function<? super T,​? extends U> mapper)
      • mapSort

        public static <T,​U> java.util.NavigableSet<U> mapSort​(java.util.Collection<? extends T> values,
                                                                    java.util.function.Function<? super T,​? extends U> mapper,
                                                                    java.util.Comparator<? super U> comparator)
      • toSortedSet

        public static <T> java.util.stream.Collector<T,​?,​java.util.NavigableSet<T>> toSortedSet​(java.util.Comparator<? super T> comparator)
      • stream

        public static <T> java.util.stream.Stream<T> stream​(java.lang.Iterable<T> values)
      • unmodifiableList

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

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

        public static <T> java.util.List<T> unmodifiableList​(java.util.stream.Stream<T> values)
      • asList

        @SafeVarargs
        public static <T> java.util.List<T> asList​(T... values)
      • asSet

        @SafeVarargs
        public static <T> java.util.Set<T> asSet​(T... values)
      • asSortedSet

        @SafeVarargs
        public static <V extends java.lang.Comparable<V>> java.util.NavigableSet<V> asSortedSet​(V... values)
      • asSortedSet

        public static <V extends java.lang.Comparable<V>> java.util.NavigableSet<V> asSortedSet​(java.util.Collection<? extends V> values)
      • asSortedSet

        @SafeVarargs
        public static <V> java.util.NavigableSet<V> asSortedSet​(java.util.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> java.util.NavigableSet<V> asSortedSet​(java.util.Comparator<? super V> comp,
                                                                java.util.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​(java.util.function.Predicate<? super T> acceptor,
                                                    T... values)
      • findFirstMatchingMember

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

        @SafeVarargs
        public static <T> java.util.List<T> selectMatchingMembers​(java.util.function.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> java.util.List<T> selectMatchingMembers​(java.util.function.Predicate<? super T> acceptor,
                                                                  java.util.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
      • stripQuotes

        public static java.lang.CharSequence stripQuotes​(java.lang.CharSequence s)
        Parameters:
        s - The CharSequence to be checked
        Returns:
        If the sequence contains any of the QUOTES on both ends, then they are stripped, otherwise nothing is done
        See Also:
        stripDelimiters(CharSequence, char)
      • stripDelimiters

        public static java.lang.CharSequence stripDelimiters​(java.lang.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> java.util.function.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> java.util.Iterator<T> iteratorOf​(java.lang.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> java.util.function.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​(java.lang.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> java.util.Iterator<T> iteratorOf​(java.util.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> java.lang.Iterable<V> wrapIterable​(java.lang.Iterable<? extends U> iter,
                                                                     java.util.function.Function<? super U,​? extends V> mapper)
      • wrapIterator

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

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

        public static <T> T selectNextMatchingValue​(java.util.Iterator<?> values,
                                                    java.lang.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> java.lang.Iterable<T> multiIterableSuppliers​(java.lang.Iterable<? extends java.util.function.Supplier<? extends java.lang.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> java.util.function.Supplier<T> memoizeLock​(java.util.function.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​(java.time.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​(java.time.Duration d)
        Check if a duration is negative or zero
        Parameters:
        d - the duration
        Returns:
        true if the duration is negative or zero