public final class GenericUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
GenericUtils.MapBuilder<K,V> |
Modifier and Type | Field and Description |
---|---|
private static java.util.function.Supplier |
CASE_INSENSITIVE_MAP_FACTORY |
static java.util.Comparator<java.lang.String> |
CASE_SENSITIVE_ORDER
The complement of
String.CASE_INSENSITIVE_ORDER |
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 extends java.lang.Throwable> |
accumulateException(T current,
T extra)
Used to "accumulate" exceptions of the same type.
|
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 <V> java.util.function.Supplier<java.util.NavigableMap<java.lang.String,V>> |
caseInsensitiveMap() |
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> 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 <K,V> java.util.Map<V,K> |
flipMap(java.util.Map<? extends K,? extends V> map,
java.util.function.Supplier<? extends java.util.Map<V,K>> mapCreator,
boolean allowDuplicates) |
static <T> void |
forEach(java.lang.Iterable<T> values,
java.util.function.Consumer<T> consumer) |
static int |
hashCode(java.lang.String s) |
static int |
hashCode(java.lang.String s,
java.lang.Boolean useUppercase) |
static int |
indexOf(java.lang.CharSequence cs,
char c) |
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 boolean |
isEmpty(java.util.Map<?,?> m) |
static <T> boolean |
isEmpty(T... a) |
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 |
isNotEmpty(java.util.Map<?,?> m) |
static <T> boolean |
isSameReference(T o1,
T o2) |
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<T> values,
java.util.function.Function<? super T,? extends U> mapper) |
static <K,V> GenericUtils.MapBuilder<K,V> |
mapBuilder() |
static <K,V> GenericUtils.MapBuilder<K,V> |
mapBuilder(java.util.Comparator<K> comparator) |
static <T,U> java.util.NavigableSet<U> |
mapSort(java.util.Collection<T> values,
java.util.function.Function<? super T,? extends U> mapper,
java.util.Comparator<U> comparator) |
static <K,V> java.util.Map<K,V> |
mapValues(java.util.function.Function<? super V,? extends K> keyMapper,
java.util.function.Supplier<? extends java.util.Map<K,V>> mapCreator,
java.util.Collection<V> values)
Creates a map out of a group of values
|
static <K,V> java.util.Map<K,V> |
mapValues(java.util.function.Function<? super V,? extends K> keyMapper,
java.util.function.Supplier<? extends java.util.Map<K,V>> mapCreator,
V... values) |
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.Throwable |
peelException(java.lang.Throwable t)
Attempts to get to the "effective" exception being thrown,
by taking care of some known exceptions that wrap the original thrown
one.
|
static java.lang.String |
replaceWhitespaceAndTrim(java.lang.String s) |
static java.lang.Throwable |
resolveExceptionCause(java.lang.Throwable t) |
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 int |
size(java.util.Collection<?> c) |
static int |
size(java.util.Map<?,?> m) |
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 |
private static <T> java.util.function.BinaryOperator<T> |
throwingMerger() |
static java.io.IOException |
toIOException(java.lang.Throwable e) |
static java.lang.RuntimeException |
toRuntimeException(java.lang.Throwable t) |
static java.lang.RuntimeException |
toRuntimeException(java.lang.Throwable t,
boolean peelThrowable)
Converts a thrown generic exception to a
RuntimeException |
static <T,K,U> java.util.stream.Collector<T,?,java.util.NavigableMap<K,U>> |
toSortedMap(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper,
java.util.Comparator<K> comparator) |
static <T,K,U> java.util.NavigableMap<K,U> |
toSortedMap(java.lang.Iterable<T> values,
java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends U> valueMapper,
java.util.Comparator<K> comparator) |
static <T> java.util.stream.Collector<T,?,java.util.NavigableSet<T>> |
toSortedSet(java.util.Comparator<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 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
private static final java.util.function.Supplier CASE_INSENSITIVE_MAP_FACTORY
public static java.lang.String trimToEmpty(java.lang.String s)
public static java.lang.String replaceWhitespaceAndTrim(java.lang.String s)
public 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 <T> boolean isSameReference(T o1, T o2)
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 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 int size(java.util.Map<?,?> m)
public static boolean isEmpty(java.util.Map<?,?> m)
public static boolean isNotEmpty(java.util.Map<?,?> m)
@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)
@SafeVarargs 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> boolean containsAny(java.util.Collection<? extends T> coll, java.lang.Iterable<? extends T> values)
public static <T> void forEach(java.lang.Iterable<T> values, java.util.function.Consumer<T> consumer)
public static <T,U> java.util.List<U> map(java.util.Collection<T> values, java.util.function.Function<? super T,? extends U> mapper)
public static <T,U> java.util.NavigableSet<U> mapSort(java.util.Collection<T> values, java.util.function.Function<? super T,? extends U> mapper, java.util.Comparator<U> comparator)
public static <T,K,U> java.util.NavigableMap<K,U> toSortedMap(java.lang.Iterable<T> values, java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper, java.util.Comparator<K> comparator)
public static <T,K,U> java.util.stream.Collector<T,?,java.util.NavigableMap<K,U>> toSortedMap(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends U> valueMapper, java.util.Comparator<K> comparator)
private static <T> java.util.function.BinaryOperator<T> throwingMerger()
public static <T> java.util.stream.Collector<T,?,java.util.NavigableSet<T>> toSortedSet(java.util.Comparator<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 comparatorpublic static <V> java.util.function.Supplier<java.util.NavigableMap<java.lang.String,V>> caseInsensitiveMap()
V
- Type of mapped valueSupplier
that returns a new NavigableMap
whenever its get()
method is invokedpublic static <K,V> java.util.Map<V,K> flipMap(java.util.Map<? extends K,? extends V> map, java.util.function.Supplier<? extends java.util.Map<V,K>> mapCreator, boolean allowDuplicates)
@SafeVarargs public static <K,V> java.util.Map<K,V> mapValues(java.util.function.Function<? super V,? extends K> keyMapper, java.util.function.Supplier<? extends java.util.Map<K,V>> mapCreator, V... values)
public static <K,V> java.util.Map<K,V> mapValues(java.util.function.Function<? super V,? extends K> keyMapper, java.util.function.Supplier<? extends java.util.Map<K,V>> mapCreator, java.util.Collection<V> values)
K
- The key typeV
- The value typekeyMapper
- The Function
that generates a key for a given value.
If the returned key is null
then the value is not mappedmapCreator
- The Supplier
used to create/retrieve the result map - provided
non-empty group of valuesvalues
- The values to be mappedMap
- Note: no validation is made to ensure
that 2 (or more) values are not mapped to the same key@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 java.lang.RuntimeException toRuntimeException(java.lang.Throwable t)
public static java.lang.RuntimeException toRuntimeException(java.lang.Throwable t, boolean peelThrowable)
RuntimeException
t
- The original thrown exceptionpeelThrowable
- Whether to determine the root cause by "peeling"
any enclosing exceptionspeelException(Throwable)
public static java.lang.Throwable peelException(java.lang.Throwable t)
t
- The original Throwable
- ignored if null
public static java.lang.Throwable resolveExceptionCause(java.lang.Throwable t)
t
- The original Throwable
- ignored if null
Throwable.getCause()
is non-null
then
the cause, otherwise the original exception - null
if
the original exception was null
public static <T extends java.lang.Throwable> T accumulateException(T current, T extra)
null
then the new one becomes the current,
otherwise the new one is added as a suppressed exception to the
current oneT
- The exception typecurrent
- The current exceptionextra
- The extra/new exceptionThrowable.addSuppressed(Throwable)
public static java.io.IOException toIOException(java.lang.Throwable e)
public 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> 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> 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 <K,V> GenericUtils.MapBuilder<K,V> mapBuilder()
public static <K,V> GenericUtils.MapBuilder<K,V> mapBuilder(java.util.Comparator<K> comparator)