001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Collections;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: /**
036: * Filters is a class of static methods for filtering (subsetting) Collections.
037: *
038: * This is functionality which should be part of Java Collections framework
039: * but is sadly missing.
040: */
041:
042: public final class Filters {
043:
044: /** an empty collect to avoid consing 'em **/
045: private static final Collection emptyCollection = Collections
046: .unmodifiableCollection(new ArrayList(0));
047:
048: /** Compute the subset of the Collection which satisfies the Predicate.
049: * Any ordering of the original Collection is maintained.
050: * @return a Collection which is always actually an ArrayList.
051: **/
052: public static Collection filter(Collection c, UnaryPredicate p) {
053: ArrayList subset = null;
054:
055: for (Iterator i = c.iterator(); i.hasNext();) {
056: Object element = i.next();
057: if (p.execute(element)) {
058: if (subset == null)
059: subset = new ArrayList(5);
060: subset.add(element);
061: }
062: }
063: return (subset == null) ? emptyCollection : subset;
064: }
065:
066: /** Compute the subset of the List which satisfies the Predicate.
067: * Any ordering of the original List is maintained.
068: * @return a Collection which is always actually an ArrayList.
069: **/
070: public static Collection filter(List c, UnaryPredicate p) {
071: ArrayList subset = null;
072: int l = c.size();
073: for (int i = 0; i < l; i++) {
074: Object element = c.get(i);
075: if (p.execute(element)) {
076: if (subset == null)
077: subset = new ArrayList(5);
078: subset.add(element);
079: }
080: }
081: return (subset == null) ? emptyCollection : subset;
082: }
083:
084: /** Compute the subset of the Collection which satisfies the Predicate.
085: * Any ordering of the original Collection is maintained.
086: * @return a Collection which is always actually an ArrayList.
087: **/
088: /** Compute the subset of the Collection underlying the specified Iterator
089: * which satisfies the Predicate.
090: * Any ordering of the original Collection is maintained.
091: * @param iterator Iterator over Collection to be filtered.
092: * @return a Collection which is always actually an ArrayList.
093: **/
094: public static Collection filter(Iterator iterator, UnaryPredicate p) {
095: ArrayList subset = null;
096: while (iterator.hasNext()) {
097: Object element = iterator.next();
098: if (p.execute(element)) {
099: if (subset == null)
100: subset = new ArrayList(5);
101: subset.add(element);
102: }
103: }
104: return (subset == null) ? emptyCollection : subset;
105: }
106:
107: /** @return the first object in Collection which satisfies
108: * the Predicate or null.
109: */
110: public static Object findElement(Collection c, UnaryPredicate p) {
111: for (Iterator i = c.iterator(); i.hasNext();) {
112: Object element = i.next();
113: if (p.execute(element))
114: return element;
115: }
116: return null;
117: }
118: }
|