net.sf.molae.pipe.trafo
Class TransformedCollection<S,E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by net.sf.molae.pipe.trafo.TransformedCollection<S,E>
All Implemented Interfaces:
Iterable<E>, Collection<E>

public final class TransformedCollection<S,E>
extends AbstractCollection<E>

Provides a different view to an existing collection by transforming each element.


Constructor Summary
TransformedCollection(Collection<S> base, TwoWayTransformer<S,E> transformer)
          Constructs a TransformedCollection object.
 
Method Summary
 boolean add(E o)
          Ensures that the base collection contains an element that transforms into the specified element.
 void clear()
          Removes all of the elements from the base collection.
 boolean contains(Object o)
          Returns true if the base collection contains an element that would transform into the specified element.
 boolean isEmpty()
          Returns true if the base collection contains no elements.
 Iterator<E> iterator()
           
static
<S,E> TransformedCollection<S,E>
newInstance(Collection<S> base, TwoWayTransformer<S,E> transformer)
          Constructs a TransformedCollection object.
 boolean remove(Object o)
          Removes an element from the base list that would transform into the specified object, if such an element exists.
 int size()
          Returns the number of elements in the base collection.
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

TransformedCollection

public TransformedCollection(Collection<S> base,
                             TwoWayTransformer<S,E> transformer)
Constructs a TransformedCollection object.

Parameters:
base - the underlying collection
transformer - the transformer that maps each element to another one.
Throws:
NullPointerException - if any of the specified objects is null.
Method Detail

newInstance

public static <S,E> TransformedCollection<S,E> newInstance(Collection<S> base,
                                                           TwoWayTransformer<S,E> transformer)
Constructs a TransformedCollection object.

Parameters:
base - the underlying collection
transformer - the transformer that maps each element to another one.
Throws:
NullPointerException - if any of the specified objects is null.
Since:
2.0

size

public int size()
Returns the number of elements in the base collection.

Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the number of elements in the base collection.

isEmpty

public boolean isEmpty()
Returns true if the base collection contains no elements.

Specified by:
isEmpty in interface Collection<E>
Overrides:
isEmpty in class AbstractCollection<E>
Returns:
true if the base collection contains no elements.

contains

public boolean contains(Object o)
Returns true if the base collection contains an element that would transform into the specified element.

Specified by:
contains in interface Collection<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
o - element whose presence in this collection is to be tested.
Returns:
true if the base collection contains an element that would transform into the specified element.

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class AbstractCollection<E>

add

public boolean add(E o)
Ensures that the base collection contains an element that transforms into the specified element.

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractCollection<E>
Throws:
UnsupportedOperationException - depending on the base class and the transformer
ClassCastException - class of the specified element prevents finding a source element for it.
IllegalArgumentException - some aspect of this element prevents finding a source element for it.

remove

public boolean remove(Object o)
Removes an element from the base list that would transform into the specified object, if such an element exists.

Specified by:
remove in interface Collection<E>
Overrides:
remove in class AbstractCollection<E>
Returns:
true if the base collection changed as a result of the call

clear

public void clear()
Removes all of the elements from the base collection.

Specified by:
clear in interface Collection<E>
Overrides:
clear in class AbstractCollection<E>
Throws:
UnsupportedOperationException - depending on the base collection