public static class Expressive.Transformers extends Object
Provides convenience access to ETransformers that are of common use.
Java type and collection manipulation in general falls into a very few small categories, such as reading properties of a homogeneous collection of types, reordering of collections or changing the representation of a data set (such as from list to a lookup map), or doing lookups in maps/dictionaries.
The ETransformers provided by this class attempt to meet the needs of many of these typical use cases:
usingLookup(Map) provides a simple map lookup through a uniform interfacetoProperty(String) and variants provide a bean to bean property transformation. Used in conjunction with CollectionTransformer this provides a succinct and
naturally expressed way of extracting values from standard java DTO.toBeanLookup(String) and variants provides a transformer which will create a one-to-many lookup map based on a bean propertytoKeyBeanLookup(String) and variants provides a transformer which will create a one-to-one lookup map based on a bean propertytransformAllUsing(ETransformer) provides a transformer capable of transforming an entire collectionCollectionTransformer| Modifier and Type | Method and Description |
|---|---|
static <From,To> ETransformer<From,To> |
cast(Class<To> type)
Returns an
ETransformer that will cast inputs to the specified type. |
static <EnumType extends Enum<EnumType>> |
fromEnum(Class<EnumType> type)
Given an
Enum type returns an ETransformer. |
static <T> ETransformer<T,String> |
stringify()
Returns an
ETransformer which will transform a given object to a string using Object.toString(). |
static <Bean,Property> |
toBeanLookup(String propertyName)
Given a the name of a javabean property returns an
ETransformer. |
static <Bean,Property> |
toBeanLookup(String propertyName,
Class<Bean> clazz)
Given a
Class and the name of a javabean property returns an ETransformer. |
static <EnumType extends Enum<EnumType>> |
toEnum(Class<EnumType> type)
Given an
Enum type returns an ETransformer. |
static <Bean,Property> |
toKeyBeanLookup(String propertyName)
Given a
Class and the name of a javabean property returns an ETransformer. |
static <Bean,Property> |
toKeyBeanLookup(String propertyName,
Class<Bean> clazz)
Given a the name of a javabean property returns an
ETransformer. |
static <Bean,Property> |
toProperty(String propertyName)
Given a the name of a javabean property returns an
ETransformer. |
static <Bean,Property> |
toProperty(String propertyName,
Class<Bean> clazz)
Given a
Class and the name of a javabean property returns an ETransformer. |
static <From,To> CollectionTransformer<From,To> |
transformAllUsing(ETransformer<From,To> transformer)
Creates a
CollectionTransformer for the given ETransformer. |
static <From,To> ETransformer<From,To> |
usingLookup(Map<From,To> lookup)
Given a lookup table in the form of a
Map returns an ETransformer. |
public static <From,To> ETransformer<From,To> usingLookup(Map<From,To> lookup)
Given a lookup table in the form of a Map returns an ETransformer. The returned ETransformer will provide a transformation to input objects by performing a lookup in
the given lookup map.
If ETransformer.from(Object) is invoked with a value which is not a key in the map, null will be returned.
public static <Bean,Property> ETransformer<Bean,Property> toProperty(String propertyName)
Given a the name of a javabean property returns an ETransformer. The returned ETransformer will provide a transformation to input javabean objects by returning the the value
of the identified property contained within the bean.
If a null object is passed to the resulting ETransformer, it will return null.
If an object which does not provide access to the named property is passed to the resulting ETransformer it will throw a RuntimeException.
propertyName - The name of the javabean property to readtoProperty(String, Class)public static <Bean,Property> ETransformer<Bean,Property> toProperty(String propertyName, Class<Bean> clazz)
Given a Class and the name of a javabean property returns an ETransformer. The returned ETransformer will provide a transformation to input javabean objects whose
type matches the given class by returning the the value of the identified property contained within the bean.
If a null object is passed to the resulting ETransformer, it will return null.
If an object which does not provide access to the named property is passed to the resulting ETransformer it will throw a RuntimeException.
propertyName - The name of the javabean property to readclazz - The Class type of javabeans which this transformer can
apply to.toProperty(String)public static <Bean,Property> ETransformer<Collection<Bean>,Map<Property,List<Bean>>> toBeanLookup(String propertyName)
Given a the name of a javabean property returns an ETransformer. The returned ETransformer will provide a transformation across a collection of input javabean objects by
returning a map which can be used as a lookup table. The lookup table maps from property value to the list of beans containing that value.
If a null collection is passed to the resulting ETransformer, it will return an empty map.
If any object in the collection provided to the resulting transformer does not provide access to the named property it will throw a RuntimeException.
If the given property is known to be unique across the set of input beans (for example an id, a pk, or a key) then toKeyBeanLookup(String) is a better transformer.
This transformer will retain the order (if any) of the given collection in both the resulting map and of individual beans in a mapped to a property value. That is, if the collection is ordered, the map keys will be ordered based on the order of the beans they are first matched in.
propertyName - toBeanLookup(String, Class)public static <Bean,Property> ETransformer<Collection<Bean>,Map<Property,List<Bean>>> toBeanLookup(String propertyName, Class<Bean> clazz)
Given a Class and the name of a javabean property returns an ETransformer. The returned ETransformer will provide a transformation across a collection of input
javabean objects of the given class by returning a map which can be used as a lookup table. The lookup table maps from property value to the list of beans containing that value.
If a null collection is passed to the resulting ETransformer, it will return an empty map.
If any object in the collection provided to the resulting transformer does not provide access to the named property it will throw a RuntimeException.
If the given property is known to be unique across the set of input beans (for example an id, a pk, or a key) then toKeyBeanLookup(String, Class) is a better transformer.
This transformer will retain the order (if any) of the given collection in both the resulting map and of individual beans in a mapped to a property value. That is, if the collection is ordered, the map keys will be ordered based on the order of the beans they are first matched in.
propertyName - clazz - toBeanLookup(String)public static <Bean,Property> ETransformer<Collection<Bean>,Map<Property,Bean>> toKeyBeanLookup(String propertyName, Class<Bean> clazz)
Given a the name of a javabean property returns an ETransformer. The returned ETransformer will provide a transformation across a collection of input javabean objects by
returning a map which can be used as a lookup table. The lookup table maps from property value to the bean containing that value.
If a null collection is passed to the resulting ETransformer, it will return an empty map.
If any object in the collection provided to the resulting transformer does not provide access to the named property it will throw a RuntimeException.
If the given property is known to be unique across the set of input beans (for example an id, a pk, or a key) then toKeyBeanLookup(String) is a better transformer.
If more than one bean contains the same value for the property (or more specifically they return true for their equals and hashcode), only the last entry (based on the iteration order of the given collection) will be present.
This transformer will retain the order (if any) of the given collection in both the resulting map and of individual beans in a mapped to a property value. That is, if the collection is ordered, the map keys will be ordered based on the order of the beans they are first matched in.
propertyName - toKeyBeanLookup(String, Class)public static <Bean,Property> ETransformer<Collection<Bean>,Map<Property,Bean>> toKeyBeanLookup(String propertyName)
Given a Class and the name of a javabean property returns an ETransformer. The returned ETransformer will provide a transformation across a collection of input
javabean objects of the given type by returning a map which can be used as a lookup table. The lookup table maps from property value to the bean containing that value.
If a null collection is passed to the resulting ETransformer, it will return an empty map.
If any object in the collection provided to the resulting transformer does not provide access to the named property it will throw a RuntimeException.
If the given property is known to be unique across the set of input beans (for example an id, a pk, or a key) then toKeyBeanLookup(String) is a better transformer.
If more than one bean contains the same value for the property (or more specifically they return true for their equals and hashcode), only the last entry (based on the iteration order of the given collection) will be present.
This transformer will retain the order (if any) of the given collection in both the resulting map and of individual beans in a mapped to a property value. That is, if the collection is ordered, the map keys will be ordered based on the order of the beans they are first matched in.
propertyName - toKeyBeanLookup(String)public static <From,To> CollectionTransformer<From,To> transformAllUsing(ETransformer<From,To> transformer)
Creates a CollectionTransformer for the given ETransformer.
CollectionTransformers use a transformer to transform a collection rather than an individual object.
transformer - public static <EnumType extends Enum<EnumType>> ETransformer<EnumType,String> fromEnum(Class<EnumType> type)
Given an Enum type returns an ETransformer. The returned ETransformer will provide a string representation of a given enum value using the Enum.name()
method.
If a null object is passed to the resulting ETransformer, it will return null.
type - The Class type of Enum which this transformer can apply to.ETransformer which transforms an enum to a stringtoEnum(Class)public static <EnumType extends Enum<EnumType>> ETransformer<String,EnumType> toEnum(Class<EnumType> type)
Given an Enum type returns an ETransformer. The returned ETransformer will provide an enum value given a string using Enum.valueOf(Class, String).
If a null object is passed to the resulting ETransformer or a string value which is not a valid enumeration , it will return null.
type - The Class type of Enum which this transformer can apply to.ETransformer which transforms a string to an enum value, or null if no matching enum value existsfromEnum(Class)public static <T> ETransformer<T,String> stringify()
Returns an ETransformer which will transform a given object to a string using Object.toString().
If a null object is passed to the resulting ETransformer, it will return null.
ETransformer which transforms an object to a stringpublic static <From,To> ETransformer<From,To> cast(Class<To> type)
Returns an ETransformer that will cast inputs to the specified type.
If the returned ETransformer cannot cast an input, it will return null.
ETransformer that will cast inputs to the specified typeCopyright © 2013 Atomic Leopard. All Rights Reserved.