public final class Containers extends Static
Collection
or CheckedContainer
objects.
Unless otherwise noted in the javadoc, every collections except Map
returned
by the methods in this class implement the CheckedContainer
interface.Defined in the sis-utility module
Modifier and Type | Method and Description |
---|---|
static <SK,SV,K,V> |
derivedMap(Map<SK,SV> storage,
ObjectConverter<SK,K> keyConverter,
ObjectConverter<SV,V> valueConverter)
Returns a map whose whose keys and values are derived on-the-fly from the given map.
|
static <S,E> Set<E> |
derivedSet(Set<S> storage,
ObjectConverter<S,E> converter)
Returns a set whose elements are derived on-the-fly from the given set.
|
static int |
hashMapCapacity(int count)
Returns the capacity to be given to the
HashMap
constructor for holding the given number of elements. |
static boolean |
isNullOrEmpty(Collection<?> collection)
Returns
true if the given collection is either null or
empty. |
static boolean |
isNullOrEmpty(Map<?,?> map)
Returns
true if the given map is either null or empty. |
static <E> List<? extends E> |
unmodifiableList(E... array)
Returns an unmodifiable view of the given array.
|
static <E> List<? extends E> |
unmodifiableList(E[] array,
int lower,
int upper)
Returns an unmodifiable view of a subregion of the given array.
|
public static boolean isNullOrEmpty(Collection<?> collection)
true
if the given collection is either null or
empty. If this method returns false
,
then the given collection is guaranteed to be non-null and to contain at least
one element.
This is a convenience method for classes implementing the lazy instantiation pattern. In such cases, null collections (i.e. collections not yet instantiated) are typically considered as empty.
collection
- The collection to test, or null
.true
if the given collection is null or empty, or false
otherwise.public static boolean isNullOrEmpty(Map<?,?> map)
true
if the given map is either null or empty.
If this method returns false
, then the given map is guaranteed to be non-null and
to contain at least one element.
This is a convenience method for classes implementing the lazy instantiation pattern. In such cases, null maps (i.e. maps not yet instantiated) are typically considered as empty.
map
- The map to test, or null
.true
if the given map is null or empty, or false
otherwise.public static <E> List<? extends E> unmodifiableList(E... array)
The returned list implements the CheckedContainer
interface. The value returned by
its CheckedContainer.getElementType()
method is inferred from the array component type.
Because arrays in the Java language are covariant (at the contrary of collections),
the list type have to be <? extends E>
instead than <E>
.
E
- The base type of elements in the list.array
- The array to wrap, or null
if none.null
if the given
array was null.Arrays.asList(Object[])
public static <E> List<? extends E> unmodifiableList(E[] array, int lower, int upper)
The returned list implements the CheckedContainer
interface. The value returned by
its CheckedContainer.getElementType()
method is inferred from the array component type.
Because arrays in the Java language are covariant (at the contrary of collections),
the list type have to be <? extends E>
instead than <E>
.
E
- The type of elements in the list.array
- The array to wrap (can not be null).lower
- Low endpoint (inclusive) of the sublist.upper
- High endpoint (exclusive) of the sublist.IndexOutOfBoundsException
- If the lower or upper value are out of bounds.public static <S,E> Set<E> derivedSet(Set<S> storage, ObjectConverter<S,E> converter)
ObjectConverter.apply(Object)
method on the given converter.
Those conversions are repeated every time a Set
method is invoked; there is no cache.
Consequently, any change in the original set is immediately visible in the derived set,
and conversely.
The Set.add(E)
method is supported only if the given converter
is invertible.
An invertible converter is not mandatory for other Set
operations.
However contains
and remove
operations are likely to be faster if the inverse converter is available.
The derived set may contain fewer elements than the original set if some elements
are not convertible. Non-convertible elements are S values for which
converter.apply(S)
returns null
. As a consequence of this sentinel
value usage, the derived set can not contain null
elements.
The returned set can be serialized if the given set and converter are serializable. The returned set is not synchronized by itself, but is nevertheless thread-safe if the given set (including its iterator) and converter are thread-safe.
S
- The type of elements in the storage (original) set.E
- The type of elements in the derived set.storage
- The storage set containing the original elements, or null
.converter
- The converter from the elements in the storage set to the elements
in the derived set.storage
set containing all elements converted by the given
converter, or null
if storage
was null.ObjectConverters.derivedSet(Set, ObjectConverter)
public static <SK,SV,K,V> Map<K,V> derivedMap(Map<SK,SV> storage, ObjectConverter<SK,K> keyConverter, ObjectConverter<SV,V> valueConverter)
ObjectConverter.apply(Object)
method on the given converters.
Those conversions are repeated every time a Map
method is invoked; there is no cache.
Consequently, any change in the original map is immediately visible in the derived map,
and conversely.
The Map.put(K,V)
method is supported only if the given
converters are invertible.
An invertible converter is not mandatory for other Map
operations.
However some of them are likely to be faster if the inverse converters are available.
The derived map may contain fewer entries than the original map if some keys
are not convertible. Non-convertible keys are K values for which
keyConverter.apply(K)
returns null
. As a consequence of this sentinel
value usage, the derived map can not contain null
keys.
It may contain null
values however.
The returned map can be serialized if the given map and converters are serializable. The returned map is not thread-safe.
SK
- The type of keys in the storage map.SV
- The type of values in the storage map.K
- The type of keys in the derived map.V
- The type of values in the derived map.storage
- The storage map containing the original entries, or null
.keyConverter
- The converter from the keys in the storage map to the keys in the derived map.valueConverter
- The converter from the values in the storage map to the values in the derived map.storage
map containing all entries converted by the given
converters, or null
if storage
was null.ObjectConverters.derivedMap(Map, ObjectConverter, ObjectConverter)
,
ObjectConverters.derivedKeys(Map, ObjectConverter, Class)
,
ObjectConverters.derivedValues(Map, Class, ObjectConverter)
public static int hashMapCapacity(int count)
HashMap
constructor for holding the given number of elements. This method computes the capacity
for the default load factor, which is 0.75.
The same calculation can be used for LinkedHashMap
and
HashSet
as well, which are built on top of HashMap
.
However it is not needed for IdentityHashMap
.
count
- The number of elements to be put into the hash map or hash set.Copyright © 2010–2013 The Apache Software Foundation. All rights reserved.