net.sf.molae.pipe.filter
Class FilteredCollection<E>

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

public final class FilteredCollection<E>
extends AbstractCollection<E>

Implements a collection that contains all elements of a base collection that match a filter condition.


Constructor Summary
FilteredCollection(Collection<E> base, TypedPredicate<? super E> filter)
          Constructs a filtered collection from the specified collection and filter.
 
Method Summary
 boolean add(E o)
          Ensures that this collection contains the specified element if the element matches the filter.
protected  void assertRange(E o)
          Checks if the specified object fulfils the filter predicate of this class.
 boolean contains(Object o)
          Returns true if the base collection contains the specified element and the element matches the underlying filter.
protected  Collection<E> getBase()
          Returns the underlying collection.
protected  TypedPredicate<Object> getFilter()
          Returns the underlying filter.
 Iterator<E> iterator()
           
 boolean remove(Object o)
          Removes a single instance of the specified element from the base collection, if it is present and matches the underlying filter.
 int size()
           
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, containsAll, isEmpty, 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

FilteredCollection

public FilteredCollection(Collection<E> base,
                          TypedPredicate<? super E> filter)
Constructs a filtered collection from the specified collection and filter.

Parameters:
base - the underlying collection
filter - predicate that determines which objects belong to the collection
Throws:
NullPointerException - if the specified objects are null.
Method Detail

getBase

protected Collection<E> getBase()
Returns the underlying collection.

Returns:
the underlying collection.

getFilter

protected TypedPredicate<Object> getFilter()
Returns the underlying filter.

Returns:
the underlying filter

assertRange

protected void assertRange(E o)
Checks if the specified object fulfils the filter predicate of this class.

Parameters:
o - object to evaluate.
Throws:
IllegalArgumentException - if the specified object does not fulfil the filter predicate of this class.

size

public int size()
Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>

contains

public boolean contains(Object o)
Returns true if the base collection contains the specified element and the element matches the underlying filter.

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 the specified element and the element matches the underlying filter

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 this collection contains the specified element if the element matches the filter.

Specified by:
add in interface Collection<E>
Overrides:
add in class AbstractCollection<E>
Throws:
UnsupportedOperationException - depending on the base collection
ClassCastException - depending on the base collection
IllegalArgumentException - if the specified object does not fulfil the filter predicate of this class or the base collection does not accept the element.

remove

public boolean remove(Object o)
Removes a single instance of the specified element from the base collection, if it is present and matches the underlying filter.

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