Class Mapper


  • public abstract class Mapper
    extends java.lang.Object
    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.
    Example usage:
     //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

      Fields 
      Modifier and Type Field Description
      static Mapper identity
      Maps an object to itself.
      static Mapper toString
      Maps an object to the results of the toString() method.
    • Constructor Summary

      Constructors 
      Constructor Description
      Mapper()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Collection apply​(java.lang.Object[] arr)  
      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.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.lang.Object[] objs)
      Map dynamically using a bean property name.
      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 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)
      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.
      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.
      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.
      java.util.Map makeMap​(java.lang.Object[] a)  
      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.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.
      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.
      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()  
      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.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • toString

        public static final Mapper toString
        Maps an object to the results of the toString() method.
      • identity

        public static final Mapper identity
        Maps an object to itself.
    • Constructor Detail

      • Mapper

        public Mapper()
    • Method Detail

      • comparator

        public 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. Note: The Mapper instance must map every object into one that implements the Comparable interface, otherwise the two objects cannot be compared. (All of the java.lang.* wrapper types will work.)
        Parameters:
        mapper - mapper
        Returns:
        comparator
      • comparator

        public static java.util.Comparator comparator​(Mapper mapper,
                                                      boolean reverse)
        Parameters:
        mapper - mapper
        reverse - if true, reverse the order of comparison
        Returns:
        comparator
      • concat

        public Mapper concat​(Mapper mapper)
        Concatenate another mapper onto this one.
        Parameters:
        mapper - mapper
        Returns:
        mapper
      • concat

        public static Mapper concat​(Mapper[] arr)
        Concatenate more than two Mappers.
        Parameters:
        arr - array of Mapper instances. a null array will return the identity Mapper.
        Returns:
        single Mapper concatenating the Mappers.
      • concat

        public static Mapper concat​(Mapper first,
                                    Mapper second)
        Concatenate two Mappers.
        Parameters:
        first - first mapper to apply
        second - second mapper to apply
        Returns:
        mapper
      • zip

        public 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.
        By default discards both key and value if either one is null.
        Parameters:
        keys - List of keys
        values - List of values
        Returns:
        map
      • zip

        public 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. If either List is shorter than the other, the resulting Map will have only as many keys as are in the smallest of the two Lists.
        If includeNull is true, both keys and values of null are allowed. Note that if more than one key is null, previous entries will be overwritten.
        Parameters:
        keys - List of keys
        values - List of values
        includeNull - allow null values and keys
        Returns:
        map
      • zip

        public 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.
        By default discards both key and value if either one is null.
        Parameters:
        keys - array of keys
        values - array of values
        Returns:
        map
      • zip

        public 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. If either Iterator is shorter than the other, the resulting Map will have only as many keys as are in the smallest of the two Iterator.
        If includeNull is true, both keys and values of null are allowed. Note that if more than one key is null, previous entries will be overwritten.
        Parameters:
        keys - array of keys
        values - array of values
        includeNull - allow null values and keys
        Returns:
        map
      • zip

        public 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.
        By default discards both key and value if either one is null.
        Parameters:
        keys - Iterator of keys
        values - Iterator of values
        Returns:
        map
      • zip

        public 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. If either Iterator is shorter than the other, the resulting Map will have only as many keys as are in the smallest of the two Iterator.
        If includeNull is true, both keys and values of null are allowed. Note that if more than one key is null, previous entries will be overwritten.
        Parameters:
        keys - Iterator of keys
        values - Iterator of values
        includeNull - allow null values and keys
        Returns:
        map
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.lang.Object o)
        Trivial case of a single object.
        Parameters:
        mapper - an Mapper
        o - an Object
        Returns:
        a Collection of the results.
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.util.Collection c)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        c - a Collection
        Returns:
        a Collection of the results.
      • apply

        public java.util.Collection apply​(java.util.Collection c)
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.util.Collection c,
                                               boolean allowNull)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        c - a Collection
        allowNull - allow null values
        Returns:
        a Collection of the results.
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.lang.Object[] arr)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        arr - an array of objects
        Returns:
        a Collection of the results.
      • apply

        public java.util.Collection apply​(java.lang.Object[] arr)
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.lang.Object[] arr,
                                               boolean allowNull)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        arr - an array of objects
        allowNull - allow null values
        Returns:
        a Collection of the results.
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.util.Enumeration en)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        en - an Enumeration
        Returns:
        a Collection of the results.
      • apply

        public java.util.Collection apply​(java.util.Enumeration en)
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.util.Enumeration en,
                                               boolean allowNull)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        en - an Enumeration
        allowNull - allow null values
        Returns:
        a Collection of the results.
      • mapKeys

        public 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.
        Parameters:
        mapper - a Mapper to map the keys
        map - a Map
        Returns:
        a new Map with keys mapped
      • applyToKeys

        public java.util.Map applyToKeys​(java.util.Map map)
      • mapKeys

        public 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.
        Parameters:
        mapper - a Mapper to map the keys
        map - a Map
        allowNull - allow null values
        Returns:
        a new Map with keys mapped
      • mapValues

        public 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.
        Parameters:
        mapper - a Mapper to map the values
        map - an Map
        Returns:
        a new Map with values mapped
      • applyToValues

        public java.util.Map applyToValues​(java.util.Map map)
      • mapValues

        public 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.
        Parameters:
        mapper - a Mapper to map the values
        map - an Map
        includeNull - true to include null
        Returns:
        a new Map with values mapped
      • mapEntries

        public 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.
        Parameters:
        mapper - a Mapper to map the values and keys
        map - a Map
        Returns:
        a new Map with values mapped
      • applyToEntries

        public java.util.Map applyToEntries​(java.util.Map map)
      • mapEntries

        public 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.
        Parameters:
        mapper - a Mapper to map both values and keys
        map - Map input
        includeNull - if true, allow null as either key or value after mapping
        Returns:
        a new Map with both keys and values mapped using the Mapper
      • makeMap

        public 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. Discard keys which return null values from the mapper.
        Parameters:
        mapper - a Mapper to map the values
        a - array of items
        Returns:
        a new Map with values mapped
      • makeMap

        public java.util.Map makeMap​(java.lang.Object[] a)
      • makeMap

        public 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.
        Parameters:
        mapper - a Mapper to map the values
        a - array of items
        includeNull - true to include null
        Returns:
        a new Map with values mapped
      • makeMap

        public 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. Discard keys which return null values from the mapper.
        Parameters:
        mapper - a Mapper to map the values
        c - Collection of items
        Returns:
        a new Map with values mapped
      • makeMap

        public java.util.Map makeMap​(java.util.Collection c)
      • makeMap

        public 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.
        Parameters:
        mapper - a Mapper to map the values
        c - Collection of items
        includeNull - true to include null
        Returns:
        a new Map with values mapped
      • makeMap

        public 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. Discard keys which return null values from the mapper.
        Parameters:
        mapper - a Mapper to map the values
        i - Iterator
        Returns:
        a new Map with values mapped
      • makeMap

        public java.util.Map makeMap​(java.util.Iterator i)
      • makeMap

        public 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.
        Parameters:
        mapper - a Mapper to map the values
        i - Iterator
        includeNull - true to include null
        Returns:
        a new Map with values mapped
      • makeMap

        public 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. Discard keys which return null values from the mapper.
        Parameters:
        mapper - a Mapper to map the values
        en - Enumeration
        Returns:
        a new Map with values mapped
      • makeMap

        public java.util.Map makeMap​(java.util.Enumeration en)
      • makeMap

        public 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.
        Parameters:
        mapper - a Mapper to map the values
        en - Enumeration
        includeNull - true to include null
        Returns:
        a new Map with values mapped
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.util.Iterator i)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        i - an Iterator
        Returns:
        a Collection of the results.
      • apply

        public java.util.Collection apply​(java.util.Iterator i)
      • map

        public static java.util.Collection map​(Mapper mapper,
                                               java.util.Iterator i,
                                               boolean includeNull)
        Return the results of mapping all objects with the mapper.
        Parameters:
        mapper - an Mapper
        i - an Iterator
        includeNull - specify whether nulls are included
        Returns:
        a Collection of the results.
      • beanMap

        public static java.util.Collection beanMap​(java.lang.String property,
                                                   java.lang.Object[] objs)
        Map dynamically using a bean property name.
        Parameters:
        property - the name of a bean property
        objs - an array of objects
        Returns:
        collection
        Throws:
        java.lang.ClassCastException - if there is an error invoking the method on any object.
      • beanMap

        public static java.util.Collection beanMap​(java.lang.String property,
                                                   java.util.Iterator i)
        Map dynamically using a bean property name.
        Parameters:
        property - the name of a bean property
        i - an iterator of objects
        Returns:
        collection
        Throws:
        java.lang.ClassCastException - if there is an error invoking the method on any object.
      • beanMap

        public static java.util.Collection beanMap​(java.lang.String property,
                                                   java.util.Collection c,
                                                   boolean includeNull)
        Map dynamically using a bean property name.
        Parameters:
        property - the name of a bean property
        c - an Collection of objects
        includeNull - true to include null results in the response
        Returns:
        collection
        Throws:
        java.lang.ClassCastException - if there is an error invoking the method on any object.
      • beanMap

        public static java.util.Collection beanMap​(java.lang.String property,
                                                   java.util.Collection c)
        Map dynamically using a bean property name.
        Parameters:
        property - the name of a bean property
        c - a collection of objects
        Returns:
        collection
        Throws:
        java.lang.ClassCastException - if there is an error invoking the method on any object.
      • beanMapper

        public static Mapper beanMapper​(java.lang.String property)
        Create a mapper for bean properties
        Parameters:
        property - name of the bean property
        Returns:
        mapper
      • beanMap

        public static java.util.Collection beanMap​(java.lang.String property,
                                                   java.util.Iterator i,
                                                   boolean includeNull)
        Map dynamically using a bean property name.
        Parameters:
        property - the name of a bean property
        i - an iterator of objects
        includeNull - true to include null results in the response
        Returns:
        collection
        Throws:
        java.lang.ClassCastException - if there is an error invoking the method on any object.
      • filterMapper

        public 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.
        Parameters:
        pred - predicate
        Returns:
        mapper
      • uniqueMapper

        public static Mapper uniqueMapper()
        Returns:
        a mapper that maps unique objects to themselves, and duplicates to null.
      • mapMapper

        public static Mapper mapMapper​(java.util.Map map)
        Return a mapper that uses a Map instance, and maps keys of that Map to the values.
        Parameters:
        map - a Map instance
        Returns:
        a Mapper instance
      • map

        public abstract java.lang.Object map​(java.lang.Object a)
        Map one object to another.
        Parameters:
        a - object
        Returns:
        the new object.