E
- The type of the elements contained in this filter.public class Filter<E> extends Object implements Collection<E>, Iterable<E>
Collection
, the base
collection, that handles only members of a certain type.
Filters are purely virtual. Every operation that manipulates the filter
collection directly manipulates the base collection as well: E.g., if you
remove an item from the filtered collection, the
same item is removed from the base collection. This has the advantage of
being a live link and working with every base collection, but it makes
operations like size()
, toArray()
and
toArray(Object[])
potentially rather slow (because they must iterate
throw the base collection).
class Shape {}; class Circle extends Shape{}; ... List<Shape> shapes = new ArrayList<Shape>(); Collection<Circle> circles = new Filter<Circle>(shapes, Circle.class);
Constructor and Description |
---|
Filter(Collection<? super E> base,
Class<E> filterType)
Creates a filter based on the given collection.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E o) |
boolean |
addAll(Collection<? extends E> c) |
void |
clear()
Removes all objects from the underlying collection that are of this
filter's filter type.
|
boolean |
contains(Object o) |
boolean |
containsAll(Collection<?> c) |
Collection<? super E> |
getBase()
Returns the base collection for this filter.
|
Class<E> |
getFilterType()
The (virtual) filter collection contains of exactly those members of the
original collection that are assignment compatible to the filter type.
|
boolean |
isAllowed(Object o)
Returns true iff the given object would be allowed as a member of this
collection.
|
boolean |
isEmpty() |
FilteringIterator<E> |
iterator()
Returns an Iterator that iterates only over the filtered elements of the
base collection.
|
boolean |
remove(Object o) |
boolean |
removeAll(Collection<?> c) |
boolean |
retainAll(Collection<?> c) |
int |
size() |
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals, hashCode, parallelStream, removeIf, spliterator, stream
public Filter(Collection<? super E> base, Class<E> filterType)
Collection<Number> numbers = new ArrayList<Number>(); Collection<Integer> integers = new Filter<Integer>(numbers, Integer.class);
base
- The base collection, the elements of which are of a supertype
of filterTypefilterType
- The class for the return type of this filter. (Required
because generics are a compile-time only thing)public Collection<? super E> getBase()
public Class<E> getFilterType()
public boolean add(E o)
add
in interface Collection<E>
public boolean addAll(Collection<? extends E> c)
addAll
in interface Collection<E>
public void clear()
isEmpty()
is true, but not
necessarily getBase()
.isEmpty()
.clear
in interface Collection<E>
Collection.clear()
public boolean contains(Object o)
contains
in interface Collection<E>
public boolean containsAll(Collection<?> c)
containsAll
in interface Collection<E>
public boolean isAllowed(Object o)
o
- the object to test.getFilterType()
.public boolean isEmpty()
isEmpty
in interface Collection<E>
public FilteringIterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
Collection.iterator()
,
FilteringIterator
public boolean remove(Object o)
remove
in interface Collection<E>
public boolean removeAll(Collection<?> c)
removeAll
in interface Collection<E>
public boolean retainAll(Collection<?> c)
retainAll
in interface Collection<E>
public int size()
size
in interface Collection<E>
public Object[] toArray()
toArray
in interface Collection<E>
public <T> T[] toArray(T[] a)
toArray
in interface Collection<E>