org.eclipse.emf.validation.util
Class FilteredCollection

java.lang.Object
  extended by java.util.AbstractCollection
      extended by org.eclipse.emf.validation.util.FilteredCollection
All Implemented Interfaces:
Iterable, Collection

public class FilteredCollection
extends AbstractCollection

Wrapper for a Java 2 Collection that provides a filtered view of its contents according to a client-specified filter algorithm.


Nested Class Summary
static interface FilteredCollection.Filter
          Interface for the algorithm that determines which elements are in and which are out of the filtered collection.
 
Constructor Summary
FilteredCollection(Collection collection, FilteredCollection.Filter filter)
           Initializes me to filter the specified collection, obtained independently.
 
Method Summary
 FilteredCollection.Filter getFilter()
          Retrieves the filter with which I was initialized.
 Iterator iterator()
          Obtains an iterator that dynamically filters out unwanted items using my filter algorithm.
 int size()
           Computes the size of the filtered view, i.e.
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, remove, 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 collection,
                          FilteredCollection.Filter filter)

Initializes me to filter the specified collection, obtained independently.

Note that it is a very bad idea to modify the wrapped collection after creating this filtered view on it. The results are undefined, but probably not what you want.

Parameters:
collection - the collection that I am to filter
filter - the filter algorithm to apply
Method Detail

getFilter

public final FilteredCollection.Filter getFilter()
Retrieves the filter with which I was initialized. Note that the result of modifying this filter's algorithm while I am using it is undefined, but not likely to be what you want.

Returns:
my filter

iterator

public Iterator iterator()
Obtains an iterator that dynamically filters out unwanted items using my filter algorithm.

Specified by:
iterator in interface Iterable
Specified by:
iterator in interface Collection
Specified by:
iterator in class AbstractCollection
Returns:
an iterator the exposes only the elements of my wrapped collection that match my filter

size

public int size()

Computes the size of the filtered view, i.e. the number of elements in the original collection that match my filter, by iterating myself.

Note that my size is recounted every time that it is requested, in case my filter's algorithm is changed or the contents of the underlying collection are changed.

Specified by:
size in interface Collection
Specified by:
size in class AbstractCollection
Returns:
the number of elements in my wrapped collection that match my filter

Copyright 2002, 2007 IBM Corporation and others.
All Rights Reserved.