com.myjavatools.lib.foundation
Class Function<X,Y>

java.lang.Object
  extended by com.myjavatools.lib.foundation.Function<X,Y>
Direct Known Subclasses:
Filter, Predicate

public abstract class Function<X,Y>
extends java.lang.Object

Function is an abstract class that represents the mathematical notion of function: X -> Y. To implement a function, you need to define method apply(): Y y = function.apply(X x).

Since:
5.0
Version:
5.0, 11/15/04
See Also:
Filter, Maps, Map

Constructor Summary
Function()
           
 
Method Summary
 java.lang.Iterable<Y> apply(java.lang.Iterable<X> iterable)
          applies Function to an Iterable
 java.util.Iterator<Y> apply(java.util.Iterator<X> iterator)
          applies Function to an Iterator
 java.util.List<Y> apply(java.util.List<X> domain)
          applies Function to a List
abstract  Y apply(X x)
          the only method you should implement
<W> Function<W,Y>
compose(Function<W,X> g)
          Returns a composition f.g : W->Y of two functions, g:W->X and f(which is this Function): X->Y.
static
<X,Y,Z> Function<X,Z>
compose(Function<X,Y> f, Function<Y,Z> g)
          Returns a compostion f.g : X->Z of two functions, f: X->Y and g: Y->Z.
static
<X,Y> Function<X,Y>
function(java.util.Map<X,Y> map)
          turns a Map into a Function
static
<X,Y> Function<X,Y>
function(java.util.Map<X,Y> map, Y defaultValue)
          Returns a function defied by a map and a default value
 java.util.Map<X,Y> toMap(java.util.Collection<X> x)
          Restricts this function to a specified Collection, returning a map on the set of collection entries
 java.util.Map<X,Y> toMap(java.util.Set<X> x)
          Restricts this function to a specified X Set, returning a Map on that set
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Function

public Function()
Method Detail

apply

public abstract Y apply(X x)
the only method you should implement

Parameters:
x - X the function parameter
Returns:
Y the function value at x

toMap

public java.util.Map<X,Y> toMap(java.util.Set<X> x)
Restricts this function to a specified X Set, returning a Map on that set

Parameters:
x - Set<X> the set of map keys
Returns:
Map<X,Y> that maps keys from X to function values on those keys

toMap

public java.util.Map<X,Y> toMap(java.util.Collection<X> x)
Restricts this function to a specified Collection, returning a map on the set of collection entries

Parameters:
x - Collection<X> keys
Returns:
Map<X,Y> that maps keys from Collection to function values on those keys

function

public static <X,Y> Function<X,Y> function(java.util.Map<X,Y> map)
turns a Map into a Function

Parameters:
map - Map<X,Y> source map
Returns:
Function<X,Y> function that returns map.get(x) for each X x

function

public static <X,Y> Function<X,Y> function(java.util.Map<X,Y> map,
                                           Y defaultValue)
Returns a function defied by a map and a default value

Parameters:
map - Map<X,Y>
defaultValue - Y
Returns:
Function f such that f(x)=map.get(x) if map.containsKey(x), and defaultValue otherwise.

apply

public java.util.List<Y> apply(java.util.List<X> domain)
applies Function to a List

Parameters:
domain - List
Returns:
List

apply

public java.util.Iterator<Y> apply(java.util.Iterator<X> iterator)
applies Function to an Iterator

Parameters:
iterator - Iterator
Returns:
Iterator that lists values of function applied to elements returned by the original iterator

Example:

new Function<String, Integer>() {
               Integer apply(String s) { return s.length(); } }.
                apply(
                Arrays.asList(new String[] {"One", "Two", "Three"}).iterator());
returns an iterator returning 3, 3, 5.


apply

public java.lang.Iterable<Y> apply(java.lang.Iterable<X> iterable)
applies Function to an Iterable

Parameters:
iterable - Iterable
Returns:
Iterable that contains values of function applied to elements of the original iterable

compose

public <W> Function<W,Y> compose(Function<W,X> g)
Returns a composition f.g : W->Y of two functions, g:W->X and f(which is this Function): X->Y. Compostion is defined as a function h such that h(x) = f(g(x)) for each w.

Parameters:
g - Function<W,X>
Returns:
Function<W,Y> composition of g and f

compose

public static <X,Y,Z> Function<X,Z> compose(Function<X,Y> f,
                                            Function<Y,Z> g)
Returns a compostion f.g : X->Z of two functions, f: X->Y and g: Y->Z. Compostion is defined as a function h such that h(x) = g(f(x)) for each x.

Parameters:
f - Function<X,Y>
g - Function<Y,Z>
Returns:
Function<X,Z> composition of f and g