public final class GenericUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.util.Comparator<java.lang.String> |
CASE_SENSITIVE_ORDER
The complement of
String.CASE_INSENSITIVE_ORDER |
static boolean[] |
EMPTY_BOOLEAN_ARRAY |
static byte[] |
EMPTY_BYTE_ARRAY |
static char[] |
EMPTY_CHAR_ARRAY |
static java.lang.Object[] |
EMPTY_OBJECT_ARRAY |
static java.lang.String[] |
EMPTY_STRING_ARRAY |
static java.lang.Object |
NULL
A value indicating a
null value - to be used as a placeholder where null s are not allowed |
static java.lang.String |
QUOTES |
Modifier | Constructor and Description |
---|---|
private |
GenericUtils() |
Modifier and Type | Method and 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>> |
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>> |
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> |
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
SortedSet s. |
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 boolean |
isEmpty(java.util.Collection<?> c) |
static <T> boolean |
isEmpty(java.lang.Iterable<? extends T> iter) |
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 boolean |
isNotEmpty(java.util.Collection<?> c) |
static <T> boolean |
isNotEmpty(java.lang.Iterable<? extends T> iter) |
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
Supplier s 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>> |
of(java.util.Collection<? extends E> values) |
static <E extends java.lang.Enum<E>> |
of(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) |
public static final byte[] EMPTY_BYTE_ARRAY
public static final char[] EMPTY_CHAR_ARRAY
public static final java.lang.String[] EMPTY_STRING_ARRAY
public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
public static final boolean[] EMPTY_BOOLEAN_ARRAY
public static final java.lang.Object NULL
null
value - to be used as a placeholder where null
s are not allowedpublic static final java.util.Comparator<java.lang.String> CASE_SENSITIVE_ORDER
String.CASE_INSENSITIVE_ORDER
public static final java.lang.String QUOTES
public static java.lang.String trimToEmpty(java.lang.String s)
public static java.lang.String replaceWhitespaceAndTrim(java.lang.String s)
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.
text
- text to search and replace inrepl
- String to search forwith
- String to replace withmax
- maximum number of values to replace, or -1
if no maximumpublic static int hashCode(java.lang.String s)
s
- The String
value to calculate the hash code on - may be null
/empty in which case a
value of zero is returnedhashCode(String, Boolean)
public static int hashCode(java.lang.String s, java.lang.Boolean useUppercase)
s
- The String
value to calculate the hash code on - may be null
/empty in which
case a value of zero is returneduseUppercase
- Whether to convert the string to uppercase, lowercase or not at all:
null
- no conversionBoolean.TRUE
- get hash code of uppercaseBoolean.FALSE
- get hash code of lowercasepublic static int safeCompare(java.lang.String s1, java.lang.String s2, boolean caseSensitive)
public static int length(java.lang.CharSequence cs)
public static boolean isEmpty(java.lang.CharSequence cs)
public static boolean isNotEmpty(java.lang.CharSequence cs)
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
cs
- the CharSequence to check, may be nulltrue
if the CharSequence is null, empty or whitespace onlypublic static boolean isNotBlank(java.lang.CharSequence cs)
public static java.util.List<java.lang.String> filterToNotBlank(java.util.List<java.lang.String> values)
public static int indexOf(java.lang.CharSequence cs, char c)
public static int lastIndexOf(java.lang.CharSequence cs, char c)
public static java.lang.String[] split(java.lang.String s, char ch)
public static <T> java.lang.String join(T[] values, char ch)
public static java.lang.String join(java.lang.Iterable<?> iter, char ch)
public static java.lang.String join(java.util.Iterator<?> iter, char ch)
public static <T> java.lang.String join(T[] values, java.lang.CharSequence sep)
public static java.lang.String join(java.lang.Iterable<?> iter, java.lang.CharSequence sep)
public static java.lang.String join(java.util.Iterator<?> iter, java.lang.CharSequence sep)
public static int size(java.util.Collection<?> c)
public static boolean isEmpty(java.util.Collection<?> c)
public static boolean isNotEmpty(java.util.Collection<?> c)
public static <T> boolean equals(java.util.Collection<T> c1, java.util.Collection<T> c2)
T
- Generic element typec1
- First collectionc2
- Second collectiontrue
if the following holds:
null
collections are consider equal to empty ones@SafeVarargs public static <T> int length(T... a)
public static <T> boolean isEmpty(java.lang.Iterable<? extends T> iter)
public static <T> boolean isNotEmpty(java.lang.Iterable<? extends T> iter)
public static <T> boolean isEmpty(java.util.Iterator<? extends T> iter)
public static <T> boolean isNotEmpty(java.util.Iterator<? extends T> iter)
public static <T> boolean isEmpty(T[] a)
public static int length(char[] chars)
public static boolean isEmpty(char[] chars)
public static int compare(char[] c1, char[] c2)
null
and empty are considered equalc1
- 1st arrayc2
- 2nd array@SafeVarargs public static <E extends java.lang.Enum<E>> java.util.Set<E> of(E... values)
public static <E extends java.lang.Enum<E>> java.util.Set<E> of(java.util.Collection<? extends E> values)
public static <T> int findFirstDifferentValueIndex(java.util.List<? extends T> c1, java.util.List<? extends T> c2)
public static <T> int findFirstDifferentValueIndex(java.util.List<? extends T> c1, java.util.List<? extends T> c2, UnaryEquator<? super T> equator)
public static <T> int findFirstDifferentValueIndex(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2)
public static <T> int findFirstDifferentValueIndex(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, UnaryEquator<? super T> equator)
public static <T> int findFirstDifferentValueIndex(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2)
public static <T> int findFirstDifferentValueIndex(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, UnaryEquator<? super T> equator)
public static <T> boolean containsAny(java.util.Collection<? extends T> coll, java.lang.Iterable<? extends T> values)
public static <T> void forEach(java.lang.Iterable<? extends T> values, java.util.function.Consumer<? super T> consumer)
public static <T,U> java.util.List<U> map(java.util.Collection<? extends T> values, java.util.function.Function<? super T,? extends U> mapper)
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)
public static <T> java.util.stream.Collector<T,?,java.util.NavigableSet<T>> toSortedSet(java.util.Comparator<? super T> comparator)
public static <T> java.util.stream.Stream<T> stream(java.lang.Iterable<T> values)
@SafeVarargs public static <T> java.util.List<T> unmodifiableList(T... values)
public static <T> java.util.List<T> unmodifiableList(java.util.Collection<? extends T> values)
public static <T> java.util.List<T> unmodifiableList(java.util.stream.Stream<T> values)
@SafeVarargs public static <T> java.util.List<T> asList(T... values)
@SafeVarargs public static <T> java.util.Set<T> asSet(T... values)
@SafeVarargs public static <V extends java.lang.Comparable<V>> java.util.NavigableSet<V> asSortedSet(V... values)
public static <V extends java.lang.Comparable<V>> java.util.NavigableSet<V> asSortedSet(java.util.Collection<? extends V> values)
@SafeVarargs public static <V> java.util.NavigableSet<V> asSortedSet(java.util.Comparator<? super V> comp, V... values)
V
- The element typecomp
- The (non-null
) Comparator
to usevalues
- The values to be added (ignored if null
)NavigableSet
containing the values (if any) sorted using the provided comparatorpublic static <V> java.util.NavigableSet<V> asSortedSet(java.util.Comparator<? super V> comp, java.util.Collection<? extends V> values)
V
- The element typecomp
- The (non-null
) Comparator
to usevalues
- The values to be added (ignored if null
/empty)NavigableSet
containing the values (if any) sorted using the provided comparator@SafeVarargs public static <T> T findFirstMatchingMember(java.util.function.Predicate<? super T> acceptor, T... values)
public static <T> T findFirstMatchingMember(java.util.function.Predicate<? super T> acceptor, java.util.Collection<? extends T> values)
@SafeVarargs public static <T> java.util.List<T> selectMatchingMembers(java.util.function.Predicate<? super T> acceptor, T... values)
T
- The type of value being evaluatedacceptor
- The Predicate
to consult whether a member is selectedvalues
- The values to be scannedList
of all the values that were accepted by the predicatepublic static <T> java.util.List<T> selectMatchingMembers(java.util.function.Predicate<? super T> acceptor, java.util.Collection<? extends T> values)
T
- The type of value being evaluatedacceptor
- The Predicate
to consult whether a member is selectedvalues
- The values to be scannedList
of all the values that were accepted by the predicatepublic static java.lang.CharSequence stripQuotes(java.lang.CharSequence s)
s
- The CharSequence
to be checkedQUOTES
on both ends, then they are stripped,
otherwise nothing is donestripDelimiters(CharSequence, char)
public static java.lang.CharSequence stripDelimiters(java.lang.CharSequence s, char delim)
s
- The CharSequence
to be checkeddelim
- The expected delimiterpublic static <T> java.util.function.Supplier<T> supplierOf(T value)
Supplier
T
- Type of value being suppliedvalue
- The value to be suppliedpublic static <T> java.util.Iterator<T> iteratorOf(java.lang.Iterable<T> iterable)
null
iteratorT
- Type of value being iteratediterable
- The Iterable
instancenull
iterator which may be empty if no iterable instance or no iterator returned
from ititeratorOf(Iterator)
public static <B,D extends B> java.util.function.Function<D,B> downcast()
B
- Generic base classD
- Generic child classFunction
that returns its input child class as a base classpublic static <T> T head(java.lang.Iterable<? extends T> it)
List
-s Deque
-s and
SortedSet
s.T
- Type of elementit
- The Iterable
instance - ignored if null
/emptynull
if none availablepublic static <T> java.util.Iterator<T> iteratorOf(java.util.Iterator<T> iter)
null
iteratorT
- Type of value being iteratediter
- The Iterator
instancenull
iterator which may be empty if no iterator instanceCollections.emptyIterator()
public static <U,V> java.lang.Iterable<V> wrapIterable(java.lang.Iterable<? extends U> iter, java.util.function.Function<? super U,? extends V> mapper)
public static <U,V> java.util.Iterator<V> wrapIterator(java.lang.Iterable<? extends U> iter, java.util.function.Function<? super U,? extends V> mapper)
public static <U,V> java.util.Iterator<V> wrapIterator(java.util.Iterator<? extends U> iter, java.util.function.Function<? super U,? extends V> mapper)
public static <T> T selectNextMatchingValue(java.util.Iterator<?> values, java.lang.Class<T> type)
T
- Generic return typevalues
- 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.null
if none foundpublic static <T> java.lang.Iterable<T> multiIterableSuppliers(java.lang.Iterable<? extends java.util.function.Supplier<? extends java.lang.Iterable<? extends T>>> providers)
Supplier
s 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 consultedT
- Type of value being iteratedproviders
- The providers - ignored if null
(i.e., return an empty iterable instance)public static <T> java.util.function.Supplier<T> memoizeLock(java.util.function.Supplier<? extends T> delegate)
T
- Generic type of supplied valuedelegate
- The actual Supplierpublic static boolean isPositive(java.time.Duration d)
d
- the durationtrue
if the duration is greater than zeropublic static boolean isNegativeOrNull(java.time.Duration d)
d
- the durationtrue
if the duration is negative or zero