ariba.util.core
Class Function<K>

java.lang.Object
  extended by ariba.util.core.Function<K>
Direct Known Subclasses:
Function.MethodFunction, RegexFunction

public abstract class Function<K>
extends java.lang.Object

Generic class that represents a function returning a type K.

Useful for generically splitting, mapping and collecting over collections. E.g. say you have a list of strings and you want to identify all of the strings in the collection that begin with the same three characters:

    final Function<String> substring = new Function<String>() {
        public String evaluate (Object... arguments) {
            String string = (String)arguments[0];
            return string.length() > 2 ? string.substring(0,3) : null;
        }
    };
    List<String> someBigListOfStrings = ...;
    Map<String,List<String>> result = substring.split(someBigListOfStrings);
    
result will hold a map mapping the first 3 characters to all the strings in the orginal list that begin with the same three characters.

Feel free to add more methods to this class. We could use a int find(Collection<V> list, K k) method returning the index of the V in the list that for which the application of the Function yields k. Similarly we could use a collectWhere().


Nested Class Summary
static class Function.MethodFunction<T>
          Convenience nested class that adapts a Method into a Function
 
Field Summary
static Function Identity
           
 
Constructor Summary
Function()
           
 
Method Summary
<V> java.util.List<K>
collect(java.lang.Iterable<V> values)
          Iterates over values applying this to each instance of V and adding the results of the evaluations to the returned list.
<V> java.util.List<K>
collect(java.lang.Iterable<V> values, boolean includeNulls)
          Iterates over values applying this to each instance of V and adding the results of the evaluations to the returned list.
<V> void
collectInto(java.lang.Iterable<V> values, java.util.Collection<K> collector, boolean includeNulls)
          Iterates over values applying this to each instance of V and adding tje results of the evaulations to collector
abstract  K evaluate(java.lang.Object... arguments)
          Evaluates the supplied arguments returning an instance of type K.
 boolean hasSameValue(java.lang.Iterable elements)
          Returns true if each of the elements in elements has the same value according to this function.
static
<X> Function<X>
identity()
           
static
<X> Function<X>
make(java.lang.Class cls, java.lang.String methodName, java.lang.Class... argumentTypes)
          Convenience function that returns a new Function based on the supplied Method.
static
<X> Function<X>
make(java.lang.reflect.Method method)
          Convenience function that returns a new Function based on the supplied Method.
<V> java.util.Map<V,K>
map(java.lang.Iterable<V> values)
          Iterates over values applying this to each instance of V and adding the mapping to the returned map.
<V> java.util.Map<V,K>
map(java.lang.Iterable<V> values, boolean includeNulls)
          Iterates over values applying this to each instance of V and adding the mapping to the returned map.
<V> void
mapInto(java.lang.Iterable<V> values, java.util.Map<V,K> collector, boolean includeNulls)
          Iterates over values applying this to each instance of V and adding the mapping to collector.
<V> void
reverseMapInto(java.lang.Iterable<V> values, java.util.Map<K,V> collector, boolean includeNulls)
          Iterates over values applying this to each instance of V and adding the reverse mapping to collector.
<V> void
reverseMapInto(V[] values, java.util.Map<K,V> collector, boolean includeNulls)
          Iterates over values applying this to each instance of V and adding the reverse mapping to collector.
<V> java.util.Map<K,java.util.List<V>>
split(java.lang.Iterable<V> collection)
          Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.
<V> void
splitInto(java.lang.Iterable<V> collection, java.util.Map<K,java.util.List<V>> result)
          Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.
<V> void
splitInto(java.lang.Iterable<V> collection, java.util.Map<K,java.util.List<V>> result, boolean includeNulls)
          Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.
<V,W> void
splitInto(java.lang.Iterable<V> collection, java.util.Map<K,W> result, Aggregator<W,V> aggregator, boolean includeNulls)
          Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

Identity

public static Function Identity
Constructor Detail

Function

public Function()
Method Detail

evaluate

public abstract K evaluate(java.lang.Object... arguments)
                    throws EvaluationException
Evaluates the supplied arguments returning an instance of type K.

Parameters:
arguments - the arguments to be evaulated
Returns:
the instance of K
Throws:
EvaluationException

hasSameValue

public boolean hasSameValue(java.lang.Iterable elements)
Returns true if each of the elements in elements has the same value according to this function.


splitInto

public <V,W> void splitInto(java.lang.Iterable<V> collection,
                            java.util.Map<K,W> result,
                            Aggregator<W,V> aggregator,
                            boolean includeNulls)
Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.

Using the aggregator function when merging the value to the map. Specifically, this method splits collection into sub-lists each of which has the same value when evaluated by this function.


splitInto

public <V> void splitInto(java.lang.Iterable<V> collection,
                          java.util.Map<K,java.util.List<V>> result,
                          boolean includeNulls)
Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.

Specifically, this method splits collection into sub-lists each of which has the same value when evaluated by this function.


splitInto

public <V> void splitInto(java.lang.Iterable<V> collection,
                          java.util.Map<K,java.util.List<V>> result)
Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.

Specifically, this method splits collection into sub-lists each of which has the same value when evaluated by this function.


split

public <V> java.util.Map<K,java.util.List<V>> split(java.lang.Iterable<V> collection)
Efficiently, splits the supplied collection of a value type V returning a map which is a partition of the collection using this function.

Specifically, this method splits collection into sub-lists each of which has the same value when evaluated by this function.


mapInto

public <V> void mapInto(java.lang.Iterable<V> values,
                        java.util.Map<V,K> collector,
                        boolean includeNulls)
Iterates over values applying this to each instance of V and adding the mapping to collector.

Parameters:
values - the collection of values
collector - the resulting map
includeNulls - whether or not null evaluations should be mapped

map

public final <V> java.util.Map<V,K> map(java.lang.Iterable<V> values,
                                        boolean includeNulls)
Iterates over values applying this to each instance of V and adding the mapping to the returned map.

Parameters:
values - the collection of values
includeNulls - whether or not null evaluations should be mapped
Returns:
the resulting map

map

public final <V> java.util.Map<V,K> map(java.lang.Iterable<V> values)
Iterates over values applying this to each instance of V and adding the mapping to the returned map.

Parameters:
values - the collection of values
Returns:
the resulting map

reverseMapInto

public final <V> void reverseMapInto(java.lang.Iterable<V> values,
                                     java.util.Map<K,V> collector,
                                     boolean includeNulls)
Iterates over values applying this to each instance of V and adding the reverse mapping to collector.

Parameters:
values - the collection of values
collector - the resulting map
includeNulls - whether or not null evaluations should be mapped

reverseMapInto

public final <V> void reverseMapInto(V[] values,
                                     java.util.Map<K,V> collector,
                                     boolean includeNulls)
Iterates over values applying this to each instance of V and adding the reverse mapping to collector.

Parameters:
values - the collection of values
collector - the resulting map
includeNulls - whether or not null evaluations should be mapped

collectInto

public final <V> void collectInto(java.lang.Iterable<V> values,
                                  java.util.Collection<K> collector,
                                  boolean includeNulls)
Iterates over values applying this to each instance of V and adding tje results of the evaulations to collector

Parameters:
values - the collection of values
collector - the resulting map
includeNulls - whether or not null evaluations should be included

collect

public final <V> java.util.List<K> collect(java.lang.Iterable<V> values,
                                           boolean includeNulls)
Iterates over values applying this to each instance of V and adding the results of the evaluations to the returned list.

Parameters:
values - the collection of values
includeNulls - whether or not null evaluations should be included
Returns:
the resulting list

collect

public final <V> java.util.List<K> collect(java.lang.Iterable<V> values)
Iterates over values applying this to each instance of V and adding the results of the evaluations to the returned list.

Parameters:
values - the collection of values
Returns:
the resulting list

make

public static <X> Function<X> make(java.lang.reflect.Method method)
Convenience function that returns a new Function based on the supplied Method.


make

public static <X> Function<X> make(java.lang.Class cls,
                                   java.lang.String methodName,
                                   java.lang.Class... argumentTypes)
                        throws java.lang.NoSuchMethodException
Convenience function that returns a new Function based on the supplied Method.

Throws:
java.lang.NoSuchMethodException

identity

public static <X> Function<X> identity()


AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.