Package org.apache.sshd.common.util
Class GenericUtils
- java.lang.Object
-
- org.apache.sshd.common.util.GenericUtils
-
public final class GenericUtils extends Object
- Author:
- Apache MINA SSHD Project
-
-
Field Summary
Fields Modifier and Type Field Description static Comparator<String>
CASE_SENSITIVE_ORDER
The complement ofString.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 anull
value - to be used as a placeholder wherenull
s are not allowedstatic String
QUOTES
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Throwable>
TaccumulateException(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>>
NavigableSet<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>>
NavigableSet<V>asSortedSet(V... values)
static <V> Supplier<NavigableMap<String,V>>
caseInsensitiveMap()
static int
compare(char[] c1, char[] c2)
Compares 2 character arrays - Note:null
and empty are considered equalstatic <T> boolean
containsAny(Collection<? extends T> coll, Iterable<? extends T> values)
static <B,D extends B>
Function<D,B>downcast()
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 <K,V,M extends Map<V,K>>
MflipMap(Map<? extends K,? extends V> map, Supplier<? extends M> mapCreator, boolean allowDuplicates)
Flips between keys and values of an input mapstatic <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
isEmpty(char[] chars)
static boolean
isEmpty(CharSequence cs)
static <T> boolean
isEmpty(Iterable<? extends T> iter)
static boolean
isEmpty(Collection<?> c)
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 <T> boolean
isNotEmpty(Iterable<? extends T> iter)
static boolean
isNotEmpty(Collection<?> c)
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
iteratorstatic <T> Iterator<T>
iteratorOf(Iterator<T> iter)
Resolves to an always non-null
iteratorstatic 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 <K,V,M extends Map<K,V>>
MmapValues(Function<? super V,? extends K> keyMapper, Supplier<? extends M> mapCreator, Collection<? extends V> values)
Creates a map out of a group of valuesstatic <K,V,M extends Map<K,V>>
MmapValues(Function<? super V,? extends K> keyMapper, Supplier<? extends M> mapCreator, V... values)
static <T> Iterable<T>
multiIterableSuppliers(Iterable<? extends Supplier<? extends Iterable<? extends T>>> providers)
static <E extends Enum<E>>
Set<E>of(E... values)
static <E extends Enum<E>>
Set<E>of(Collection<? extends 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 predicatestatic <T> List<T>
selectMatchingMembers(Predicate<? super T> acceptor, T... values)
Returns a list of all the values that were accepted by a predicatestatic <T> T
selectNextMatchingValue(Iterator<?> values, Class<T> type)
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 aSupplier
static <T> BinaryOperator<T>
throwingMerger()
static IOException
toIOException(Throwable e)
static RuntimeException
toRuntimeException(Throwable t)
static RuntimeException
toRuntimeException(Throwable t, boolean peelThrowable)
Converts a thrown generic exception to aRuntimeException
static <T,K,U>
NavigableMap<K,U>toSortedMap(Iterable<? extends T> values, Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, Comparator<? super K> comparator)
static <T,K,U>
Collector<T,?,NavigableMap<K,U>>toSortedMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, Comparator<? super K> comparator)
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)
-
-
-
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 String[] EMPTY_STRING_ARRAY
-
EMPTY_OBJECT_ARRAY
public static final Object[] EMPTY_OBJECT_ARRAY
-
EMPTY_BOOLEAN_ARRAY
public static final boolean[] EMPTY_BOOLEAN_ARRAY
-
NULL
public static final Object NULL
A value indicating anull
value - to be used as a placeholder wherenull
s are not allowed
-
CASE_SENSITIVE_ORDER
public static final Comparator<String> CASE_SENSITIVE_ORDER
The complement ofString.CASE_INSENSITIVE_ORDER
-
QUOTES
public static final String QUOTES
- See Also:
- Constant Field Values
-
-
Method Detail
-
hashCode
public static int hashCode(String s)
- Parameters:
s
- TheString
value 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(String s, Boolean useUppercase)
- Parameters:
s
- TheString
value 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
-
isSameReference
public static <T> boolean isSameReference(T o1, T o2)
-
length
public static int length(CharSequence cs)
-
isEmpty
public static boolean isEmpty(CharSequence cs)
-
isNotEmpty
public static boolean isNotEmpty(CharSequence cs)
-
indexOf
public static int indexOf(CharSequence cs, char c)
-
lastIndexOf
public static int lastIndexOf(CharSequence cs, char c)
-
join
public static <T> String join(T[] values, char ch)
-
join
public static <T> String join(T[] values, CharSequence sep)
-
join
public static String join(Iterable<?> iter, CharSequence sep)
-
join
public static String join(Iterator<?> iter, CharSequence sep)
-
size
public static int size(Collection<?> c)
-
isEmpty
public static boolean isEmpty(Collection<?> c)
-
isNotEmpty
public static boolean isNotEmpty(Collection<?> c)
-
size
public static int size(Map<?,?> m)
-
isEmpty
public static boolean isEmpty(Map<?,?> m)
-
isNotEmpty
public static boolean isNotEmpty(Map<?,?> m)
-
length
@SafeVarargs public static <T> int length(T... a)
-
isEmpty
public static <T> boolean isEmpty(Iterable<? extends T> iter)
-
isNotEmpty
public static <T> boolean isNotEmpty(Iterable<? extends T> iter)
-
isEmpty
public static <T> boolean isEmpty(Iterator<? extends T> iter)
-
isNotEmpty
public static <T> boolean isNotEmpty(Iterator<? extends T> iter)
-
isEmpty
@SafeVarargs 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:null
and 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 Enum<E>> Set<E> of(E... values)
-
of
public static <E extends Enum<E>> Set<E> of(Collection<? extends E> values)
-
findFirstDifferentValueIndex
public static <T> int findFirstDifferentValueIndex(List<? extends T> c1, List<? extends T> c2)
-
findFirstDifferentValueIndex
public static <T> int findFirstDifferentValueIndex(List<? extends T> c1, List<? extends T> c2, UnaryEquator<? super T> equator)
-
findFirstDifferentValueIndex
public static <T> int findFirstDifferentValueIndex(Iterable<? extends T> c1, Iterable<? extends T> c2)
-
findFirstDifferentValueIndex
public static <T> int findFirstDifferentValueIndex(Iterable<? extends T> c1, Iterable<? extends T> c2, UnaryEquator<? super T> equator)
-
findFirstDifferentValueIndex
public static <T> int findFirstDifferentValueIndex(Iterator<? extends T> i1, Iterator<? extends T> i2)
-
findFirstDifferentValueIndex
public static <T> int findFirstDifferentValueIndex(Iterator<? extends T> i1, Iterator<? extends T> i2, UnaryEquator<? super T> equator)
-
containsAny
public static <T> boolean containsAny(Collection<? extends T> coll, Iterable<? extends T> values)
-
map
public static <T,U> List<U> map(Collection<? extends T> values, Function<? super T,? extends U> mapper)
-
mapSort
public static <T,U> NavigableSet<U> mapSort(Collection<? extends T> values, Function<? super T,? extends U> mapper, Comparator<? super U> comparator)
-
toSortedMap
public static <T,K,U> NavigableMap<K,U> toSortedMap(Iterable<? extends T> values, Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, Comparator<? super K> comparator)
-
toSortedMap
public static <T,K,U> Collector<T,?,NavigableMap<K,U>> toSortedMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, Comparator<? super K> comparator)
-
throwingMerger
public static <T> BinaryOperator<T> throwingMerger()
-
toSortedSet
public static <T> Collector<T,?,NavigableSet<T>> toSortedSet(Comparator<? super T> comparator)
-
unmodifiableList
@SafeVarargs public static <T> List<T> unmodifiableList(T... values)
-
unmodifiableList
public static <T> List<T> unmodifiableList(Collection<? extends T> values)
-
asList
@SafeVarargs public static <T> List<T> asList(T... values)
-
asSet
@SafeVarargs public static <T> Set<T> asSet(T... values)
-
asSortedSet
@SafeVarargs public static <V extends Comparable<V>> NavigableSet<V> asSortedSet(V... values)
-
asSortedSet
public static <V extends Comparable<V>> NavigableSet<V> asSortedSet(Collection<? extends V> values)
-
asSortedSet
@SafeVarargs public static <V> NavigableSet<V> asSortedSet(Comparator<? super V> comp, V... values)
- Type Parameters:
V
- The element type- Parameters:
comp
- The (non-null
)Comparator
to usevalues
- The values to be added (ignored ifnull
)- Returns:
- A
NavigableSet
containing the values (if any) sorted using the provided comparator
-
asSortedSet
public static <V> NavigableSet<V> asSortedSet(Comparator<? super V> comp, Collection<? extends V> values)
- Type Parameters:
V
- The element type- Parameters:
comp
- The (non-null
)Comparator
to usevalues
- The values to be added (ignored ifnull
/empty)- Returns:
- A
NavigableSet
containing the values (if any) sorted using the provided comparator
-
caseInsensitiveMap
public static <V> Supplier<NavigableMap<String,V>> caseInsensitiveMap()
- Type Parameters:
V
- Type of mapped value- Returns:
- A
Supplier
that returns a newNavigableMap
whenever itsget()
method is invoked
-
flipMap
public static <K,V,M extends Map<V,K>> M flipMap(Map<? extends K,? extends V> map, Supplier<? extends M> mapCreator, boolean allowDuplicates)
Flips between keys and values of an input map- Type Parameters:
K
- Original map key typeV
- Original map value typeM
- Flipped map type- Parameters:
map
- The original map to flipmapCreator
- The creator of the target mapallowDuplicates
- Whether to ignore duplicates on flip- Returns:
- The flipped map result
- Throws:
IllegalArgumentException
- if allowDuplicates isfalse
and a duplicate value found in the original map.
-
mapValues
@SafeVarargs public static <K,V,M extends Map<K,V>> M mapValues(Function<? super V,? extends K> keyMapper, Supplier<? extends M> mapCreator, V... values)
-
mapValues
public static <K,V,M extends Map<K,V>> M mapValues(Function<? super V,? extends K> keyMapper, Supplier<? extends M> mapCreator, Collection<? extends V> values)
Creates a map out of a group of values- Type Parameters:
K
- The key typeV
- The value typeM
- The resultMap
type- Parameters:
keyMapper
- TheFunction
that generates a key for a given value. If the returned key isnull
then the value is not mappedmapCreator
- TheSupplier
used to create/retrieve the result map - provided non-empty group of valuesvalues
- The values to be mapped- Returns:
- The resulting
Map
- Note: no validation is made to ensure that 2 (or more) values are not mapped to the same key
-
findFirstMatchingMember
@SafeVarargs public static <T> T findFirstMatchingMember(Predicate<? super T> acceptor, T... values)
-
findFirstMatchingMember
public static <T> T findFirstMatchingMember(Predicate<? super T> acceptor, Collection<? extends T> values)
-
selectMatchingMembers
@SafeVarargs public static <T> List<T> selectMatchingMembers(Predicate<? super T> acceptor, T... values)
Returns a list of all the values that were accepted by a predicate
-
selectMatchingMembers
public 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
-
stripQuotes
public static CharSequence stripQuotes(CharSequence s)
- Parameters:
s
- TheCharSequence
to be checked- Returns:
- If the sequence contains any of the
QUOTES
on both ends, then they are stripped, otherwise nothing is done - See Also:
stripDelimiters(CharSequence, char)
-
stripDelimiters
public static CharSequence stripDelimiters(CharSequence s, char delim)
- Parameters:
s
- TheCharSequence
to be checkeddelim
- The expected delimiter- Returns:
- If the sequence contains the delimiter on both ends, then it is are stripped, otherwise nothing is done
-
toRuntimeException
public static RuntimeException toRuntimeException(Throwable t)
-
toRuntimeException
public static RuntimeException toRuntimeException(Throwable t, boolean peelThrowable)
Converts a thrown generic exception to aRuntimeException
- Parameters:
t
- The original thrown exceptionpeelThrowable
- Whether to determine the root cause by "peeling" any enclosing exceptions- Returns:
- The thrown cause if already a runtime exception, otherwise a runtime exception of the resolved exception as its cause
- See Also:
peelException(Throwable)
-
peelException
public 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.- Parameters:
t
- The originalThrowable
- ignored ifnull
- Returns:
- The effective exception - same as input if not a wrapper
-
resolveExceptionCause
public static Throwable resolveExceptionCause(Throwable t)
- Parameters:
t
- The originalThrowable
- ignored ifnull
- Returns:
- If
Throwable.getCause()
is non-null
then the cause, otherwise the original exception -null
if the original exception wasnull
-
accumulateException
public static <T extends Throwable> T accumulateException(T current, T extra)
Used to "accumulate" exceptions of the same type. If the current exception isnull
then the new one becomes the current, otherwise the new one is added as a suppressed exception to the current one- Type Parameters:
T
- The exception type- Parameters:
current
- The current exceptionextra
- The extra/new exception- Returns:
- The resolved exception
- See Also:
Throwable.addSuppressed(Throwable)
-
toIOException
public static IOException toIOException(Throwable e)
-
supplierOf
public static <T> 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> Iterator<T> iteratorOf(Iterable<T> iterable)
Resolves to an always non-null
iterator- Type Parameters:
T
- Type of value being iterated- Parameters:
iterable
- TheIterable
instance- Returns:
- A non-
null
iterator 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> Function<D,B> downcast()
- Type Parameters:
B
- Generic base classD
- Generic child class- Returns:
- An identity
Function
that returns its input child class as a base class
-
head
public static <T> T head(Iterable<? extends T> it)
- Type Parameters:
T
- Type of element- Parameters:
it
- TheIterable
instance - ignored ifnull
/empty- Returns:
- first element by iteration or
null
if none available
-
iteratorOf
public static <T> Iterator<T> iteratorOf(Iterator<T> iter)
Resolves to an always non-null
iterator- Type Parameters:
T
- Type of value being iterated- Parameters:
iter
- TheIterator
instance- Returns:
- A non-
null
iterator which may be empty if no iterator instance - See Also:
Collections.emptyIterator()
-
wrapIterable
public static <U,V> Iterable<V> wrapIterable(Iterable<? extends U> iter, Function<? super U,? extends V> mapper)
-
wrapIterator
public static <U,V> Iterator<V> wrapIterator(Iterable<? extends U> iter, Function<? super U,? extends V> mapper)
-
wrapIterator
public static <U,V> Iterator<V> wrapIterator(Iterator<? extends U> iter, Function<? super U,? extends V> mapper)
-
selectNextMatchingValue
public static <T> T selectNextMatchingValue(Iterator<?> values, Class<T> type)
- Type Parameters:
T
- Generic return type- Parameters:
values
- The source values - ignored ifnull
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.- Returns:
- The first value that matches the specified type -
null
if none found
-
multiIterableSuppliers
public static <T> Iterable<T> multiIterableSuppliers(Iterable<? extends Supplier<? extends Iterable<? extends T>>> providers)
Wraps a group ofSupplier
s ofIterable
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- 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
-
-