org.jjoost.util
Class Functions

java.lang.Object
  extended by org.jjoost.util.Functions

public class Functions
extends java.lang.Object

A class defining simple functions, and methods for working with functions

Author:
b.elliottsmith

Nested Class Summary
static class Functions.AbstractValueProjection<E extends Value<V>,V>
          A function that retrieves the value from a Value
static class Functions.FieldProjection<E,F>
          A Function which accepts a Field (reflection API), which will be retrieved and returned from every object the function is applied to.
static class Functions.MapEntryKeyProjection<E extends java.util.Map.Entry<K,?>,K>
          A function that retrieves the key portion of a Map.Entry
static class Functions.MapEntryValueProjection<E extends java.util.Map.Entry<?,V>,V>
          A function that retrieves the value portion of a Map.Entry
static class Functions.MethodProjection<E,F>
          A Function which accepts a Method and arguments to provide to the method, which will execute the method on each object it receives as an argument, returning the result.
 
Constructor Summary
Functions()
           
 
Method Summary
static
<E,F> ClosableIterator<F>
apply(ClosableIterator<? extends E> iter, Function<? super E,? extends F> f)
          /** Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator.
static
<E,F> ClosableIterator<F>
apply(Function<? super E,? extends F> f, ClosableIterator<? extends E> iter)
          Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator.
static
<E,F> java.lang.Iterable<F>
apply(Function<? super E,? extends F> f, java.lang.Iterable<? extends E> iter)
          Returns a new Iterable which lazily applies the provided function to all iterators constructed from it.
static
<E,F> java.util.Iterator<F>
apply(Function<? super E,? extends F> f, java.util.Iterator<? extends E> iter)
          Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator.
static
<E,F> java.util.List<F>
apply(Function<? super E,? extends F> f, java.util.List<? extends E> list)
          Creates a new ArrayList that contains the result of applying the provided function to every element in the provided list.
static
<E,F> java.lang.Iterable<F>
apply(java.lang.Iterable<? extends E> iter, Function<? super E,? extends F> f)
          Returns a new Iterable which lazily applies the provided function to all iterators constructed from it.
static
<E,F> java.util.Iterator<F>
apply(java.util.Iterator<? extends E> iter, Function<? super E,? extends F> f)
          Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator.
static
<E,F> java.util.List<F>
apply(java.util.List<? extends E> list, Function<? super E,? extends F> f)
          Creates a new ArrayList that contains the result of applying the provided function to every element in the provided list.
static
<E,F,G> Function<E,G>
composition(Function<F,G> f2, Function<E,F> f1)
          Returns a function that is the composition of the two provided functions; the function provided as the second argument is applied first, and the function provided as the first argument is then applied to the result of this, i.e.
static
<E,F> Function<E,F>
getFieldProjection(java.lang.Class<E> clazz, java.lang.String propertyName, java.lang.Class<F> propertyType)
          Return a Function which retrieves the named field from each object it is applied to
static
<K,E extends java.util.Map.Entry<? extends K,?>>
Function<E,K>
getMapEntryKeyProjection()
          Return a function that retrieves the key portion of a Map.Entry
static
<V,E extends java.util.Map.Entry<?,? extends V>>
Function<E,V>
getMapEntryValueProjection()
          Return a function that retrieves the value portion of a Map.Entry
static
<E,F> Function<E,F>
getMethodProjection(java.lang.Class<E> clazz, java.lang.String methodName, java.lang.Class<F> returnType)
          Return a Function which executes the named no-arg method on objects of the provided type
static
<V> Function<Value<V>,V>
getValueContentsProjection()
          Return a function that retrieves the value from a Value object
static
<E> Function<E,E>
identity()
          Returns the identity function, i.e.
static
<E> Function<E,E>
replaceNullWith(E val)
          Returns a function that for all non-null inputs acts like the identity function (i.e.
static
<E> Function<E,java.lang.String>
toString(boolean replaceNulls)
          Returns a function that converts its input to a String; if replaceNulls is true then null values are replaced by "null", otherwise they are left as null
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Functions

public Functions()
Method Detail

identity

public static <E> Function<E,E> identity()
Returns the identity function, i.e. that returns its input as output

Type Parameters:
E - input type
Returns:
the identity function

toString

public static <E> Function<E,java.lang.String> toString(boolean replaceNulls)
Returns a function that converts its input to a String; if replaceNulls is true then null values are replaced by "null", otherwise they are left as null

Parameters:
replaceNulls - if true, replace nulls with "null"
Returns:
function that converts its input to a String

replaceNullWith

public static <E> Function<E,E> replaceNullWith(E val)
Returns a function that for all non-null inputs acts like the identity function (i.e. yields input as output), however replaces all null values it is applied to with the provided value

Parameters:
val - value to replace nulls with
Returns:
a function replacing nulls with the provided value

composition

public static <E,F,G> Function<E,G> composition(Function<F,G> f2,
                                                Function<E,F> f1)
Returns a function that is the composition of the two provided functions; the function provided as the second argument is applied first, and the function provided as the first argument is then applied to the result of this, i.e. returns f2.apply(f1.apply(v))

Parameters:
f2 - function applied second
f1 - function applied first
Returns:
composition of f1 and f2

getMethodProjection

public static <E,F> Function<E,F> getMethodProjection(java.lang.Class<E> clazz,
                                                      java.lang.String methodName,
                                                      java.lang.Class<F> returnType)
                                         throws java.lang.SecurityException,
                                                java.lang.NoSuchMethodException
Return a Function which executes the named no-arg method on objects of the provided type

Parameters:
clazz - function input type
methodName - name of the method to call
returnType - the return type of the method
Returns:
a Function which executes the named no-arg method on objects of the provided type
Throws:
java.lang.SecurityException - if a SecurityManager prevents access to the method
java.lang.NoSuchMethodException - if no no-args method of the provided name can be found in the provided class' hierarchy

getFieldProjection

public static <E,F> Function<E,F> getFieldProjection(java.lang.Class<E> clazz,
                                                     java.lang.String propertyName,
                                                     java.lang.Class<F> propertyType)
                                        throws java.lang.SecurityException,
                                               java.lang.NoSuchFieldException
Return a Function which retrieves the named field from each object it is applied to

Parameters:
clazz - the input type of the function
propertyName - the name of the property
propertyType - the type of the property
Returns:
a Function which retrieves the named field from each object it is applied to
Throws:
java.lang.SecurityException - if a SecurityManager prevents access to the method
java.lang.NoSuchFieldException - if a field of the provided name could not be found in the provided class

getMapEntryValueProjection

public static <V,E extends java.util.Map.Entry<?,? extends V>> Function<E,V> getMapEntryValueProjection()
Return a function that retrieves the value portion of a Map.Entry

Returns:
a function that retrieves the value portion of a Map.Entry

getMapEntryKeyProjection

public static <K,E extends java.util.Map.Entry<? extends K,?>> Function<E,K> getMapEntryKeyProjection()
Return a function that retrieves the key portion of a Map.Entry

Returns:
a function that retrieves the key portion of a Map.Entry

getValueContentsProjection

public static <V> Function<Value<V>,V> getValueContentsProjection()
Return a function that retrieves the value from a Value object

Returns:
a function that retrieves the value from a Value object

apply

public static <E,F> java.util.List<F> apply(Function<? super E,? extends F> f,
                                            java.util.List<? extends E> list)
Creates a new ArrayList that contains the result of applying the provided function to every element in the provided list. Equivalent to apply(list, f).

Parameters:
list - list to apply the function to
f - function to apply to the list
Returns:
a new ArrayList that contains the result of applying the provided function to every element in the provided list

apply

public static <E,F> java.util.List<F> apply(java.util.List<? extends E> list,
                                            Function<? super E,? extends F> f)
Creates a new ArrayList that contains the result of applying the provided function to every element in the provided list. Equivalent to apply(f, list).

Parameters:
list - list to apply the function to
f - function to apply to the list
Returns:
a new ArrayList that contains the result of applying the provided function to every element in the provided list

apply

public static <E,F> java.lang.Iterable<F> apply(Function<? super E,? extends F> f,
                                                java.lang.Iterable<? extends E> iter)
Returns a new Iterable which lazily applies the provided function to all iterators constructed from it. Equivalent to apply(iter, f).

Parameters:
iter - iterable to apply the function to
f - function to apply to the iterable
Returns:
a new Iterable which lazily applies the provided function to all iterators constructed from it

apply

public static <E,F> java.lang.Iterable<F> apply(java.lang.Iterable<? extends E> iter,
                                                Function<? super E,? extends F> f)
Returns a new Iterable which lazily applies the provided function to all iterators constructed from it. Equivalent to apply(f, iter).

Parameters:
iter - iterable to apply the function to
f - function to apply to the iterable
Returns:
a new Iterable which lazily applies the provided function to all iterators constructed from it

apply

public static <E,F> java.util.Iterator<F> apply(java.util.Iterator<? extends E> iter,
                                                Function<? super E,? extends F> f)
Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator. Equivalent to apply(f, iter).

Parameters:
iter - iterator to apply the function to
f - function to apply to the iterator
Returns:
a new iterator which return f.f(iter.next()) for each call to next()

apply

public static <E,F> java.util.Iterator<F> apply(Function<? super E,? extends F> f,
                                                java.util.Iterator<? extends E> iter)
Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator. Equivalent to apply(iter, f).

Parameters:
iter - iterator to apply the function to
f - function to apply to the iterator
Returns:
a new iterator which return f.f(iter.next()) for each call to next()

apply

public static <E,F> ClosableIterator<F> apply(ClosableIterator<? extends E> iter,
                                              Function<? super E,? extends F> f)
/** Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator. Equivalent to apply(f, iter).

Parameters:
iter - iterator to apply the function to
f - function to apply to the iterator
Returns:
a new iterator which return f.f(iter.next()) for each call to next()

apply

public static <E,F> ClosableIterator<F> apply(Function<? super E,? extends F> f,
                                              ClosableIterator<? extends E> iter)
Returns a new iterator which returns f.f(iter.next()) for each call to next() in the resulting iterator. Equivalent to apply(iter, f).

Parameters:
iter - iterator to apply the function to
f - function to apply to the iterator
Returns:
a new iterator which return f.f(iter.next()) for each call to next()