public final class ObjectConverters extends Static
ObjectConverter
instances or collection views based on converters.
Converters are created by the following methods:
Converters can be used for creating derived collections by the following methods:
derivedSet(Set, ObjectConverter)
derivedMap(Map, ObjectConverter, ObjectConverter)
derivedKeys(Map, ObjectConverter, Class)
derivedValues(Map, Class, ObjectConverter)
S
to type T
,
where the types are unknown at compile-time. Note that the converter is obtained only once
before to be applied to every elements in the loop.
Class<S> sourceType = ... Class<T> targetType = ... Collection<S> sources = ...; Collection<T> targets = ...; ObjectConverter<S,T> converter = ObjectConverters.find(sourceType, targetType); for (S source : sources) { targets.add(converter.apply(source)); }
ObjectConverter
Defined in the sis-utility
module
Modifier and Type | Method and Description |
---|---|
static <T> T |
convert(Object value,
Class<T> target)
Converts the given value to the given type.
|
static <SK,K,V> Map<K,V> |
derivedKeys(Map<SK,V> storage,
ObjectConverter<SK,K> keyConverter,
Class<V> valueType)
Returns a map whose keys are derived on-the-fly from the given map.
|
static <SK,SV,K,V> |
derivedMap(Map<SK,SV> storage,
ObjectConverter<SK,K> keyConverter,
ObjectConverter<SV,V> valueConverter)
Returns a map 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 <K,SV,V> Map<K,V> |
derivedValues(Map<K,SV> storage,
Class<K> keyType,
ObjectConverter<SV,V> valueConverter)
Returns a map whose values are derived on-the-fly from the given map.
|
static <S,T> ObjectConverter<? super S,? extends T> |
find(Class<S> source,
Class<T> target)
Returns a converter for the specified source and target classes.
|
static <T> ObjectConverter<T,T> |
identity(Class<T> type)
Returns an identity converter for objects of the given type.
|
public static <T> ObjectConverter<T,T> identity(Class<T> type)
T
- The object type.type
- The object type.public static <S,T> ObjectConverter<? super S,? extends T> find(Class<S> source, Class<T> target) throws UnconvertibleObjectException
S
- The source class.T
- The target class.source
- The source class.target
- The target class, or Object.class
for any.UnconvertibleObjectException
- if no converter is found.public static <T> T convert(Object value, Class<T> target) throws UnconvertibleObjectException
find(Class, Class)
instead in order to reuse the same converter
for all values to convert.T
- The type of the target
class.value
- The value to convert, or null
.target
- The target class.null
).UnconvertibleObjectException
- if the given value can not be converted.public static <S,E> Set<E> derivedSet(Set<S> storage, ObjectConverter<S,E> converter)
ObjectConverter.apply(Object)
method on the given converter.
This convenience method delegates to
Containers.derivedSet(…)
.
See the javadoc of the above method for more information.
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.Containers.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.
This convenience method delegates to
Containers.derivedMap(…)
.
See the javadoc of the above method for more information.
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.Containers.derivedMap(Map, ObjectConverter, ObjectConverter)
public static <SK,K,V> Map<K,V> derivedKeys(Map<SK,V> storage, ObjectConverter<SK,K> keyConverter, Class<V> valueType)
ObjectConverter.apply(Object)
method on the given converter.
This convenience method delegates to
Containers.derivedMap(…)
.
See the javadoc of the above method for more information.
SK
- The type of keys in the storage map.K
- The type of keys in the derived map.V
- The type of values in the storage and 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.valueType
- The type of values in the storage and derived map.storage
map containing all entries with the keys converted
by the given converter, or null
if storage
was null.Containers.derivedMap(Map, ObjectConverter, ObjectConverter)
public static <K,SV,V> Map<K,V> derivedValues(Map<K,SV> storage, Class<K> keyType, ObjectConverter<SV,V> valueConverter)
ObjectConverter.apply(Object)
method on the given converter.
This convenience method delegates to
Containers.derivedMap(…)
.
See the javadoc of the above method for more information.
K
- The type of keys in the storage and derived map.SV
- The type of values in the storage map.V
- The type of values in the derived map.storage
- The storage map containing the original entries, or null
.keyType
- The type of keys in the storage and derived map.valueConverter
- The converter from the values in the storage map to the values in the derived map.storage
map containing all entries with the values converted
by the given converter, or null
if storage
was null.Containers.derivedMap(Map, ObjectConverter, ObjectConverter)
Copyright © 2010–2015 The Apache Software Foundation. All rights reserved.