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:
6.0 05/04/06
See Also:
Filter, Maps, Map

Constructor Summary
Function()
           
 
Method Summary
 java.lang.Iterable<Y> apply(java.lang.Iterable<? extends X> iterable)
          applies Function to an Iterable
 java.util.Iterator<Y> apply(java.util.Iterator<? extends X> iterator)
          applies Function to an Iterator
 java.util.List<Y> apply(java.util.List<? extends 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,? extends 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,? extends Y> f, Function<? super Y,? extends 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>
forMap(java.util.Map<? super X,Y> map, Y defaultValue)
          Returns a function defied by a map and a default value
static
<X,Y> Function<X,Y>
forMap(java.util.Map<X,Y> map)
          turns a Map into a Function
static
<X,Y> Function<X,Y>
function(java.util.Map<? super X,Y> map, Y defaultValue)
          Deprecated. use forMap instead.
static
<X,Y> Function<X,Y>
function(java.util.Map<X,Y> map)
          Deprecated. use forMap instead
static
<X> Function<X,X>
id()
           
 java.util.Map<X,Y> toMap(java.util.Collection<? extends X> keys)
          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<? extends X> keys)
          Restricts this function to a specified Set, returning a map with keys from the 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<? extends X> keys)
Restricts this function to a specified Set, returning a map with keys from the set.

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

toMap

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

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

forMap

public static <X,Y> Function<X,Y> forMap(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)
Deprecated. use forMap instead

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

forMap

public static <X,Y> Function<X,Y> forMap(java.util.Map<? super 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.

function

public static <X,Y> Function<X,Y> function(java.util.Map<? super X,Y> map,
                                           Y defaultValue)
Deprecated. use forMap instead.

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<? extends X> domain)
applies Function to a List

Parameters:
domain - List
Returns:
List

apply

public java.util.Iterator<Y> apply(java.util.Iterator<? extends 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<? extends 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,? extends 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,? extends Y> f,
                                            Function<? super Y,? extends 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

id

public static <X> Function<X,X> id()