net.sf.molae.pipe.binop
Class ElementwiseIterator<E,S1 extends E,S2>

java.lang.Object
  extended by net.sf.molae.pipe.binop.ElementwiseIterator<E,S1,S2>
All Implemented Interfaces:
Iterator<E>

public class ElementwiseIterator<E,S1 extends E,S2>
extends Object
implements Iterator<E>

Performs a binary operation on every pair of elements from two iterators. The first element from iterator 1 is computed with the first element from iterator 2, the second element from iterator 1 with the second element from iterator 2 and so on...

If the first iterator has more elements than the second its elements are returned at the end of this iterator.

If the second iterator has more elements than the first one the trailing elements are ignored.


Constructor Summary
ElementwiseIterator(BinaryFunction<? super S1,? super S2,? extends E> operation, Iterator<S1> iterator1, Iterator<S2> iterator2)
          Constructs a new ElementwiseIterator.
 
Method Summary
 Iterator<S1> getIterator1()
          Returns the first underlying iterator.
 Iterator<S2> getIterator2()
          Returns the second underlying iterator.
 BinaryFunction<? super S1,? super S2,? extends E> getOperation()
          Returns the underlying binary function.
 boolean hasNext()
          Returns true if the iteration has more elements.
 E next()
          Returns the next element in the interation.
 void remove()
          Removes from the underlying collection the last element returned by the iterator.
static
<T,S extends T>
T
sum(BinaryFunction<? super T,? super S,T> operation, Iterator<S> it)
          Sums up all elements of an iterator using the specified binary operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ElementwiseIterator

public ElementwiseIterator(BinaryFunction<? super S1,? super S2,? extends E> operation,
                           Iterator<S1> iterator1,
                           Iterator<S2> iterator2)
Constructs a new ElementwiseIterator.

Parameters:
operation - The operation to be used for computation.
iterator1 - An iterator conaining the first arguments.
iterator2 - An iterator conaining the second arguments.
Throws:
NullPointerException - if any argument is null.
Method Detail

getOperation

public BinaryFunction<? super S1,? super S2,? extends E> getOperation()
Returns the underlying binary function.

Returns:
the underlying binary function

getIterator1

public Iterator<S1> getIterator1()
Returns the first underlying iterator.

Returns:
the first underlying iterator.

getIterator2

public Iterator<S2> getIterator2()
Returns the second underlying iterator.

Returns:
the second underlying iterator.

hasNext

public boolean hasNext()
Returns true if the iteration has more elements. This implementation returns true if the first iterator has more elements.

Specified by:
hasNext in interface Iterator<E>
Returns:
true if the iteration has more elements.

next

public E next()
Returns the next element in the interation. This implementation checks hasNext first. Then it performs the base operation on the next elements of the two base iterators and returns the result. If either iterator has no more elements the next element of the other iterator is returned. If both iterators have no more elements an exception is thrown.

Specified by:
next in interface Iterator<E>
Returns:
the next element in the interation.
Throws:
NoSuchElementException - iteration has no more elements

remove

public void remove()
Removes from the underlying collection the last element returned by the iterator.

Specified by:
remove in interface Iterator<E>
Throws:
UnsupportedOperationException - if one or both of the underlying iterators' remove method is not supported.
IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method

sum

public static <T,S extends T> T sum(BinaryFunction<? super T,? super S,T> operation,
                                    Iterator<S> it)
Sums up all elements of an iterator using the specified binary operation. If the iterator is empty, null is returned.

Parameters:
operation - The operation to be used for computation.
it - iterator on which the computation is performed.
Returns:
the result of the computation over all elements in the specified iterator.
Throws:
NoSuchOperationException - if the iterator is empty and the binary operation does not have an identity element.