org.semanticweb.elk.util.collections
Class Operations

java.lang.Object
  extended by org.semanticweb.elk.util.collections.Operations

public class Operations
extends Object

Some useful static methods for collections

Author:
"Yevgeny Kazakov"

Nested Class Summary
static interface Operations.Condition<T>
          Boolean conditions over some type.
static interface Operations.Functor<I,O>
          A simple object that transforms objects of type I to objects of type O
static interface Operations.FunctorEx<I,O>
          An extension of Operations.Functor which can do the reverse transformation
static interface Operations.Transformation<I,O>
          Transformations of input values to output values
 
Field Summary
static Multimap<?,?> EMPTY_MULTIMAP
           
 
Constructor Summary
Operations()
           
 
Method Summary
static
<T> Iterable<T>
concat(Iterable<? extends Iterable<? extends T>> input)
          Concatenates several Iterables into one
static
<T> Iterable<T>
concat(Iterable<? extends T> a, Iterable<? extends T> b)
           
static
<T> void
dumpDiff(Collection<T> first, Collection<T> second, Writer writer, String prefix)
          Prints the elements present in the first Collection but not in the second Collection using the given Writer and prefixing all messages with a given prefix.
static
<K,V> void
dumpDiff(Multimap<K,V> first, Multimap<K,V> second, Writer writer, String prefix)
          Prints key-value entries present in the first Multimap but not in the second Multimap using the given Writer and prefixing all messages with a given prefix.
static
<S,T> Multimap<S,T>
emptyMultimap()
           
static
<T,S> Iterable<T>
filter(Iterable<S> input, Class<T> type)
           
static
<T> Iterable<T>
filter(Iterable<T> input, Operations.Condition<? super T> condition)
           
static
<T> Set<T>
filter(Set<T> input, Operations.Condition<? super T> condition, int size)
          Returns read-only view of the given set consisting of the elements satisfying a given condition, if the number of such elements is known
static
<T> Collection<T>
getCollection(Iterable<T> iterable, int size)
           
static
<I,O> Iterable<O>
map(Iterable<I> input, Operations.Transformation<? super I,O> transformation)
          Transforms elements using a given Operations.Transformation the output elements consist of the result of the transformation in the same order; if the transformation returns null, it is not included in the output
static
<I,O> Set<O>
map(Set<? extends I> input, Operations.FunctorEx<I,O> functor)
          A simple second-order map function
static
<T> Iterable<T>
singleton(T element)
           
static
<T> Collection<ArrayList<T>>
split(Collection<? extends T> elements, int batchSize)
          Splits the input Collection on batches with at most given number of elements.
static
<T> Iterable<ArrayList<T>>
split(Iterable<? extends T> elements, int batchSize)
          Splits the input Iterable on batches with at most given number of elements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_MULTIMAP

public static final Multimap<?,?> EMPTY_MULTIMAP
Constructor Detail

Operations

public Operations()
Method Detail

emptyMultimap

public static <S,T> Multimap<S,T> emptyMultimap()

concat

public static <T> Iterable<T> concat(Iterable<? extends T> a,
                                     Iterable<? extends T> b)

singleton

public static <T> Iterable<T> singleton(T element)

concat

public static <T> Iterable<T> concat(Iterable<? extends Iterable<? extends T>> input)
Concatenates several Iterables into one

Parameters:
input - the Iterable of Iterables to be concatenated
Returns:
Iterable consisting of all elements found in input Iterables

split

public static <T> Iterable<ArrayList<T>> split(Iterable<? extends T> elements,
                                               int batchSize)
Splits the input Iterable on batches with at most given number of elements.

Parameters:
elements - the Iterable to be split
batchSize - the maximal number of elements in batches
Returns:
a Iterable of batches containing elements from the input collection
See Also:
concat(Iterable)

split

public static <T> Collection<ArrayList<T>> split(Collection<? extends T> elements,
                                                 int batchSize)
Splits the input Collection on batches with at most given number of elements.

Parameters:
elements - the Collection to be split
batchSize - the maximal number of elements in batches
Returns:
a Collection of batches containing elements from the input collection

getCollection

public static <T> Collection<T> getCollection(Iterable<T> iterable,
                                              int size)

filter

public static <T> Iterable<T> filter(Iterable<T> input,
                                     Operations.Condition<? super T> condition)
Parameters:
input - the input iterator
condition - the condition used for filtering
Returns:
the filtered iterator

filter

public static <T,S> Iterable<T> filter(Iterable<S> input,
                                       Class<T> type)

filter

public static <T> Set<T> filter(Set<T> input,
                                Operations.Condition<? super T> condition,
                                int size)
Returns read-only view of the given set consisting of the elements satisfying a given condition, if the number of such elements is known

Parameters:
input - the given set to be filtered
condition - the condition used for filtering the set. Must be consistent with equals() for T, that is: a.equals(b) must imply that holds(a) == holds(b)
size - the number of elements in the filtered set
Returns:
the set consisting of the elements of the input set satisfying the given condition

map

public static <I,O> Iterable<O> map(Iterable<I> input,
                                    Operations.Transformation<? super I,O> transformation)
Transforms elements using a given Operations.Transformation the output elements consist of the result of the transformation in the same order; if the transformation returns null, it is not included in the output

Parameters:
input - the input elements
transformation - the transformation for elements
Returns:
the transformed output elements

dumpDiff

public static <K,V> void dumpDiff(Multimap<K,V> first,
                                  Multimap<K,V> second,
                                  Writer writer,
                                  String prefix)
                     throws IOException
Prints key-value entries present in the first Multimap but not in the second Multimap using the given Writer and prefixing all messages with a given prefix.

Parameters:
first -
second -
writer -
prefix -
Throws:
IOException

dumpDiff

public static <T> void dumpDiff(Collection<T> first,
                                Collection<T> second,
                                Writer writer,
                                String prefix)
                     throws IOException
Prints the elements present in the first Collection but not in the second Collection using the given Writer and prefixing all messages with a given prefix.

Parameters:
first -
second -
writer -
prefix -
Throws:
IOException

map

public static <I,O> Set<O> map(Set<? extends I> input,
                               Operations.FunctorEx<I,O> functor)
A simple second-order map function

Parameters:
input -
functor -
Returns:
a set which contains the results of applying the functor to the elements in the input set


Copyright © 2011-2013 Department of Computer Science, University of Oxford. All Rights Reserved.