public class CollectionUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
protected static class |
CollectionUtil.LocalStringPair |
Modifier and Type | Field and Description |
---|---|
static CollectionUtil |
CU
An instance of this class which can be easily included
as static import and then used like
SU.repeat('x', 3) . |
static int[] |
EMPTY_INT_ARRAY
A constant that contains an empty int array.
|
static java.util.List |
EMPTY_LIST
A constant that contains an immutable empty List.
|
static long[] |
EMPTY_LONG_ARRAY
A constant that contains an empty long array.
|
static java.util.Map |
EMPTY_MAP
A constant that contains an immutable empty Map.
|
static java.lang.Object[] |
EMPTY_OBJECT_ARRAY
A constant that contains an empty object array.
|
static java.util.Set |
EMPTY_SET
A constant that contains an immutable empty Set.
|
Modifier | Constructor and Description |
---|---|
protected |
CollectionUtil()
Initialize the new instance with default values.
|
Modifier and Type | Method and Description |
---|---|
protected <E> void |
addAll(java.util.Collection<E> collection,
E[] objects,
boolean justNew)
Adds all given objects to the specified collection.
|
<T> void |
addAll(java.util.Collection<T> collection,
T... objects)
Adds all given objects to the specified collection.
|
void |
addAll(java.util.Map<java.lang.String,java.lang.String> map,
java.lang.String... keyValuePairs)
Adds all given key/value pairs to the map.
|
void |
addAll(java.util.Properties properties,
java.lang.String... keyValuePairs)
Adds all given key/value pairs to the properties.
|
<T> void |
addAllNew(java.util.Collection<T> collection,
T... objects)
Adds all given objects to the specified collection, if they are not
already in the collection.
|
int[] |
append(int[] values,
int... appendValues)
Returns an array of int values that contains all ints given
by the first and second array.
|
long[] |
append(long[] values,
long... appendValues)
Returns an array of long values that contains all longs given
by the first and second array.
|
<T> T[] |
append(T[] objects,
T... appendObjects)
Returns an array of objects that contains all objects given
by the first and second object array.
|
<T> java.util.Enumeration<T> |
asEnumeration(java.util.Collection<T> collection)
Convert the given collection to an Enumeration.
|
<T> java.util.Enumeration<T> |
asEnumeration(T... objects)
Convert the given array to an Enumeration.
|
<T> java.util.Iterator<T> |
asIterator(java.util.Enumeration<T> enumeration)
Returns an iterator based on the given enumeration or null if the input
parameter is null, too.
|
<T> java.util.List<T> |
asList(java.util.Enumeration<T> enumeration)
Returns a list containing all elements of the given enumeration.
|
<T> java.util.List<T> |
asList(T... objects)
Returns an ArrayList that contains all objects from the given array.
|
<E,L extends java.util.List<E>> |
asListType(java.lang.Class<L> listType,
E... elements)
Creates a list of the specified type and adds all given elements to it.
|
java.util.Map<java.lang.String,java.lang.String> |
asMap(IConfigSettings settings)
Returns a Map
|
java.util.Map<java.lang.String,java.lang.String> |
asMap(java.util.Properties properties)
Returns a Map
|
<T extends java.lang.Comparable<T>> |
asSortedList(java.util.Collection<T> collection,
boolean ascending)
Returns an ArrayList that contains all objects from the collection in a sorted order.
|
boolean |
contains(int[] valueArray,
int value)
Returns true only if the given value is found in the given array.
|
boolean |
contains(long[] valueArray,
long value)
Returns true only if the given value is found in the given array.
|
boolean |
contains(java.lang.Object[] objArray,
java.lang.Object searchObj)
Returns true if the search object exists in the given object array.
|
boolean |
containsIdentical(java.lang.Object[] objArray,
java.lang.Object searchObj)
Returns true if the search object exists in the given object array.
|
<S,T> void |
convertCollectionElements(java.util.Collection<T> result,
java.util.Collection<S> collection,
IFunction<S,T> converter)
Converts all elements in the given collection and adds the new objects
into the given result collection.
|
<S,T> void |
convertCollectionElements(java.util.Collection<T> result,
java.util.Collection<S> collection,
IObjectConverter<S,T> converter)
Converts all elements in the given collection and adds the new objects
into the given result collection.
|
<S,T> java.util.List<T> |
convertToList(java.util.Collection<S> collection,
IFunction<S,T> converter)
Converts all elements in the given collection to a list using the specified
converter function to convert each element.
|
<S,T> java.util.List<T> |
convertToList(java.util.Collection<S> collection,
IObjectConverter<S,T> converter)
Converts all elements in the given collection to a list using the specified
converter to convert each element.
|
<S,T> java.util.Set<T> |
convertToSet(java.util.Collection<S> collection,
IFunction<S,T> converter)
Converts all elements in the given collection to a set using the specified
converter function to convert each element.
|
<S,T> java.util.Set<T> |
convertToSet(java.util.Collection<S> collection,
IObjectConverter<S,T> converter)
Converts all elements in the given collection to a set using the specified
converter to convert each element.
|
<T> java.util.Collection<T> |
copy(java.util.Collection<T> objects,
IObjectFilter<T> filter)
Returns a copy of the given collection which contains only those objects
that match the given filter.
|
<K,V> java.util.Map<K,V> |
copy(java.util.Map<K,V> map,
IObjectFilter<java.util.Map.Entry<K,V>> filter)
Returns a copy of the given map which contains only those objects
that match the given filter.
|
<T> T[] |
copy(T... objects)
Returns a copy of the given object array.
|
<T> T[] |
copy(T[] objects,
IObjectFilter<T> filter)
Returns a copy of the given object array which contains only those objects
that match the given filter.
|
protected <T> T[] |
copy(T[] objArray,
IObjectFilter<T> filter,
boolean without) |
protected <E> java.util.Collection<E> |
copyCollection(java.util.Collection<E> objects,
IObjectFilter<E> filter,
boolean without) |
protected java.util.Map |
copyMap(java.util.Map map,
IObjectFilter filter,
boolean without) |
void |
copyPropertiesStartingWith(java.util.Properties source,
java.util.Properties destination,
java.lang.String prefix,
boolean cutPrefix)
Copies all properties that start with the given prefix from the
specified source properties object to the destination properties.
|
<T> java.util.Collection<T> |
copyWithout(java.util.Collection<T> objects,
IObjectFilter<T> filter)
Returns a copy of the specified collection that contains only those elements
that do NOT match the given filter.
|
<K,V> java.util.Map<K,V> |
copyWithout(java.util.Map<K,V> map,
IObjectFilter<java.util.Map.Entry<K,V>> filter)
Returns a copy of the specified map that contains only those elements
that do NOT match the given filter.
|
<T> T[] |
copyWithout(T[] objArray,
IObjectFilter<T> filter)
Returns a copy of the specified array that contains only those elements
that do NOT match the given filter.
|
<T> T[] |
copyWithout(T[] objArray,
T... skipArray)
Returns a copy of the first array that contains only elements that are
not in the second array.
|
<T> T[] |
copyWithoutIdentical(T[] objArray,
T... skipArray)
Returns a copy of the first array that contains only elements that are
not in the second array.
|
protected <C extends java.util.Collection<E>,E> |
createCollection(java.lang.Class<C> collectionType) |
static CollectionUtil |
current()
Returns the one and only instance of this class.
|
<T> T |
getFirstNonNull(T... array)
Returns the first element of the given array that is not null or
throws an ObjectNotFoundException if no such element can be found.
|
<K,V> V |
getOrInit(java.util.Map<K,V> map,
K key,
ISupplier<V> factory)
Returns the value from the given map that is associated to the specified key.
|
<K,V> V |
getOrInit(java.util.Map<K,V> map,
K key,
V initialValue)
Returns the value from the given map that is associated to the specified key.
|
<T> int |
indexOf(java.util.List<T> list,
IObjectFilter<T> filter)
Returns the index of the first object in the given list
that matches the given filter.
|
<T> int |
indexOf(T[] objArray,
IObjectFilter<T> filter)
Returns the index of the first object in the given object array
that matches the given filter.
|
<T> int |
indexOf(T[] objArray,
T searchObj)
Returns the index of the search object in the given object array.
|
<T> int |
indexOfIdentical(T[] objArray,
T searchObj)
Returns the index of the search object in the given object array.
|
protected int |
indexOfObject(java.lang.Object[] objArray,
java.lang.Object searchObj,
boolean identical) |
boolean |
isAnyNull(java.lang.Object... objects)
Returns true if the given array contains at least one null value.
|
boolean |
isNoneNull(java.lang.Object... objects)
Returns true if the given array contains no null value.
|
boolean |
isNullOrEmpty(AssociationList<?,?> associationList)
Returns true if the given association list is null or has no elements.
|
boolean |
isNullOrEmpty(java.util.Collection<?> collection)
Returns true if the given collection is null or has no elements.
|
boolean |
isNullOrEmpty(java.util.Map<?,?> map)
Returns true if the given map is null or has no elements.
|
boolean |
isNullOrEmpty(java.lang.Object[] array)
Returns true if the given array is null or has no elements.
|
<T> java.util.Iterator<T> |
iterator(T... objects)
Returns an iterator on the given array.
|
<T> int |
lastIndexOf(java.util.List<T> list,
IObjectFilter<T> filter)
Returns the index of the last object in the given list
that matches the given filter.
|
<T> int |
lastIndexOf(T[] objArray,
IObjectFilter<T> filter)
Returns the index of the last object in the given object array
that matches the given filter.
|
java.util.Properties |
propertiesStartingWith(java.util.Properties source,
java.lang.String prefix)
Returns a new properties object with all properties that start with the given
prefix copied from the specified source properties object.
|
java.util.Properties |
propertiesStartingWith(java.util.Properties source,
java.lang.String prefix,
boolean cutPrefix)
Returns a new properties object with all properties that start with the given
prefix copied from the specified source properties object.
|
protected <T> T[] |
removeFromArray(T[] array,
T[] removeObjects)
Removes the given objects from the array.
|
<T> T[] |
removeNull(T... array)
Removes all null values from the given array.
|
void |
reverse(java.lang.Object[] array)
Puts all elements in the specified array into reverse order.
|
<T> T[] |
reverseCopy(T... array)
Returns a new array that contains all elements of the given array
in reverse order.
|
protected IStringPair |
splitToStringPair(java.lang.String string,
java.lang.String... separatorsToTry) |
<T> T[] |
toArray(java.util.Collection<T> objects)
Returns an array which has elements of the type of the first element in the given
collection.
|
<T> T[] |
toArray(java.util.Collection<T> objects,
java.lang.Class<T> elementType)
Returns an array which has elements of the specified element type.
|
<T> java.util.List<T> |
toList(T... objects)
Returns an ArrayList that contains all objects from the given array.
|
public static final CollectionUtil CU
SU.repeat('x', 3)
.public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
public static final int[] EMPTY_INT_ARRAY
public static final long[] EMPTY_LONG_ARRAY
public static final java.util.Set EMPTY_SET
public static final java.util.List EMPTY_LIST
public static final java.util.Map EMPTY_MAP
protected CollectionUtil()
public static CollectionUtil current()
public java.util.Properties propertiesStartingWith(java.util.Properties source, java.lang.String prefix)
source
- The properties to copy fromprefix
- The prefix all properties to copy have in commonpublic java.util.Properties propertiesStartingWith(java.util.Properties source, java.lang.String prefix, boolean cutPrefix)
source
- The properties to copy fromprefix
- The prefix all properties to copy have in commoncutPrefix
- If true, the prefix gets cut off the keys in the destination objectpublic void copyPropertiesStartingWith(java.util.Properties source, java.util.Properties destination, java.lang.String prefix, boolean cutPrefix)
source
- The properties to copy from (if null nothing happens at all)destination
- The destination object where the properties are copied to (if null nothing happens at all)prefix
- The prefix all properties to copy have in common (if null nothing happens at all)cutPrefix
- If true, the prefix gets cut off the keys in the destination objectpublic <T> T[] copy(T... objects)
public <T> T[] copy(T[] objects, IObjectFilter<T> filter)
public <T> java.util.Collection<T> copy(java.util.Collection<T> objects, IObjectFilter<T> filter)
public <K,V> java.util.Map<K,V> copy(java.util.Map<K,V> map, IObjectFilter<java.util.Map.Entry<K,V>> filter)
public <K,V> V getOrInit(java.util.Map<K,V> map, K key, V initialValue)
map
- The map to get the value from (must not be null).key
- The key to lookup or store the initial value (must not be null).initialValue
- The value to be put to the map if no entry with the given key exists yet (must not be null).public <K,V> V getOrInit(java.util.Map<K,V> map, K key, ISupplier<V> factory)
map
- The map to get the value from (must not be null).key
- The key to lookup or store the initial value (must not be null).factory
- The factory to produce an initial value to be put to the map if no entry with the given key exists yet (must not be null).public <T> T[] reverseCopy(T... array)
public void reverse(java.lang.Object[] array)
public <T> void addAll(java.util.Collection<T> collection, T... objects)
collection
- The collection to which the objects are addedobjects
- The objects to add to the collectionpublic <T> void addAllNew(java.util.Collection<T> collection, T... objects)
collection
- The collection to which the objects are addedobjects
- The objects to add to the collectionpublic void addAll(java.util.Map<java.lang.String,java.lang.String> map, java.lang.String... keyValuePairs)
Example: addAll(map, "a=1", "b=2", "c=3")
map
- The map to which the key/value pairs must be added.
If this parameter is null, nothing happens.keyValuePairs
- The key/value pairs to be added.public void addAll(java.util.Properties properties, java.lang.String... keyValuePairs)
Example: addAll(properties, "a=1", "b=2", "c=3")
properties
- The properties to which the key/value pairs must be added.
If this parameter is null, nothing happens.keyValuePairs
- The key/value pairs to be added.public <T> T[] append(T[] objects, T... appendObjects)
objects
- The array of objects to which to appendappendObjects
- The objects to be appended to the first arraypublic int[] append(int[] values, int... appendValues)
values
- The array of int values to which to appendappendValues
- The int values to be appended to the first arraypublic long[] append(long[] values, long... appendValues)
values
- The array of long values to which to appendappendValues
- The long values to be appended to the first arraypublic <T> int indexOfIdentical(T[] objArray, T searchObj)
objArray
- The array to search insearchObj
- The object to search for (might be null)public <T> int indexOf(T[] objArray, T searchObj)
objArray
- The array to search insearchObj
- The object to search for (might be null)public <T> int indexOf(T[] objArray, IObjectFilter<T> filter)
objArray
- The array to search infilter
- The filter to determine the object to look for (must not be null)public <T> int lastIndexOf(T[] objArray, IObjectFilter<T> filter)
objArray
- The array to search infilter
- The filter to determine the object to look for (must not be null)public <T> int indexOf(java.util.List<T> list, IObjectFilter<T> filter)
list
- The list to search infilter
- The filter to determine the object to look for (must not be null)public <T> int lastIndexOf(java.util.List<T> list, IObjectFilter<T> filter)
list
- The list to search infilter
- The filter to determine the object to look for (must not be null)public boolean containsIdentical(java.lang.Object[] objArray, java.lang.Object searchObj)
objArray
- The array to search insearchObj
- The object to search for (might be null)public boolean contains(java.lang.Object[] objArray, java.lang.Object searchObj)
objArray
- The array to search insearchObj
- The object to search for (might be null)public boolean contains(int[] valueArray, int value)
valueArray
- The array in which to look for the valuevalue
- The value to look forpublic boolean contains(long[] valueArray, long value)
valueArray
- The array in which to look for the valuevalue
- The value to look forpublic <T> T[] copyWithoutIdentical(T[] objArray, T... skipArray)
objArray
- The array to take the elements fromskipArray
- The array that contains all objects that must not be in the result arraypublic <T> T[] copyWithout(T[] objArray, T... skipArray)
objArray
- The array to take the elements fromskipArray
- The array that contains all objects that must not be in the result arraypublic <T> T[] copyWithout(T[] objArray, IObjectFilter<T> filter)
objArray
- The array to take the elements fromfilter
- A filter that defines all elements must not be copied into the result arraypublic <T> java.util.Collection<T> copyWithout(java.util.Collection<T> objects, IObjectFilter<T> filter)
objects
- The collection to take the elements fromfilter
- A filter that defines all elements must not be copied into the result collectionpublic <K,V> java.util.Map<K,V> copyWithout(java.util.Map<K,V> map, IObjectFilter<java.util.Map.Entry<K,V>> filter)
map
- The map to take the elements fromfilter
- A filter that defines all elements that must not be copied into the result mappublic <T> java.util.Iterator<T> iterator(T... objects)
objects
- The objects to provide an iterator forpublic <T> T[] removeNull(T... array)
array
- The array to be cleared of null valuespublic <T> T getFirstNonNull(T... array) throws ObjectNotFoundException
array
- The array from which to detect the non-null element.ObjectNotFoundException
- If no non-null object has been found in the array or the array was null.public <T> T[] toArray(java.util.Collection<T> objects, java.lang.Class<T> elementType)
objects
- The object to be put into the result arrayelementType
- The type of the array elementsjava.lang.ArrayStoreException
- if any element in the collection is not of the same type as the element typepublic <T> T[] toArray(java.util.Collection<T> objects)
java.lang.ArrayStoreException
- if any element in the collection is not of the same type as the first elementpublic <T> java.util.List<T> toList(T... objects)
Be aware that in contrary to Arrays.asList(Object...)
this method
returns an instance of ArrayList
which is NOT backed by the given
array of objects. That is, changing the list has no impact on the array.
public <T> java.util.List<T> asList(T... objects)
Be aware that in contrary to Arrays.asList(Object...)
this method
returns an instance of ArrayList
which is NOT backed by the given
array of objects. That is, changing the list has no impact on the array.
Actually this method is doing exactly the same as toList(Object...)
.
It is just here to provide the same method name as Arrays.asList(Object...)
.
public <T extends java.lang.Comparable<T>> java.util.List<T> asSortedList(java.util.Collection<T> collection, boolean ascending)
collection
- The objects to be sorted (must not be null).ascending
- if true the objects will be sorted ascending otherwise descending.public <E,L extends java.util.List<E>> java.util.List<E> asListType(java.lang.Class<L> listType, E... elements)
listType
- A valid list class that implements the List
interface (must not be null).elements
- The elements the new list should be filled with.public <S,T> java.util.List<T> convertToList(java.util.Collection<S> collection, IFunction<S,T> converter)
collection
- The collection of elements to convert (must not be null).converter
- The converter function to be applied on each element (must not be null).public <S,T> java.util.List<T> convertToList(java.util.Collection<S> collection, IObjectConverter<S,T> converter)
collection
- The collection of elements to convert (must not be null).converter
- The converter to be applied on each element (must not be null).public <S,T> java.util.Set<T> convertToSet(java.util.Collection<S> collection, IFunction<S,T> converter)
collection
- The collection of elements to convert (must not be null).converter
- The converter function to be applied on each element (must not be null).public <S,T> java.util.Set<T> convertToSet(java.util.Collection<S> collection, IObjectConverter<S,T> converter)
collection
- The collection of elements to convert (must not be null).converter
- The converter to be applied on each element (must not be null).public <S,T> void convertCollectionElements(java.util.Collection<T> result, java.util.Collection<S> collection, IFunction<S,T> converter)
result
- The result collection into which the converted object will be put (must not be null).collection
- The collection of elements to convert (must not be null).converter
- The converter function to be applied on each element (must not be null).public <S,T> void convertCollectionElements(java.util.Collection<T> result, java.util.Collection<S> collection, IObjectConverter<S,T> converter)
result
- The result collection into which the converted object will be put (must not be null).collection
- The collection of elements to convert (must not be null).converter
- The converter to be applied on each element (must not be null).public boolean isNoneNull(java.lang.Object... objects)
objects
- The array to check (must not be null).public boolean isAnyNull(java.lang.Object... objects)
objects
- The array to check (must not be null).public boolean isNullOrEmpty(java.util.Collection<?> collection)
collection
- The collection to checkpublic boolean isNullOrEmpty(java.lang.Object[] array)
array
- The array to checkpublic boolean isNullOrEmpty(java.util.Map<?,?> map)
map
- The map to checkpublic boolean isNullOrEmpty(AssociationList<?,?> associationList)
associationList
- The association list to checkpublic <T> java.util.Iterator<T> asIterator(java.util.Enumeration<T> enumeration)
public <T> java.util.List<T> asList(java.util.Enumeration<T> enumeration)
public java.util.Map<java.lang.String,java.lang.String> asMap(java.util.Properties properties)
public java.util.Map<java.lang.String,java.lang.String> asMap(IConfigSettings settings)
public <T> java.util.Enumeration<T> asEnumeration(java.util.Collection<T> collection)
public <T> java.util.Enumeration<T> asEnumeration(T... objects)
protected <E> void addAll(java.util.Collection<E> collection, E[] objects, boolean justNew)
collection
- The collection to which the objects are addedobjects
- The objects to add to the collectionjustNew
- If true only new elements are added to the collectionprotected int indexOfObject(java.lang.Object[] objArray, java.lang.Object searchObj, boolean identical)
protected <T> T[] removeFromArray(T[] array, T[] removeObjects)
protected <T> T[] copy(T[] objArray, IObjectFilter<T> filter, boolean without)
protected <E> java.util.Collection<E> copyCollection(java.util.Collection<E> objects, IObjectFilter<E> filter, boolean without)
protected <C extends java.util.Collection<E>,E> java.util.Collection<E> createCollection(java.lang.Class<C> collectionType)
protected java.util.Map copyMap(java.util.Map map, IObjectFilter filter, boolean without)
protected IStringPair splitToStringPair(java.lang.String string, java.lang.String... separatorsToTry)