public final class GenericUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
GenericUtils.MapBuilder<K,V> |
| Modifier and Type | Field and Description |
|---|---|
static Comparator<String> |
CASE_SENSITIVE_ORDER
The complement of
String.CASE_INSENSITIVE_ORDER |
static byte[] |
EMPTY_BYTE_ARRAY |
static char[] |
EMPTY_CHAR_ARRAY |
static Object[] |
EMPTY_OBJECT_ARRAY |
static String[] |
EMPTY_STRING_ARRAY |
static Object |
NULL
A value indicating a
null value - to be used as a placeholder
where nulls are not allowed |
static String |
QUOTES |
| Modifier and Type | Method and Description |
|---|---|
static <T extends Throwable> |
accumulateException(T current,
T extra)
Used to "accumulate" exceptions of the same type.
|
static <T> List<T> |
asList(T... values) |
static <T> Set<T> |
asSet(T... values) |
static <V extends Comparable<V>> |
asSortedSet(Collection<? extends V> values) |
static <V> SortedSet<V> |
asSortedSet(Comparator<? super V> comp,
Collection<? extends V> values) |
static <V> SortedSet<V> |
asSortedSet(Comparator<? super V> comp,
V... values) |
static <V extends Comparable<V>> |
asSortedSet(V... values) |
static <V> Supplier<SortedMap<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(Collection<? extends T> coll,
Iterable<? extends T> values) |
static <T> T |
findFirstMatchingMember(Predicate<? super T> acceptor,
Collection<? extends T> values) |
static <T> T |
findFirstMatchingMember(Predicate<? super T> acceptor,
T... values) |
static <K,V> Map<V,K> |
flipMap(Map<? extends K,? extends V> map,
Supplier<? extends Map<V,K>> mapCreator,
boolean allowDuplicates) |
static <T> void |
forEach(Iterable<T> values,
Consumer<T> consumer) |
static int |
hashCode(String s) |
static int |
hashCode(String s,
Boolean useUppercase) |
static int |
indexOf(CharSequence cs,
char c) |
static boolean |
isEmpty(char[] chars) |
static boolean |
isEmpty(CharSequence cs) |
static boolean |
isEmpty(Collection<?> c) |
static <T> boolean |
isEmpty(Iterable<? extends T> iter) |
static <T> boolean |
isEmpty(Iterator<? extends T> iter) |
static boolean |
isEmpty(Map<?,?> m) |
static <T> boolean |
isEmpty(T... a) |
static boolean |
isNotEmpty(CharSequence cs) |
static boolean |
isNotEmpty(Collection<?> c) |
static <T> boolean |
isNotEmpty(Iterable<? extends T> iter) |
static <T> boolean |
isNotEmpty(Iterator<? extends T> iter) |
static boolean |
isNotEmpty(Map<?,?> m) |
static <T> boolean |
isSameReference(T o1,
T o2) |
static <T> Iterator<T> |
iteratorOf(Iterable<T> iterable)
Resolves to an always non-
null iterator |
static <T> Iterator<T> |
iteratorOf(Iterator<T> iter)
Resolves to an always non-
null iterator |
static String |
join(Iterable<?> iter,
char ch) |
static String |
join(Iterable<?> iter,
CharSequence sep) |
static String |
join(Iterator<?> iter,
char ch) |
static String |
join(Iterator<?> iter,
CharSequence sep) |
static <T> String |
join(T[] values,
char ch) |
static <T> String |
join(T[] values,
CharSequence sep) |
static int |
lastIndexOf(CharSequence cs,
char c) |
static int |
length(char[] chars) |
static int |
length(CharSequence cs) |
static <T> int |
length(T... a) |
static <T,U> List<U> |
map(Collection<T> values,
Function<? super T,? extends U> mapper) |
static <K,V> GenericUtils.MapBuilder<K,V> |
mapBuilder() |
static <K,V> GenericUtils.MapBuilder<K,V> |
mapBuilder(Comparator<K> comparator) |
static <T,U> SortedSet<U> |
mapSort(Collection<T> values,
Function<? super T,? extends U> mapper,
Comparator<U> comparator) |
static <K,V> Map<K,V> |
mapValues(Function<? super V,? extends K> keyMapper,
Supplier<? extends Map<K,V>> mapCreator,
Collection<V> values)
Creates a map out of a group of values
|
static <K,V> Map<K,V> |
mapValues(Function<? super V,? extends K> keyMapper,
Supplier<? extends Map<K,V>> mapCreator,
V... values) |
static <T> Iterable<T> |
multiIterableSuppliers(Iterable<? extends Supplier<? extends Iterable<? extends T>>> providers)
|
static <E extends Enum<E>> |
of(Collection<? extends E> values) |
static <E extends Enum<E>> |
of(E... values) |
static Throwable |
peelException(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 String |
replaceWhitespaceAndTrim(String s) |
static Throwable |
resolveExceptionCause(Throwable t) |
static int |
safeCompare(String s1,
String s2,
boolean caseSensitive) |
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
|
static <T> List<T> |
selectMatchingMembers(Predicate<? super T> acceptor,
T... values)
Returns a list of all the values that were accepted by a predicate
|
static int |
size(Collection<?> c) |
static int |
size(Map<?,?> m) |
static String[] |
split(String s,
char ch) |
static <T> Stream<T> |
stream(Iterable<T> values) |
static CharSequence |
stripDelimiters(CharSequence s,
char delim) |
static CharSequence |
stripQuotes(CharSequence s) |
static <T> Supplier<T> |
supplierOf(T value)
Wraps a value into a
Supplier |
static IOException |
toIOException(Throwable e) |
static RuntimeException |
toRuntimeException(Throwable t) |
static RuntimeException |
toRuntimeException(Throwable t,
boolean peelThrowable)
Converts a thrown generic exception to a
RuntimeException |
static <T,K,U> Collector<T,?,SortedMap<K,U>> |
toSortedMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
Comparator<K> comparator) |
static <T,K,U> SortedMap<K,U> |
toSortedMap(Iterable<T> values,
Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
Comparator<K> comparator) |
static <T> Collector<T,?,SortedSet<T>> |
toSortedSet(Comparator<T> comparator) |
static String |
trimToEmpty(String s) |
static <T> List<T> |
unmodifiableList(Collection<? extends T> values) |
static <T> List<T> |
unmodifiableList(Stream<T> values) |
static <T> List<T> |
unmodifiableList(T... values) |
static <U,V> Iterable<V> |
wrapIterable(Iterable<? extends U> iter,
Function<U,V> mapper) |
static <U,V> Iterator<V> |
wrapIterator(Iterable<? extends U> iter,
Function<U,V> mapper) |
static <U,V> Iterator<V> |
wrapIterator(Iterator<? extends U> iter,
Function<U,V> mapper) |
public static final byte[] EMPTY_BYTE_ARRAY
public static final char[] EMPTY_CHAR_ARRAY
public static final String[] EMPTY_STRING_ARRAY
public static final Object[] EMPTY_OBJECT_ARRAY
public static final Object NULL
null value - to be used as a placeholder
where nulls are not allowedpublic static final Comparator<String> CASE_SENSITIVE_ORDER
String.CASE_INSENSITIVE_ORDERpublic static final String QUOTES
public static int hashCode(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(String s, 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 <T> boolean isSameReference(T o1,
T o2)
public static int length(CharSequence cs)
public static boolean isEmpty(CharSequence cs)
public static boolean isNotEmpty(CharSequence cs)
public static int indexOf(CharSequence cs, char c)
public static int lastIndexOf(CharSequence cs, char c)
public static <T> String join(T[] values, char ch)
public static <T> String join(T[] values, CharSequence sep)
public static String join(Iterable<?> iter, CharSequence sep)
public static String join(Iterator<?> iter, CharSequence sep)
public static int size(Collection<?> c)
public static boolean isEmpty(Collection<?> c)
public static boolean isNotEmpty(Collection<?> c)
public static int size(Map<?,?> m)
public static boolean isEmpty(Map<?,?> m)
public static boolean isNotEmpty(Map<?,?> m)
@SafeVarargs public static <T> int length(T... a)
public static <T> boolean isEmpty(Iterable<? extends T> iter)
public static <T> boolean isNotEmpty(Iterable<? extends T> iter)
public static <T> boolean isEmpty(Iterator<? extends T> iter)
public static <T> boolean isNotEmpty(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 Enum<E>> Set<E> of(E... values)
public static <E extends Enum<E>> Set<E> of(Collection<? extends E> values)
public static <T> boolean containsAny(Collection<? extends T> coll, Iterable<? extends T> values)
public static <T,U> List<U> map(Collection<T> values, Function<? super T,? extends U> mapper)
public static <T,U> SortedSet<U> mapSort(Collection<T> values, Function<? super T,? extends U> mapper, Comparator<U> comparator)
public static <T,K,U> SortedMap<K,U> toSortedMap(Iterable<T> values, Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, Comparator<K> comparator)
public static <T,K,U> Collector<T,?,SortedMap<K,U>> toSortedMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, Comparator<K> comparator)
public static <T> Collector<T,?,SortedSet<T>> toSortedSet(Comparator<T> comparator)
@SafeVarargs public static <T> List<T> unmodifiableList(T... values)
public static <T> List<T> unmodifiableList(Collection<? extends T> values)
@SafeVarargs public static <T> List<T> asList(T... values)
@SafeVarargs public static <T> Set<T> asSet(T... values)
@SafeVarargs public static <V extends Comparable<V>> SortedSet<V> asSortedSet(V... values)
public static <V extends Comparable<V>> SortedSet<V> asSortedSet(Collection<? extends V> values)
@SafeVarargs public static <V> SortedSet<V> asSortedSet(Comparator<? super V> comp, V... values)
V - The element typecomp - The (non-null) Comparator to usevalues - The values to be added (ignored if null)SortedSet containing the values (if any) sorted
using the provided comparatorpublic static <V> SortedSet<V> asSortedSet(Comparator<? super V> comp, Collection<? extends V> values)
V - The element typecomp - The (non-null) Comparator to usevalues - The values to be added (ignored if null/empty)SortedSet containing the values (if any) sorted
using the provided comparatorpublic static <K,V> Map<V,K> flipMap(Map<? extends K,? extends V> map, Supplier<? extends Map<V,K>> mapCreator, boolean allowDuplicates)
@SafeVarargs public static <K,V> Map<K,V> mapValues(Function<? super V,? extends K> keyMapper, Supplier<? extends Map<K,V>> mapCreator, V... values)
public static <K,V> Map<K,V> mapValues(Function<? super V,? extends K> keyMapper, Supplier<? extends Map<K,V>> mapCreator, 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(Predicate<? super T> acceptor, T... values)
public static <T> T findFirstMatchingMember(Predicate<? super T> acceptor, Collection<? extends T> values)
@SafeVarargs public static <T> List<T> selectMatchingMembers(Predicate<? super T> acceptor, T... values)
public static <T> List<T> selectMatchingMembers(Predicate<? super T> acceptor, Collection<? extends T> values)
public static CharSequence stripQuotes(CharSequence s)
s - The CharSequence to be checkedQUOTES
on both ends, then they are stripped, otherwise
nothing is donestripDelimiters(CharSequence, char)public static CharSequence stripDelimiters(CharSequence s, char delim)
s - The CharSequence to be checkeddelim - The expected delimiterpublic static RuntimeException toRuntimeException(Throwable t)
public static RuntimeException toRuntimeException(Throwable t, boolean peelThrowable)
RuntimeExceptiont - The original thrown exceptionpeelThrowable - Whether to determine the root cause by "peeling"
any enclosing exceptionspeelException(Throwable)public static Throwable peelException(Throwable t)
t - The original Throwable - ignored if nullpublic static Throwable resolveExceptionCause(Throwable t)
t - The original Throwable - ignored if nullThrowable.getCause() is non-null then
the cause, otherwise the original exception - null if
the original exception was nullpublic static <T extends 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 IOException toIOException(Throwable e)
public static <T> Supplier<T> supplierOf(T value)
SupplierT - Type of value being suppliedvalue - The value to be suppliedpublic static <T> Iterator<T> iteratorOf(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 <T> Iterator<T> iteratorOf(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> Iterable<V> wrapIterable(Iterable<? extends U> iter, Function<U,V> mapper)
public static <U,V> Iterator<V> wrapIterator(Iterable<? extends U> iter, Function<U,V> mapper)
public static <U,V> Iterator<V> wrapIterator(Iterator<? extends U> iter, Function<U,V> mapper)
public static <T> Iterable<T> multiIterableSuppliers(Iterable<? extends Supplier<? extends Iterable<? extends T>>> providers)
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 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(Comparator<K> comparator)
Copyright © 2008–2017 The Apache Software Foundation. All rights reserved.