|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.dtolabs.utils.Mapper
public abstract class Mapper
Some basic functions from other languages just don't exist in java :)
This utility class provides simple methods
for "mapping" a set of objects to a new set of objects, based on a provided implementation.
The mapping will be
one to one, although if the result of a particular mapping returns null, the result can be discarded (default) rather
than included.
Each static utility method which by default ignores null results has an equivalent method which
has a boolean parameter indicating whether nulls should be discarded or not.
The other use for this utility
class is to perform the same action on all items of a collection, where no result is necessarily returned or
important.
//create a collection of Strings from some objects Collection c = Mapper.map(new Mapper(){ public Object map(Object o){ return o.toString(); }, someIterator); //simply perform some action and discard the results Mapper.map(new Mapper(){ public Object map(Object o){ ((MyTaskThread)o).performTask(); return null; }, someIterator);
Field Summary | |
---|---|
static Mapper |
identity
Maps an object to itself. |
static Mapper |
toString
Maps an object to the results of the toString() method. |
Constructor Summary | |
---|---|
Mapper()
|
Method Summary | |
---|---|
java.util.Collection |
apply(java.util.Collection c)
|
java.util.Collection |
apply(java.util.Enumeration en)
|
java.util.Collection |
apply(java.util.Iterator i)
|
java.util.Collection |
apply(java.lang.Object[] arr)
|
java.util.Map |
applyToEntries(java.util.Map map)
|
java.util.Map |
applyToKeys(java.util.Map map)
|
java.util.Map |
applyToValues(java.util.Map map)
|
static java.util.Collection |
beanMap(java.lang.String property,
java.util.Collection c)
Map dynamically using a bean property name. |
static java.util.Collection |
beanMap(java.lang.String property,
java.util.Collection c,
boolean includeNull)
Map dynamically using a bean property name. |
static java.util.Collection |
beanMap(java.lang.String property,
java.util.Iterator i)
Map dynamically using a bean property name. |
static java.util.Collection |
beanMap(java.lang.String property,
java.util.Iterator i,
boolean includeNull)
Map dynamically using a bean property name. |
static java.util.Collection |
beanMap(java.lang.String property,
java.lang.Object[] objs)
Map dynamically using a bean property name. |
static Mapper |
beanMapper(java.lang.String property)
Create a mapper for bean properties |
static java.util.Comparator |
comparator(Mapper mapper)
Create a Comparator which compares two objects by first mapping each object with a mapper and then comparing the result objects. |
static java.util.Comparator |
comparator(Mapper mapper,
boolean reverse)
see comparator(Mapper) |
Mapper |
concat(Mapper mapper)
Concatenate another mapper onto this one. |
static Mapper |
concat(Mapper[] arr)
Concatenate more than two Mappers. |
static Mapper |
concat(Mapper first,
Mapper second)
Concatenate two Mappers. |
static Mapper |
filterMapper(org.apache.commons.collections.Predicate pred)
Return a mapper than maps an object to itself if the predicate evaluates to true, and to null otherwise. |
java.util.Map |
makeMap(java.util.Collection c)
|
java.util.Map |
makeMap(java.util.Enumeration en)
|
java.util.Map |
makeMap(java.util.Iterator i)
|
static java.util.Map |
makeMap(Mapper mapper,
java.util.Collection c)
Create a new Map by using the collection objects as keys, and the mapping result as values. |
static java.util.Map |
makeMap(Mapper mapper,
java.util.Collection c,
boolean includeNull)
Create a new Map by using the collection objects as keys, and the mapping result as values. |
static java.util.Map |
makeMap(Mapper mapper,
java.util.Enumeration en)
Create a new Map by using the enumeration objects as keys, and the mapping result as values. |
static java.util.Map |
makeMap(Mapper mapper,
java.util.Enumeration en,
boolean includeNull)
Create a new Map by using the enumeration objects as keys, and the mapping result as values. |
static java.util.Map |
makeMap(Mapper mapper,
java.util.Iterator i)
Create a new Map by using the iterator objects as keys, and the mapping result as values. |
static java.util.Map |
makeMap(Mapper mapper,
java.util.Iterator i,
boolean includeNull)
Create a new Map by using the iterator objects as keys, and the mapping result as values. |
static java.util.Map |
makeMap(Mapper mapper,
java.lang.Object[] a)
Create a new Map by using the array objects as keys, and the mapping result as values. |
static java.util.Map |
makeMap(Mapper mapper,
java.lang.Object[] a,
boolean includeNull)
Create a new Map by using the array objects as keys, and the mapping result as values. |
java.util.Map |
makeMap(java.lang.Object[] a)
|
static java.util.Collection |
map(Mapper mapper,
java.util.Collection c)
Return the results of mapping all objects with the mapper. |
static java.util.Collection |
map(Mapper mapper,
java.util.Collection c,
boolean allowNull)
Return the results of mapping all objects with the mapper. |
static java.util.Collection |
map(Mapper mapper,
java.util.Enumeration en)
Return the results of mapping all objects with the mapper. |
static java.util.Collection |
map(Mapper mapper,
java.util.Enumeration en,
boolean allowNull)
Return the results of mapping all objects with the mapper. |
static java.util.Collection |
map(Mapper mapper,
java.util.Iterator i)
Return the results of mapping all objects with the mapper. |
static java.util.Collection |
map(Mapper mapper,
java.util.Iterator i,
boolean includeNull)
Return the results of mapping all objects with the mapper. |
static java.util.Collection |
map(Mapper mapper,
java.lang.Object o)
Trivial case of a single object. |
static java.util.Collection |
map(Mapper mapper,
java.lang.Object[] arr)
Return the results of mapping all objects with the mapper. |
static java.util.Collection |
map(Mapper mapper,
java.lang.Object[] arr,
boolean allowNull)
Return the results of mapping all objects with the mapper. |
abstract java.lang.Object |
map(java.lang.Object a)
Map one object to another. |
static java.util.Map |
mapEntries(Mapper mapper,
java.util.Map map)
Create a new Map by mapping all values from the original map and mapping all keys. |
static java.util.Map |
mapEntries(Mapper mapper,
java.util.Map map,
boolean includeNull)
Create a new Map by mapping all values from the original map, and mapping all keys. |
static java.util.Map |
mapKeys(Mapper mapper,
java.util.Map map)
Create a new Map by mapping all keys from the original map and maintaining the original value. |
static java.util.Map |
mapKeys(Mapper mapper,
java.util.Map map,
boolean allowNull)
Create a new Map by mapping all keys from the original map and maintaining the original value. |
static Mapper |
mapMapper(java.util.Map map)
Return a mapper that uses a Map instance, and maps keys of that Map to the values. |
static java.util.Map |
mapValues(Mapper mapper,
java.util.Map map)
Create a new Map by mapping all values from the original map and maintaining the original key. |
static java.util.Map |
mapValues(Mapper mapper,
java.util.Map map,
boolean includeNull)
Create a new Map by mapping all values from the original map and maintaining the original key. |
static Mapper |
uniqueMapper()
Return a mapper that maps unique objects to themselves, and duplicates to null. |
static java.util.Map |
zip(java.util.Iterator keys,
java.util.Iterator values)
Creates a map where the object at index N from the first Iterator is the key for the object at index N of the second Iterator. |
static java.util.Map |
zip(java.util.Iterator keys,
java.util.Iterator values,
boolean includeNull)
Creates a map where the object at index N from the first Iterator is the key for the object at index N of the second Iterator. |
static java.util.Map |
zip(java.util.List keys,
java.util.List values)
Creates a map where the object at index N from the first List is the key for the object at index N of the second List. |
static java.util.Map |
zip(java.util.List keys,
java.util.List values,
boolean includeNull)
Creates a map where the object at index N from the first List is the key for the object at index N of the second List. |
static java.util.Map |
zip(java.lang.Object[] keys,
java.lang.Object[] values)
Creates a map where the object at index N from the first Iterator is the key for the object at index N of the second Iterator. |
static java.util.Map |
zip(java.lang.Object[] keys,
java.lang.Object[] values,
boolean includeNull)
Creates a map where the object at index N from the first Iterator is the key for the object at index N of the second Iterator. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Mapper toString
public static final Mapper identity
Constructor Detail |
---|
public Mapper()
Method Detail |
---|
public static java.util.Comparator comparator(Mapper mapper)
Comparable
interface, otherwise the two
objects cannot be compared. (All of the java.lang.* wrapper types will work.)
mapper
-
public static java.util.Comparator comparator(Mapper mapper, boolean reverse)
comparator(Mapper)
reverse
- if true, reverse the order of comparison
public Mapper concat(Mapper mapper)
mapper
-
public static Mapper concat(Mapper[] arr)
arr
- array of Mapper instances. a null array will return the identity Mapper.
public static Mapper concat(Mapper first, Mapper second)
first
- first mapper to applysecond
- second mapper to apply
public static java.util.Map zip(java.util.List keys, java.util.List values)
keys
- List of keysvalues
- List of valuespublic static java.util.Map zip(java.util.List keys, java.util.List values, boolean includeNull)
keys
- List of keysvalues
- List of valuesincludeNull
- allow null values and keyspublic static java.util.Map zip(java.lang.Object[] keys, java.lang.Object[] values)
keys
- array of keysvalues
- array of valuespublic static java.util.Map zip(java.lang.Object[] keys, java.lang.Object[] values, boolean includeNull)
keys
- array of keysvalues
- array of valuesincludeNull
- allow null values and keyspublic static java.util.Map zip(java.util.Iterator keys, java.util.Iterator values)
keys
- Iterator of keysvalues
- Iterator of valuespublic static java.util.Map zip(java.util.Iterator keys, java.util.Iterator values, boolean includeNull)
keys
- Iterator of keysvalues
- Iterator of valuesincludeNull
- allow null values and keyspublic static java.util.Collection map(Mapper mapper, java.lang.Object o)
mapper
- an Mappero
- an Object
public static java.util.Collection map(Mapper mapper, java.util.Collection c)
mapper
- an Mapperc
- a Collection
public java.util.Collection apply(java.util.Collection c)
public static java.util.Collection map(Mapper mapper, java.util.Collection c, boolean allowNull)
mapper
- an Mapperc
- a CollectionallowNull
- allow null values
public static java.util.Collection map(Mapper mapper, java.lang.Object[] arr)
mapper
- an Mapperarr
- an array of objects
public java.util.Collection apply(java.lang.Object[] arr)
public static java.util.Collection map(Mapper mapper, java.lang.Object[] arr, boolean allowNull)
mapper
- an Mapperarr
- an array of objectsallowNull
- allow null values
public static java.util.Collection map(Mapper mapper, java.util.Enumeration en)
mapper
- an Mapperen
- an Enumeration
public java.util.Collection apply(java.util.Enumeration en)
public static java.util.Collection map(Mapper mapper, java.util.Enumeration en, boolean allowNull)
mapper
- an Mapperen
- an EnumerationallowNull
- allow null values
public static java.util.Map mapKeys(Mapper mapper, java.util.Map map)
mapper
- a Mapper to map the keysmap
- a Map
public java.util.Map applyToKeys(java.util.Map map)
public static java.util.Map mapKeys(Mapper mapper, java.util.Map map, boolean allowNull)
mapper
- a Mapper to map the keysmap
- a MapallowNull
- allow null values
public static java.util.Map mapValues(Mapper mapper, java.util.Map map)
mapper
- a Mapper to map the valuesmap
- an Map
public java.util.Map applyToValues(java.util.Map map)
public static java.util.Map mapValues(Mapper mapper, java.util.Map map, boolean includeNull)
mapper
- a Mapper to map the valuesmap
- an Map
public static java.util.Map mapEntries(Mapper mapper, java.util.Map map)
mapper
- a Mapper to map the values and keysmap
- a Map
public java.util.Map applyToEntries(java.util.Map map)
public static java.util.Map mapEntries(Mapper mapper, java.util.Map map, boolean includeNull)
mapper
- a Mapper to map both values and keysmap
- Map inputincludeNull
- if true, allow null as either key or value after mapping
public static java.util.Map makeMap(Mapper mapper, java.lang.Object[] a)
mapper
- a Mapper to map the valuesa
- array of items
public java.util.Map makeMap(java.lang.Object[] a)
public static java.util.Map makeMap(Mapper mapper, java.lang.Object[] a, boolean includeNull)
mapper
- a Mapper to map the valuesa
- array of items
public static java.util.Map makeMap(Mapper mapper, java.util.Collection c)
mapper
- a Mapper to map the valuesc
- Collection of items
public java.util.Map makeMap(java.util.Collection c)
public static java.util.Map makeMap(Mapper mapper, java.util.Collection c, boolean includeNull)
mapper
- a Mapper to map the valuesc
- Collection of items
public static java.util.Map makeMap(Mapper mapper, java.util.Iterator i)
mapper
- a Mapper to map the valuesi
- Iterator
public java.util.Map makeMap(java.util.Iterator i)
public static java.util.Map makeMap(Mapper mapper, java.util.Iterator i, boolean includeNull)
mapper
- a Mapper to map the valuesi
- Iterator
public static java.util.Map makeMap(Mapper mapper, java.util.Enumeration en)
mapper
- a Mapper to map the valuesen
- Enumeration
public java.util.Map makeMap(java.util.Enumeration en)
public static java.util.Map makeMap(Mapper mapper, java.util.Enumeration en, boolean includeNull)
mapper
- a Mapper to map the valuesen
- Enumeration
public static java.util.Collection map(Mapper mapper, java.util.Iterator i)
mapper
- an Mapperi
- an Iterator
public java.util.Collection apply(java.util.Iterator i)
public static java.util.Collection map(Mapper mapper, java.util.Iterator i, boolean includeNull)
mapper
- an Mapperi
- an IteratorincludeNull
- specify whether nulls are included
public static java.util.Collection beanMap(java.lang.String property, java.lang.Object[] objs)
property
- the name of a bean propertyobjs
- an array of objects
java.lang.ClassCastException
- if there is an error invoking the method on any object.public static java.util.Collection beanMap(java.lang.String property, java.util.Iterator i)
property
- the name of a bean propertyi
- an iterator of objects
java.lang.ClassCastException
- if there is an error invoking the method on any object.public static java.util.Collection beanMap(java.lang.String property, java.util.Collection c, boolean includeNull)
property
- the name of a bean propertyc
- an Collection of objectsincludeNull
- true to include null results in the response
java.lang.ClassCastException
- if there is an error invoking the method on any object.public static java.util.Collection beanMap(java.lang.String property, java.util.Collection c)
property
- the name of a bean propertyc
- a collection of objects
java.lang.ClassCastException
- if there is an error invoking the method on any object.public static Mapper beanMapper(java.lang.String property)
property
- name of the bean property
public static java.util.Collection beanMap(java.lang.String property, java.util.Iterator i, boolean includeNull)
property
- the name of a bean propertyi
- an iterator of objectsincludeNull
- true to include null results in the response
java.lang.ClassCastException
- if there is an error invoking the method on any object.public static Mapper filterMapper(org.apache.commons.collections.Predicate pred)
pred
-
public static Mapper uniqueMapper()
public static Mapper mapMapper(java.util.Map map)
Map
instance, and maps keys of that Map to the values.
map
- a Map
instance
public abstract java.lang.Object map(java.lang.Object a)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |