eu.monnetproject.lemon.conversions.lmf
Class CollectionFunctions

java.lang.Object
  extended by eu.monnetproject.lemon.conversions.lmf.CollectionFunctions

public class CollectionFunctions
extends Object

Static extensions to the collection API to give functional-programming-like operations. If you use this class it is strongly recommended that you static import it.

Example usage:
if(exists(list, new Criterion() {
   public boolean f(Integer i) {
     return i > 10;
   }
 })) {
  System.out.println("List contains an elements larger than 10");
}

Author:
John McCrae

Nested Class Summary
static interface CollectionFunctions.Converter<E,F>
          A single abstract method interface for a function of arity 1:1
static interface CollectionFunctions.Criterion<E>
          A single abstract method interface returning a boolean
static interface CollectionFunctions.FoldFunction<F,E>
          A single abstract method interface for a function of arity 2:1
 
Field Summary
static CollectionFunctions.Converter IDENTITY
          Convenience SAM function that returns the same value
 
Method Summary
static
<E> boolean
contains(E[] array, E element)
          Returns true if the array contains a specific element.
static
<E> CollectionFunctions.Criterion<E>
eq(E e)
          Convenience equivalence function
static
<E> boolean
exists(Collection<E> collection, CollectionFunctions.Criterion<E> f)
          Returns true if the collection has an element satisfying the condition
static
<E> boolean
exists(E[] array, CollectionFunctions.Criterion<E> f)
          Returns true if the array has an element satisfying the condition
static
<E> E[]
filter(E[] array, CollectionFunctions.Criterion<E> f)
          Return an array containing all elements matching a specific criterion
static
<E> List<E>
filter(List<E> list, CollectionFunctions.Criterion<E> f)
          Return a collection containing all elements matching a specific criterion
static
<E> Set<E>
filter(Set<E> set, CollectionFunctions.Criterion<E> f)
          Return a collection containing all elements matching a specific criterion
static
<E,F> Map<E,F>
filterByKey(Map<E,F> map, CollectionFunctions.Criterion<E> f)
          Return a map containing all entries whose keys match a specific criterion
static
<E,F> Map<E,F>
filterByValue(Map<E,F> map, CollectionFunctions.Criterion<F> f)
          Return a map containing all entries whose values match a specific criterion
static
<E> E
find(Collection<E> collection, CollectionFunctions.Criterion<E> f)
          Find the first element of the collection satisfying the condition
static
<E> E
find(E[] array, CollectionFunctions.Criterion<E> f)
          Find the first element of the array satisfying the condition
static
<E,F> F
foldLeft(Collection<E> collection, F initial, CollectionFunctions.FoldFunction<F,E> f)
          Fold the collection to the left.
static
<E,F> F
foldLeft(E[] array, F initial, CollectionFunctions.FoldFunction<F,E> f)
          Fold the array to the left.
static
<E> boolean
forall(Collection<E> collection, CollectionFunctions.Criterion<E> f)
          Check if a condition holds for all elements in the collection
static
<E> boolean
forall(E[] array, CollectionFunctions.Criterion<E> f)
          Check if a condition holds for all elements in the array
static
<StrLike extends CharSequence>
String
joinString(Collection<StrLike> collection, String separator)
          Create a string from a collection using a particular separator String.
static
<E,F> F[]
map(E[] array, CollectionFunctions.Converter<E,F> f)
          Convert each element of an array
static
<E,F> List<F>
map(List<E> list, CollectionFunctions.Converter<E,F> f)
          Convert each element of a list
static
<E,F,G,H> Map<G,H>
map(Map<E,F> map, CollectionFunctions.Converter<E,G> f1, CollectionFunctions.Converter<F,H> f2)
          Convert each element of a map
static
<E,F> Set<F>
map(Set<E> set, CollectionFunctions.Converter<E,F> f)
          Convert each element of a set
static
<E> CollectionFunctions.Criterion<E>
neq(E e)
          Convenience equivalence function
static
<E> List<E>
partition(List<E> list, CollectionFunctions.Criterion<E> f)
          Remove all elements matching a criterion from a list and return a new list containing all elements matching the criterion
static
<E> Set<E>
partition(Set<E> set, CollectionFunctions.Criterion<E> f)
          Remove all elements matching a criterion from a set and return a new set containing all elements matching the criterion
static
<E,F> Map<E,F>
partitionByKey(Map<E,F> map, CollectionFunctions.Criterion<E> f)
          Remove all entries whose keys match a criterion from a set and return a new map containing all entries matching the criterion
static
<E,F> Map<E,F>
partitionByValue(Map<E,F> map, CollectionFunctions.Criterion<F> f)
          Remove all entries whose values match a criterion from a set and return a new map containing all entries matching the criterion
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IDENTITY

public static final CollectionFunctions.Converter IDENTITY
Convenience SAM function that returns the same value

Method Detail

contains

public static <E> boolean contains(E[] array,
                                   E element)
Returns true if the array contains a specific element. Uses Object.equals()

Parameters:
array - The array
element - The element
Returns:
true if the element is in the array

exists

public static <E> boolean exists(E[] array,
                                 CollectionFunctions.Criterion<E> f)
Returns true if the array has an element satisfying the condition

Parameters:
array - The array
f - The function

exists

public static <E> boolean exists(Collection<E> collection,
                                 CollectionFunctions.Criterion<E> f)
Returns true if the collection has an element satisfying the condition

Parameters:
collection - The collection
f - The function

filter

public static <E> E[] filter(E[] array,
                             CollectionFunctions.Criterion<E> f)
Return an array containing all elements matching a specific criterion

Parameters:
array - The array
f - The function

filter

public static <E> List<E> filter(List<E> list,
                                 CollectionFunctions.Criterion<E> f)
Return a collection containing all elements matching a specific criterion

Parameters:
list - The list
f - The function

filter

public static <E> Set<E> filter(Set<E> set,
                                CollectionFunctions.Criterion<E> f)
Return a collection containing all elements matching a specific criterion

Parameters:
set - The list
f - The function

filterByKey

public static <E,F> Map<E,F> filterByKey(Map<E,F> map,
                                         CollectionFunctions.Criterion<E> f)
Return a map containing all entries whose keys match a specific criterion

Parameters:
map - The map
f - The function

filterByValue

public static <E,F> Map<E,F> filterByValue(Map<E,F> map,
                                           CollectionFunctions.Criterion<F> f)
Return a map containing all entries whose values match a specific criterion

Parameters:
map - The map
f - The function

find

public static <E> E find(E[] array,
                         CollectionFunctions.Criterion<E> f)
Find the first element of the array satisfying the condition

Parameters:
array - The array
f - The condition
Returns:
The first element or null if no element satisfies the condition

find

public static <E> E find(Collection<E> collection,
                         CollectionFunctions.Criterion<E> f)
Find the first element of the collection satisfying the condition

Parameters:
collection - The collection
f - The condition
Returns:
The first element or null if no element satisfies the condition

foldLeft

public static <E,F> F foldLeft(E[] array,
                               F initial,
                               CollectionFunctions.FoldFunction<F,E> f)
Fold the array to the left. That is apply f to each element in order

Parameters:
array - The array
initial - The initial value
f - The folding function
Returns:
The result

foldLeft

public static <E,F> F foldLeft(Collection<E> collection,
                               F initial,
                               CollectionFunctions.FoldFunction<F,E> f)
Fold the collection to the left. That is apply f to each element in order, e.g., to sum a List<Integer> the following code can be used:
foldLeft(list, 0, new SAM2() { public Integer f(Integer v1, Integer v2) { return v1 + v2 } });

Parameters:
array - The array
initial - The initial value
f - The folding function
Returns:
The result

forall

public static <E> boolean forall(E[] array,
                                 CollectionFunctions.Criterion<E> f)
Check if a condition holds for all elements in the array

Parameters:
array - The array
f - The condition

forall

public static <E> boolean forall(Collection<E> collection,
                                 CollectionFunctions.Criterion<E> f)
Check if a condition holds for all elements in the collection

Parameters:
array - The collection
f - The condition

map

public static <E,F> F[] map(E[] array,
                            CollectionFunctions.Converter<E,F> f)
Convert each element of an array

Parameters:
array - The array
f - The conversion function

map

public static <E,F> List<F> map(List<E> list,
                                CollectionFunctions.Converter<E,F> f)
Convert each element of a list

Parameters:
list - The list
f - The conversion function

map

public static <E,F> Set<F> map(Set<E> set,
                               CollectionFunctions.Converter<E,F> f)
Convert each element of a set

Parameters:
set - The set
f - The conversion function

map

public static <E,F,G,H> Map<G,H> map(Map<E,F> map,
                                     CollectionFunctions.Converter<E,G> f1,
                                     CollectionFunctions.Converter<F,H> f2)
Convert each element of a map

Parameters:
map - The map
f1 - The key conversion function
f2 - The value conversion function

partition

public static <E> List<E> partition(List<E> list,
                                    CollectionFunctions.Criterion<E> f)
Remove all elements matching a criterion from a list and return a new list containing all elements matching the criterion

Parameters:
list - The list
f - The criterion

partition

public static <E> Set<E> partition(Set<E> set,
                                   CollectionFunctions.Criterion<E> f)
Remove all elements matching a criterion from a set and return a new set containing all elements matching the criterion

Parameters:
set - The set
f - The criterion

partitionByKey

public static <E,F> Map<E,F> partitionByKey(Map<E,F> map,
                                            CollectionFunctions.Criterion<E> f)
Remove all entries whose keys match a criterion from a set and return a new map containing all entries matching the criterion

Parameters:
map - The map
f - The criterion

partitionByValue

public static <E,F> Map<E,F> partitionByValue(Map<E,F> map,
                                              CollectionFunctions.Criterion<F> f)
Remove all entries whose values match a criterion from a set and return a new map containing all entries matching the criterion

Parameters:
map - The map
f - The criterion

joinString

public static <StrLike extends CharSequence> String joinString(Collection<StrLike> collection,
                                                               String separator)
Create a string from a collection using a particular separator String. e.g., builds Strings like "a,b,c" of "a|||b|||c"

Parameters:
collection - The collection
separator - The separator
Returns:
The string

eq

public static <E> CollectionFunctions.Criterion<E> eq(E e)
Convenience equivalence function


neq

public static <E> CollectionFunctions.Criterion<E> neq(E e)
Convenience equivalence function



Copyright © 2012. All Rights Reserved.