public < E extends T > Collection < E > createView ( final Collection < E > input ) { // TODO: create a collection of the same type? return new AbstractCollection < E > ( ) { @ Override public int size ( ) { // oh god, oh god: we iterate and count int size = 0 ; for ( Iterator < E > iter = iterator ( ) ; iter . hasNext ( ) ; ) { iter . next ( ) ; size ++ ; } return size ; } @ Override public boolean add ( E element ) { return input . add ( element ) ; } @ Override public boolean remove ( Object element ) { return input . remove ( element ) ; } @ Override public boolean contains ( Object element ) { try { @ SuppressWarnings ( "unchecked" ) E elem = ( E ) element ; return isMatch ( elem ) && input . contains ( elem ) ; } catch ( ClassCastException cce ) { // since it's not an E, it can't be a member of our view return false ; } } @ Override public Iterator < E > iterator ( ) { return filter ( input . iterator ( ) ) ; } } ; }
Create a view of the specified collection that only contains elements that match the predicate . This collection can be examined at any time in the future to view the current predicate - matching elements of the input collection .