public final class GenericUtils extends Object
Modifier and Type | Field and Description |
---|---|
static Comparator<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 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 null s are not allowed |
static String |
QUOTES |
Modifier and Type | Method and Description |
---|---|
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> NavigableSet<V> |
asSortedSet(Comparator<? super V> comp,
Collection<? extends V> values) |
static <V> NavigableSet<V> |
asSortedSet(Comparator<? super V> comp,
V... values) |
static <V extends 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(Collection<? extends T> coll,
Iterable<? extends T> values) |
static <B,D extends B> |
downcast() |
static <T> boolean |
equals(Collection<T> c1,
Collection<T> c2) |
static List<String> |
filterToNotBlank(List<String> values) |
static <T> int |
findFirstDifferentValueIndex(Iterable<? extends T> c1,
Iterable<? extends T> c2) |
static <T> int |
findFirstDifferentValueIndex(Iterable<? extends T> c1,
Iterable<? extends T> c2,
UnaryEquator<? super T> equator) |
static <T> int |
findFirstDifferentValueIndex(Iterator<? extends T> i1,
Iterator<? extends T> i2) |
static <T> int |
findFirstDifferentValueIndex(Iterator<? extends T> i1,
Iterator<? extends T> i2,
UnaryEquator<? super T> equator) |
static <T> int |
findFirstDifferentValueIndex(List<? extends T> c1,
List<? extends T> c2) |
static <T> int |
findFirstDifferentValueIndex(List<? extends T> c1,
List<? extends T> c2,
UnaryEquator<? super T> equator) |
static <T> T |
findFirstMatchingMember(Predicate<? super T> acceptor,
Collection<? extends T> values) |
static <T> T |
findFirstMatchingMember(Predicate<? super T> acceptor,
T... values) |
static <T> void |
forEach(Iterable<? extends T> values,
Consumer<? super T> consumer) |
static int |
hashCode(String s) |
static int |
hashCode(String s,
Boolean useUppercase) |
static <T> T |
head(Iterable<? extends T> it)
|
static int |
indexOf(CharSequence cs,
char c) |
static boolean |
isBlank(CharSequence cs)
Checks if a CharSequence is empty (""), null or whitespace only.
|
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 <T> boolean |
isEmpty(T[] a) |
static boolean |
isNegativeOrNull(Duration d)
Check if a duration is negative or zero
|
static boolean |
isNotBlank(CharSequence cs) |
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 |
isPositive(Duration d)
Check if a duration is positive
|
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<? extends T> values,
Function<? super T,? extends U> mapper) |
static <T,U> NavigableSet<U> |
mapSort(Collection<? extends T> values,
Function<? super T,? extends U> mapper,
Comparator<? super U> comparator) |
static <T> Supplier<T> |
memoizeLock(Supplier<? extends T> delegate)
The delegate Suppliers get() method is called exactly once and the result is cached.
|
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 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. |
static String |
replaceWhitespaceAndTrim(String s) |
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 <T> T |
selectNextMatchingValue(Iterator<?> values,
Class<T> type) |
static int |
size(Collection<?> c) |
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 <T> Collector<T,?,NavigableSet<T>> |
toSortedSet(Comparator<? super 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<? super U,? extends V> mapper) |
static <U,V> Iterator<V> |
wrapIterator(Iterable<? extends U> iter,
Function<? super U,? extends V> mapper) |
static <U,V> Iterator<V> |
wrapIterator(Iterator<? extends U> iter,
Function<? super U,? extends 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 boolean[] EMPTY_BOOLEAN_ARRAY
public static final Object NULL
null
value - to be used as a placeholder where null
s are not allowedpublic static final Comparator<String> CASE_SENSITIVE_ORDER
String.CASE_INSENSITIVE_ORDER
public static final String QUOTES
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.
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(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 int length(CharSequence cs)
public static boolean isEmpty(CharSequence cs)
public static boolean isNotEmpty(CharSequence cs)
public static boolean isBlank(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(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 <T> boolean equals(Collection<T> c1, 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(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)
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> int findFirstDifferentValueIndex(List<? extends T> c1, List<? extends T> c2)
public static <T> int findFirstDifferentValueIndex(List<? extends T> c1, List<? extends T> c2, UnaryEquator<? super T> equator)
public static <T> int findFirstDifferentValueIndex(Iterable<? extends T> c1, Iterable<? extends T> c2)
public static <T> int findFirstDifferentValueIndex(Iterable<? extends T> c1, Iterable<? extends T> c2, UnaryEquator<? super T> equator)
public static <T> int findFirstDifferentValueIndex(Iterator<? extends T> i1, Iterator<? extends T> i2)
public static <T> int findFirstDifferentValueIndex(Iterator<? extends T> i1, Iterator<? extends T> i2, UnaryEquator<? super T> equator)
public static <T> boolean containsAny(Collection<? extends T> coll, Iterable<? extends T> values)
public static <T,U> List<U> map(Collection<? extends T> values, Function<? super T,? extends U> mapper)
public static <T,U> NavigableSet<U> mapSort(Collection<? extends T> values, Function<? super T,? extends U> mapper, Comparator<? super U> comparator)
public static <T> Collector<T,?,NavigableSet<T>> toSortedSet(Comparator<? super 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>> NavigableSet<V> asSortedSet(V... values)
public static <V extends Comparable<V>> NavigableSet<V> asSortedSet(Collection<? extends V> values)
@SafeVarargs public static <V> NavigableSet<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
)NavigableSet
containing the values (if any) sorted using the provided comparatorpublic static <V> NavigableSet<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)NavigableSet
containing the values (if any) sorted using the provided comparator@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 <T> Supplier<T> supplierOf(T value)
Supplier
T
- 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 <B,D extends B> 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(Iterable<? extends T> it)
T
- Type of elementit
- The Iterable
instance - ignored if null
/emptynull
if none availablepublic 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<? super U,? extends V> mapper)
public static <U,V> Iterator<V> wrapIterator(Iterable<? extends U> iter, Function<? super U,? extends V> mapper)
public static <U,V> Iterator<V> wrapIterator(Iterator<? extends U> iter, Function<? super U,? extends V> mapper)
public static <T> T selectNextMatchingValue(Iterator<?> values, 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> Iterable<T> multiIterableSuppliers(Iterable<? extends Supplier<? extends 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> Supplier<T> memoizeLock(Supplier<? extends T> delegate)
T
- Generic type of supplied valuedelegate
- The actual Supplierpublic static boolean isPositive(Duration d)
d
- the durationtrue
if the duration is greater than zeropublic static boolean isNegativeOrNull(Duration d)
d
- the durationtrue
if the duration is negative or zeroCopyright © 2008–2024 The Apache Software Foundation. All rights reserved.