Static methods for creating
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)
Example
the following code converts instances in a collection from typeS
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));
}
- Since:
- 0.3
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
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>
Map<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> Returns a converter for the specified source and target classes.static <T> ObjectConverter
<T, T> Returns an identity converter for objects of the given type.
-
Method Details
-
identity
Returns an identity converter for objects of the given type.- Type Parameters:
T
- the object type.- Parameters:
type
- the object type.- Returns:
- an identity converter for objects of the given type.
-
find
public static <S,T> ObjectConverter<? super S,? extends T> find(Class<S> source, Class<T> target) throws UnconvertibleObjectException Returns a converter for the specified source and target classes.- Type Parameters:
S
- the source class.T
- the target class.- Parameters:
source
- the source class.target
- the target class, orObject.class
for any.- Returns:
- the converter from the specified source class to the target class.
- Throws:
UnconvertibleObjectException
- if no converter is found.
-
convert
Converts the given value to the given type. This convenience method shall be used only for rare conversions. For converting many instances between the same source and target classes, consider invokingfind(Class, Class)
instead in order to reuse the same converter for all values to convert.- Type Parameters:
T
- the type of thetarget
class.- Parameters:
value
- the value to convert, ornull
.target
- the target class.- Returns:
- the converted value (may be
null
). - Throws:
UnconvertibleObjectException
- if the given value cannot be converted.
-
derivedSet
Returns a set whose elements are derived on-the-fly from the given set. Conversions from the original elements to the derived elements are performed when needed by invoking theObjectConverter.apply(Object)
method on the given converter.This convenience method delegates to
Containers.derivedSet(…)
. See the javadoc of the above method for more information.- Type Parameters:
S
- the type of elements in the storage (original) set.E
- the type of elements in the derived set.- Parameters:
storage
- the storage set containing the original elements, ornull
.converter
- the converter from the elements in the storage set to the elements in the derived set.- Returns:
- a view over the
storage
set containing all elements converted by the given converter, ornull
ifstorage
was null. - See Also:
-
derivedMap
public static <SK,SV, Map<K,K, V> 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. Conversions from the original entries to the derived entries are performed when needed by invoking theObjectConverter.apply(Object)
method on the given converters.This convenience method delegates to
Containers.derivedMap(…)
. See the javadoc of the above method for more information.- Type Parameters:
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.- Parameters:
storage
- the storage map containing the original entries, ornull
.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.- Returns:
- a view over the
storage
map containing all entries converted by the given converters, ornull
ifstorage
was null. - See Also:
-
derivedKeys
public static <SK,K, Map<K,V> 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. Conversions from the original keys to the derived keys are performed when needed by invoking theObjectConverter.apply(Object)
method on the given converter.This convenience method delegates to
Containers.derivedMap(…)
. See the javadoc of the above method for more information.- Type Parameters:
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.- Parameters:
storage
- the storage map containing the original entries, ornull
.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.- Returns:
- a view over the
storage
map containing all entries with the keys converted by the given converter, ornull
ifstorage
was null. - See Also:
-
derivedValues
public static <K,SV, Map<K,V> 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. Conversions from the original values to the derived values are performed when needed by invoking theObjectConverter.apply(Object)
method on the given converter.This convenience method delegates to
Containers.derivedMap(…)
. See the javadoc of the above method for more information.- Type Parameters:
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.- Parameters:
storage
- the storage map containing the original entries, ornull
.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.- Returns:
- a view over the
storage
map containing all entries with the values converted by the given converter, ornull
ifstorage
was null. - See Also:
-