com.myjavatools.lib.foundation
Class Function2<X,Y,Z>

java.lang.Object
  extended by com.myjavatools.lib.foundation.Function2<X,Y,Z>

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

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

Since:
6.0
Version:
6.0 12/06/06
See Also:
Filter, Maps, Function

Constructor Summary
Function2()
           
 
Method Summary
abstract  Z apply(X x, Y y)
          the only method you should implement
static
<A,B,X,Y,Z>
Function2<A,B,Z>
compose(Function<A,? extends X> f, Function<B,? extends Y> g, Function2<? super X,? super Y,? extends Z> h)
          Returns a compostion (f,g).h : AxB->Z of a two-parameter function, h: XxY->Z and two single-parameter functions, f:A->Z and g:B->Y.
static
<X,Y,Z,T> Function2<X,Y,T>
compose(Function2<X,Y,? extends Z> f, Function<? super Z,? extends T> g)
          Returns a compostion f.g : XxY->T of a two-parameter function, f: XxY->Z and g: Z->T.
 Function<Y,Z> curry1(X x)
          Curries the function by the first argument.
 Function<X,Z> curry2(Y y)
          Curries the function by the second argument.
static
<X,Y,Z> Function2<X,Y,Z>
forFunction(Function<java.util.Map.Entry<X,Y>,Z> f)
          Creates a two-parameter function for a function defined on pairs.
static
<X,Y,Z> Function2<X,Y,Z>
forMap(java.util.Map<X,java.util.Map<Y,Z>> map)
          Creates a two-parameter function for a "cascade map".
static
<X,Y,Z> Function2<X,Y,Z>
forMap(java.util.Map<X,java.util.Map<Y,Z>> map, Z defaultValue)
          Creates a two-parameter function for a "cascade map".
static
<X,Y> Function2<X,Y,X>
p1()
          First component projection.
static
<X,Y> Function2<X,Y,Y>
p2()
          Second component projection.
 Function2<Y,X,Z> swap()
          Swaps function arguments.
 Function<java.util.Map.Entry<X,Y>,Z> toFunction()
          Converts a two-parameter function to a function on pairs, (X x, Y y).
 java.util.Map<X,java.util.Map<Y,Z>> toMap(java.util.Set<? extends X> xKeys, java.util.Set<? extends Y> yKeys)
          Creates a (virtual) map for a two-parameter function.
 Z zz(X x)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Function2

public Function2()
Method Detail

apply

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

Parameters:
x - X the function parameter
y - Y the function parameter
Returns:
Z the function value at (x,y)

p1

public static <X,Y> Function2<X,Y,X> p1()
First component projection.

Returns:
Function2 that returns its first parameter

p2

public static <X,Y> Function2<X,Y,Y> p2()
Second component projection.

Returns:
Function2 that returns its second parameter

toFunction

public Function<java.util.Map.Entry<X,Y>,Z> toFunction()
Converts a two-parameter function to a function on pairs, (X x, Y y).

Returns:
Function that takes a pair (actually, a Map.Entry, which is a popular interface that represents pairs of objects) and returns the value of the original function on the components of the pair. That is, for f: X x Y -> Z, the resulting function, when applied to (x,y), will return f(x,y).

forFunction

public static <X,Y,Z> Function2<X,Y,Z> forFunction(Function<java.util.Map.Entry<X,Y>,Z> f)
Creates a two-parameter function for a function defined on pairs.

Parameters:
f - Function that takes Map.Entry as an argument and returns an instance of Z
Returns:
Function2 defined on X x Y, such that for X x and Y y it returns f((x,y)).

forMap

public static <X,Y,Z> Function2<X,Y,Z> forMap(java.util.Map<X,java.util.Map<Y,Z>> map,
                                              Z defaultValue)
Creates a two-parameter function for a "cascade map".

Parameters:
map - <X,Map<Y,Z>>Map
defaultValue - Z default function value for parameters that are not contained as keys in Map
Returns:
Function2 defined on X x Y and taking values in Z. For a map m, an X x, and a Y y, the value of the function is calculated like this: m.get(x).get(y). If any of these get() returns null, the function returns defaultValue.

forMap

public static <X,Y,Z> Function2<X,Y,Z> forMap(java.util.Map<X,java.util.Map<Y,Z>> map)
Creates a two-parameter function for a "cascade map".

Parameters:
map - <X,Map<Y,Z>>Map
Returns:
Function2 defined on X x Y and taking values in Z. For a map m, an X x, and a Y y, the value of the function is calculated like this: m.get(x).get(y). If any of these get() returns null, the function returns null.

toMap

public java.util.Map<X,java.util.Map<Y,Z>> toMap(java.util.Set<? extends X> xKeys,
                                                 java.util.Set<? extends Y> yKeys)
Creates a (virtual) map for a two-parameter function.

Parameters:
xKeys - Set the set of keys for the first argument
yKeys - Set the set of keys for the second argument
Returns:
Map a cascade map that for an x from xKeys and a y from yKeys get(x).get(y) returns f(x,y).

swap

public Function2<Y,X,Z> swap()
Swaps function arguments.

Returns:
Function2 such that its arguments are in reverse order: g(y,x) = f(x,Y).

curry1

public Function<Y,Z> curry1(X x)
Curries the function by the first argument.

Parameters:
x - X
Returns:
Function that for each Y y returns f(x,y).

zz

public Z zz(X x)

curry2

public Function<X,Z> curry2(Y y)
Curries the function by the second argument.

Parameters:
y - Y
Returns:
Function that for each X x returns f(x,y).

compose

public static <X,Y,Z,T> Function2<X,Y,T> compose(Function2<X,Y,? extends Z> f,
                                                 Function<? super Z,? extends T> g)
Returns a compostion f.g : XxY->T of a two-parameter function, f: XxY->Z and g: Z->T. Compostion is defined as a function h such that h(x,y) = g(f(x,y)) for each pair x,y.

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

compose

public static <A,B,X,Y,Z> Function2<A,B,Z> compose(Function<A,? extends X> f,
                                                   Function<B,? extends Y> g,
                                                   Function2<? super X,? super Y,? extends Z> h)
Returns a compostion (f,g).h : AxB->Z of a two-parameter function, h: XxY->Z and two single-parameter functions, f:A->Z and g:B->Y. Compostion is defined as a function q such that q(a,b) = h(f(a),g(b))) for each pair a,b.

Parameters:
f - Function<A,X>
g - Function<B,Y>
h - Function2<X,Y,Z>
Returns:
Function<A,B,Z> composition of (f,g) and h.