KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > algorithm > FindElement


1 // ============================================================================
2
// $Id: FindElement.java,v 1.15 2006/12/05 04:52:38 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.algorithm;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Vector JavaDoc;
39 import net.sf.jga.fn.BinaryFunctor;
40 import net.sf.jga.fn.UnaryFunctor;
41 import net.sf.jga.util.FindIterator;
42
43 /**
44  * Locates values from a given collection in an iteration.
45  * <p>
46  * To Serialize a FindElement, the generic parameter T must be serializable.
47  * <p>
48  * Copyright &copy; 2003-2005 David A. Hall
49  *
50  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
51  * @deprecated
52  **/

53
54 public class FindElement<T> extends FindIteratorFunctor<T> {
55     
56     static final long serialVersionUID = -1746637029942790280L;
57
58     // The predicate used to compare values; can be null
59
private BinaryFunctor<T,T,Boolean JavaDoc> _bf;
60
61     // The predicate used to find the next qualifying element in an iteration
62
private UnaryFunctor<T,Boolean JavaDoc> _uf;
63
64     // The collection of values that are being sought
65
private Collection JavaDoc<? extends T> _elements;
66
67     /**
68      * Builds a FindElement functor that locates values in the given collection
69      * using the contains() method.
70      */

71     public FindElement(Collection JavaDoc<? extends T> elements) {
72         _elements = (elements == null) ? new Vector JavaDoc<T>() : elements;
73         _uf = new ElementOf<T>().bind2nd(elements);
74     }
75
76     /**
77      * Builds a FindElement functor that locates values in the given collection
78      * using the given functor. The functor is expected to compare two values
79      * and return TRUE if they are determined to be equal.
80      */

81     public FindElement(Collection JavaDoc<? extends T> elements,
82                        BinaryFunctor<T,T,Boolean JavaDoc> eq )
83     {
84         _bf = eq;
85         _elements = (elements == null) ? new Vector JavaDoc<T>() : elements;
86         _uf = new ElementOf<T>(eq).bind2nd(elements);
87     }
88
89     /**
90      * Returns the set of values being sought.
91      */

92     public Collection JavaDoc<? extends T> getElementSet() {
93         return Collections.unmodifiableCollection(_elements);
94     }
95
96     /**
97      * Returns the (possibly null) functor used to compare a value to the
98      * contents of the given collection.
99      */

100     public BinaryFunctor<T,T,Boolean JavaDoc> getComparisonFn() {
101         return _bf;
102     }
103
104     // UnaryFunctor Interface
105

106     /**
107      * Finds the first/next element in the iteration that is an element of the
108      * given collection.
109      *
110      * @return an iterator whose next() [if it hasNext()] points to the next
111      * element in the iteration that is an element of the given collection. If
112      * no such element exists, then the returned iterator's hasNext() will be
113      * false.
114      */

115     public FindIterator<T> fn(Iterator JavaDoc<? extends T> iterator) {
116         FindIterator<T> finder = wrap(iterator);
117         finder.findNext(_uf);
118         return finder;
119     }
120
121     /**
122      * Calls the Visitor's <code>visit(FindElement)</code> method, if it
123      * implements the nested Visitor interface.
124      */

125     public void accept(net.sf.jga.fn.Visitor v) {
126         if (v instanceof FindElement.Visitor)
127             ((FindElement.Visitor)v).visit(this);
128         else
129             v.visit(this);
130     }
131     
132     // Object overrides
133

134     public String JavaDoc toString() {
135         return "FindElement";
136     }
137     
138     // AcyclicVisitor
139

140     /**
141      * Interface for classes that may interpret a <b>FindElement</b>
142      * functor
143      */

144     public interface Visitor extends net.sf.jga.fn.Visitor {
145         public void visit(FindElement host);
146     }
147
148 }
149
Popular Tags