001: // ============================================================================
002: // $Id: ElementOf.java,v 1.11 2006/01/08 00:52:25 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032: package net.sf.jga.fn.algorithm;
033:
034: import java.util.Collection;
035: import net.sf.jga.fn.BinaryFunctor;
036: import net.sf.jga.fn.UnaryFunctor;
037: import net.sf.jga.fn.BinaryPredicate;
038: import net.sf.jga.util.FindIterator;
039:
040: /**
041: * BinaryPredicate that returns true if a given value is an element of a
042: * given collection.
043: * <p>
044: * Copyright © 2003-2005 David A. Hall
045: *
046: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
047: */
048:
049: public class ElementOf<T> extends
050: BinaryPredicate<T, Collection<? extends T>> {
051:
052: static final long serialVersionUID = 4639100512962835854L;
053:
054: // The functor used to compare the element to the items in the collection
055: private BinaryFunctor<T, T, Boolean> _eq;
056:
057: /**
058: * Builds an ElementOf predicate that will use the collection's built in
059: * contains() method. This form is potentially more efficient than the
060: * other constructed form, if the collection passed at evaluation
061: * implements contains() using an algorithm more efficient than a linear
062: * search.
063: */
064: public ElementOf() {
065: }
066:
067: /**
068: * Builds an ElementOf predicate that will use the given functor to
069: * determine collection membership. The collection will be searched
070: * sequentially for the first element for which the functor returns true.
071: */
072: public ElementOf(BinaryFunctor<T, T, Boolean> eq) {
073: _eq = eq;
074: }
075:
076: /**
077: * Returns the (possibly null) functor used to compare a value to the
078: * contents of a collection.
079: */
080: public BinaryFunctor<T, T, Boolean> getComparisonFn() {
081: return _eq;
082: }
083:
084: /**
085: * Return true if the given value is an element of the collection
086: */
087: public Boolean fn(T value, Collection<? extends T> collection) {
088: if (_eq == null)
089: return collection.contains(value);
090: else {
091: FindIterator<T> finder = new FindIterator<T>(collection
092: .iterator());
093: UnaryFunctor<T, Boolean> uf = _eq.bind2nd(value);
094: return finder.findNext(uf);
095: }
096: }
097:
098: /**
099: * Calls the Visitor's <code>visit(ElementOf)</code> method, if it
100: * implements the nested Visitor interface.
101: */
102: public void accept(net.sf.jga.fn.Visitor v) {
103: if (v instanceof ElementOf.Visitor)
104: ((ElementOf.Visitor) v).visit(this );
105: else
106: v.visit(this );
107: }
108:
109: // Object overrides
110:
111: public String toString() {
112: return "ElementOf";
113: }
114:
115: // AcyclicVisitor
116:
117: /**
118: * Interface for classes that may interpret an <b>ElementOf</b> functor.
119: */
120: public interface Visitor extends net.sf.jga.fn.Visitor {
121: public void visit(ElementOf host);
122: }
123: }
|