001: // ============================================================================
002: // $Id: FindElement.java,v 1.15 2006/12/05 04:52:38 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:
033: package net.sf.jga.fn.algorithm;
034:
035: import java.util.Collection;
036: import java.util.Collections;
037: import java.util.Iterator;
038: import java.util.Vector;
039: import net.sf.jga.fn.BinaryFunctor;
040: import net.sf.jga.fn.UnaryFunctor;
041: import net.sf.jga.util.FindIterator;
042:
043: /**
044: * Locates values from a given collection in an iteration.
045: * <p>
046: * To Serialize a FindElement, the generic parameter T must be serializable.
047: * <p>
048: * Copyright © 2003-2005 David A. Hall
049: *
050: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
051: * @deprecated
052: **/
053:
054: public class FindElement<T> extends FindIteratorFunctor<T> {
055:
056: static final long serialVersionUID = -1746637029942790280L;
057:
058: // The predicate used to compare values; can be null
059: private BinaryFunctor<T, T, Boolean> _bf;
060:
061: // The predicate used to find the next qualifying element in an iteration
062: private UnaryFunctor<T, Boolean> _uf;
063:
064: // The collection of values that are being sought
065: private Collection<? extends T> _elements;
066:
067: /**
068: * Builds a FindElement functor that locates values in the given collection
069: * using the contains() method.
070: */
071: public FindElement(Collection<? extends T> elements) {
072: _elements = (elements == null) ? new Vector<T>() : elements;
073: _uf = new ElementOf<T>().bind2nd(elements);
074: }
075:
076: /**
077: * Builds a FindElement functor that locates values in the given collection
078: * using the given functor. The functor is expected to compare two values
079: * and return TRUE if they are determined to be equal.
080: */
081: public FindElement(Collection<? extends T> elements,
082: BinaryFunctor<T, T, Boolean> eq) {
083: _bf = eq;
084: _elements = (elements == null) ? new Vector<T>() : elements;
085: _uf = new ElementOf<T>(eq).bind2nd(elements);
086: }
087:
088: /**
089: * Returns the set of values being sought.
090: */
091: public Collection<? extends T> getElementSet() {
092: return Collections.unmodifiableCollection(_elements);
093: }
094:
095: /**
096: * Returns the (possibly null) functor used to compare a value to the
097: * contents of the given collection.
098: */
099: public BinaryFunctor<T, T, Boolean> getComparisonFn() {
100: return _bf;
101: }
102:
103: // UnaryFunctor Interface
104:
105: /**
106: * Finds the first/next element in the iteration that is an element of the
107: * given collection.
108: *
109: * @return an iterator whose next() [if it hasNext()] points to the next
110: * element in the iteration that is an element of the given collection. If
111: * no such element exists, then the returned iterator's hasNext() will be
112: * false.
113: */
114: public FindIterator<T> fn(Iterator<? extends T> iterator) {
115: FindIterator<T> finder = wrap(iterator);
116: finder.findNext(_uf);
117: return finder;
118: }
119:
120: /**
121: * Calls the Visitor's <code>visit(FindElement)</code> method, if it
122: * implements the nested Visitor interface.
123: */
124: public void accept(net.sf.jga.fn.Visitor v) {
125: if (v instanceof FindElement.Visitor)
126: ((FindElement.Visitor) v).visit(this );
127: else
128: v.visit(this );
129: }
130:
131: // Object overrides
132:
133: public String toString() {
134: return "FindElement";
135: }
136:
137: // AcyclicVisitor
138:
139: /**
140: * Interface for classes that may interpret a <b>FindElement</b>
141: * functor
142: */
143: public interface Visitor extends net.sf.jga.fn.Visitor {
144: public void visit(FindElement host);
145: }
146:
147: }
|