Package org.apache.sshd.common.util
Class GenericUtils
- java.lang.Object
-
- org.apache.sshd.common.util.GenericUtils
-
public final class GenericUtils extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.Comparator<java.lang.String>CASE_SENSITIVE_ORDERThe complement ofString.CASE_INSENSITIVE_ORDERstatic boolean[]EMPTY_BOOLEAN_ARRAYstatic byte[]EMPTY_BYTE_ARRAYstatic char[]EMPTY_CHAR_ARRAYstatic java.lang.Object[]EMPTY_OBJECT_ARRAYstatic java.lang.String[]EMPTY_STRING_ARRAYstatic java.lang.ObjectNULLA value indicating anullvalue - to be used as a placeholder wherenulls are not allowedstatic java.lang.StringQUOTES
-
Constructor Summary
Constructors Modifier Constructor Description privateGenericUtils()
-
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 intcompare(char[] c1, char[] c2)Compares 2 character arrays - Note:nulland empty are considered equalstatic <T> booleancontainsAny(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> booleanequals(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> intfindFirstDifferentValueIndex(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2)static <T> intfindFirstDifferentValueIndex(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, UnaryEquator<? super T> equator)static <T> intfindFirstDifferentValueIndex(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2)static <T> intfindFirstDifferentValueIndex(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, UnaryEquator<? super T> equator)static <T> intfindFirstDifferentValueIndex(java.util.List<? extends T> c1, java.util.List<? extends T> c2)static <T> intfindFirstDifferentValueIndex(java.util.List<? extends T> c1, java.util.List<? extends T> c2, UnaryEquator<? super T> equator)static <T> TfindFirstMatchingMember(java.util.function.Predicate<? super T> acceptor, java.util.Collection<? extends T> values)static <T> TfindFirstMatchingMember(java.util.function.Predicate<? super T> acceptor, T... values)static <T> voidforEach(java.lang.Iterable<? extends T> values, java.util.function.Consumer<? super T> consumer)static inthashCode(java.lang.String s)static inthashCode(java.lang.String s, java.lang.Boolean useUppercase)static <T> Thead(java.lang.Iterable<? extends T> it)Returns the first element in iterable - it has some optimization forList-sDeque-s andSortedSets.static intindexOf(java.lang.CharSequence cs, char c)static booleanisBlank(java.lang.CharSequence cs)Checks if a CharSequence is empty (""), null or whitespace only.static booleanisEmpty(char[] chars)static booleanisEmpty(java.lang.CharSequence cs)static <T> booleanisEmpty(java.lang.Iterable<? extends T> iter)static booleanisEmpty(java.util.Collection<?> c)static <T> booleanisEmpty(java.util.Iterator<? extends T> iter)static <T> booleanisEmpty(T[] a)static booleanisNegativeOrNull(java.time.Duration d)Check if a duration is negative or zerostatic booleanisNotBlank(java.lang.CharSequence cs)static booleanisNotEmpty(java.lang.CharSequence cs)static <T> booleanisNotEmpty(java.lang.Iterable<? extends T> iter)static booleanisNotEmpty(java.util.Collection<?> c)static <T> booleanisNotEmpty(java.util.Iterator<? extends T> iter)static booleanisPositive(java.time.Duration d)Check if a duration is positivestatic <T> java.util.Iterator<T>iteratorOf(java.lang.Iterable<T> iterable)Resolves to an always non-nulliteratorstatic <T> java.util.Iterator<T>iteratorOf(java.util.Iterator<T> iter)Resolves to an always non-nulliteratorstatic java.lang.Stringjoin(java.lang.Iterable<?> iter, char ch)static java.lang.Stringjoin(java.lang.Iterable<?> iter, java.lang.CharSequence sep)static java.lang.Stringjoin(java.util.Iterator<?> iter, char ch)static java.lang.Stringjoin(java.util.Iterator<?> iter, java.lang.CharSequence sep)static <T> java.lang.Stringjoin(T[] values, char ch)static <T> java.lang.Stringjoin(T[] values, java.lang.CharSequence sep)static intlastIndexOf(java.lang.CharSequence cs, char c)static intlength(char[] chars)static intlength(java.lang.CharSequence cs)static <T> intlength(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 ofSuppliers ofIterableinstances into a "unified"Iterableof 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 consultedstatic <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.Stringreplace(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 firstmaxvalues of the search String.static java.lang.StringreplaceWhitespaceAndTrim(java.lang.String s)static intsafeCompare(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 predicatestatic <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 predicatestatic <T> TselectNextMatchingValue(java.util.Iterator<?> values, java.lang.Class<T> type)static intsize(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.CharSequencestripDelimiters(java.lang.CharSequence s, char delim)static java.lang.CharSequencestripQuotes(java.lang.CharSequence s)static <T> java.util.function.Supplier<T>supplierOf(T value)Wraps a value into aSupplierstatic <T> java.util.stream.Collector<T,?,java.util.NavigableSet<T>>toSortedSet(java.util.Comparator<? super T> comparator)static java.lang.StringtrimToEmpty(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)
-
-
-
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 anullvalue - to be used as a placeholder wherenulls are not allowed
-
CASE_SENSITIVE_ORDER
public static final java.util.Comparator<java.lang.String> CASE_SENSITIVE_ORDER
The complement ofString.CASE_INSENSITIVE_ORDER
-
QUOTES
public static final java.lang.String QUOTES
- See Also:
- Constant Field Values
-
-
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
maxvalues of the search String.A
nullreference passed to this method is a no-op.- Parameters:
text- text to search and replace inrepl- String to search forwith- String to replace withmax- maximum number of values to replace, or-1if no maximum- Returns:
- the text with any replacements processed
-
hashCode
public static int hashCode(java.lang.String s)
- Parameters:
s- TheStringvalue to calculate the hash code on - may benull/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- TheStringvalue to calculate the hash code on - may benull/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 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:
trueif 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 collectionc2- Second collection- Returns:
trueif the following holds:- Same size - Note:
nullcollections are consider equal to empty ones - First collection contains all elements of second one and vice versa
- Same size - Note:
-
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:nulland empty are considered equal- Parameters:
c1- 1st arrayc2- 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)Comparatorto usevalues- The values to be added (ignored ifnull)- Returns:
- A
NavigableSetcontaining 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)Comparatorto usevalues- The values to be added (ignored ifnull/empty)- Returns:
- A
NavigableSetcontaining 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- ThePredicateto consult whether a member is selectedvalues- The values to be scanned- Returns:
- A
Listof 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- ThePredicateto consult whether a member is selectedvalues- The values to be scanned- Returns:
- A
Listof all the values that were accepted by the predicate
-
stripQuotes
public static java.lang.CharSequence stripQuotes(java.lang.CharSequence s)
- Parameters:
s- TheCharSequenceto be checked- Returns:
- If the sequence contains any of the
QUOTESon 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- TheCharSequenceto be checkeddelim- 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 aSupplier- 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-nulliterator- Type Parameters:
T- Type of value being iterated- Parameters:
iterable- TheIterableinstance- Returns:
- A non-
nulliterator 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 classD- Generic child class- Returns:
- An identity
Functionthat 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 forList-sDeque-s andSortedSets.- Type Parameters:
T- Type of element- Parameters:
it- TheIterableinstance - ignored ifnull/empty- Returns:
- first element by iteration or
nullif none available
-
iteratorOf
public static <T> java.util.Iterator<T> iteratorOf(java.util.Iterator<T> iter)
Resolves to an always non-nulliterator- Type Parameters:
T- Type of value being iterated- Parameters:
iter- TheIteratorinstance- Returns:
- A non-
nulliterator 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 ifnulltype- 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 -
nullif 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 ofSuppliers ofIterableinstances into a "unified"Iterableof 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 ifnull(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:
trueif 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:
trueif the duration is negative or zero
-
-