[ERA-CVS] src/org/jdaemon/util/functional FilterList.java, NONE, 1.1 FilterIterator.java, NONE, 1.1
Brought to you by:
jessex
|
From: <je...@us...> - 2008-07-30 23:41:24
|
Update of /cvsroot/era/src/org/jdaemon/util/functional In directory sc8-pr-cvs17.sourceforge.net:/tmp/cvs-serv15255 Modified Files: Operations.java Added Files: FilterList.java FilterIterator.java FilterCollection.java Log Message: CompoundComparator - create composite comparators --- NEW FILE: FilterList.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.functional; import java.util.AbstractList; import java.util.List; import java.util.ArrayList; /** Lazy filter for lists. * <p> * A list which appears to contain all the elements in some parent collection for * which some filter evaluates to true. Evaluation is lazy so the filtering is * performed only when required (i.e. by get(i) or size() rather than on construction. * </p> * <p> * Filtered elements are cached so a call to get(i) followed by a call to get(j) will * not involve any filtering operation so long as j <= i. A call to size() of necessity * requires that all elements are filtered so after size() is called, no further call to get() * or size() will involve any filtering operation. * </p> * <p> * At the moment the iterator implementation is naive (inherited from AbstractList) so * will presumably call size() and this perform all filtering operations in one fell swoop. * </p> * * @author jonathan */ class FilterList<T> extends AbstractList<T> { private ArrayList<T> cache; private List<? extends T> base; private UnaryFunction<T,Boolean> filter; private int top; /** Create a list containing only those elements from to_filter for which filter.operate(i) * evaluates to true. * * @param to_filter List to filter * @param filter Unary function to choose elements in result list */ public FilterList(List<? extends T> to_filter, UnaryFunction<T,Boolean> filter) { this.base = to_filter; this.filter = filter; this.cache = new ArrayList<T>(); this.top = 0; } /** Get ith element from this list. * * Gets the ith element from the to_filter list for which filter.operate(elem) evaluates * to true. Caches filtered elements, so criteria are evaluated only once for each element. * * @param i Index of element to return */ @Override public T get(int i) { while (i >= cache.size()) { T elem = base.get(top); if (filter.operate(elem)) { cache.add(elem); } top++; } return cache.get(i); } /** Get the number of elements in this list. * * Evaluates to the number of elements in the to_filter list for which filter.operate(elem) * evaluates to true. * * @return the size of this list. */ @Override public int size() { while (top < base.size()) { T elem = base.get(top); if (filter.operate(elem)) { cache.add(elem); } top++; } return cache.size(); } } --- NEW FILE: FilterIterator.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.functional; import java.util.Iterator; /** * * @author jonathan */ public class FilterIterator<T> implements Iterator<T> { private Iterator<? extends T> parent; private T next; private boolean next_ok; private UnaryFunction<T,Boolean> filter; private void move() { do { next = parent.next(); next_ok = filter.operate(next); } while (!next_ok && parent.hasNext()); } public boolean hasNext() { return next_ok; } public T next() { T retval = next; move(); return retval; } public void remove() { throw new UnsupportedOperationException("Not supported yet."); } public FilterIterator(Iterator<? extends T> to_filter, UnaryFunction<T,Boolean> filter_p) { parent = to_filter; filter = filter_p; next_ok = false; next = null; move(); } } --- NEW FILE: FilterCollection.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.util.functional; import java.util.AbstractCollection; import java.util.Collection; import java.util.Iterator; /** Lazy-filtered collection. * * Note that any changes to the base collection invalidate the FilterCollection. * * @author jonathan * */ class FilterCollection<T> extends AbstractCollection<T> { private Collection<? extends T> base; private UnaryFunction<T,Boolean> filter; private int size; /** Constructor. * * Creates a collection which appears to contain all the elements i of to_filter for * which filter.operate(i) evaluates to true. Note that the constructor does no work and * only the size of the resultant collection is cached. Filter.operate is called once * for every invocation of next() on the iterator returned by this object's iterator method. * * @param to_filter Collection to filter * @param filter Function to determine which elements are in the filtered collection. */ public FilterCollection(Collection<? extends T> to_filter, UnaryFunction<T,Boolean> filter) { this.base = to_filter; this.filter = filter; this.size = -1; // -1 indicates size unknown. } /** Create an iterator for this collection. * * Changes to the to_filter collection provided in the constructor invalidate any iterator * created by this method. Also the returned iterator will raise an exception if remove() is * called. * * @return An iterator object. */ @Override public Iterator<T> iterator() { return new FilterIterator<T>(base.iterator(), filter); } /** Calculate or return cached size. * * Remember: this is a read-only collection. Change that, and you'd have to * clear the size cache every time the colleciton is changed. * * @return Number of elements in base collection for which filter condition is true. */ @Override public int size() { if (size == -1) { size = 0; for (T item : base) { if (filter.operate(item)) size++; } } return size; } } Index: Operations.java =================================================================== RCS file: /cvsroot/era/src/org/jdaemon/util/functional/Operations.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Operations.java 29 Jul 2008 22:41:07 -0000 1.3 --- Operations.java 30 Jul 2008 23:41:20 -0000 1.4 *************** *** 9,12 **** --- 9,13 ---- import java.util.List; + import java.util.ArrayList; /** Library of operations which can be performed on UnaryFunction and BinaryFunction objects. *************** *** 151,154 **** } } ! } } --- 152,189 ---- } } ! } ! ! /** Filter a collection. ! * <p> ! * Returns a new collection, containing all the elements of <i>collection</i> for which ! * filter.operate(element) evaluates to true. ! * </p><p> ! * The implementation is lazy; This actual operation is lightweight. The filter critera is evaluated ! * only as elements in the result collection are actually accessed. ! * </p> ! * @param collection Collection to filter ! * @param filter Critera to triage collection ! * @return A collection containing all elements in <I>collection</I> for which filter.operate(elem) is true ! */ ! public static <I> Collection<I> filter(Collection<? extends I> collection, UnaryFunction<I,Boolean> filter) { ! return new FilterCollection<I>(collection, filter); ! } ! ! /** Filter a list. ! * <p> ! * Returns a new list, containing all the elements of <i>list</i> for which ! * filter.operate(element) evaluates to true. ! * </p><p> ! * The implementation is lazy; This actual operation is lightweight. The filter critera is evaluated ! * only as elements in the result list are actually accessed. ! * </p> ! * @param list List to filter ! * @param filter Critera to triage list ! * @return A list containing all elements in <I>list</I> for which filter.operate(elem) is true ! */ ! public static <I> List<I> filter(List<? extends I> list, UnaryFunction<I,Boolean> filter) { ! return new FilterList<I>(list, filter); ! } ! ! } |