KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ============================================================================
2
// $Id: Find.java,v 1.17 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
package net.sf.jga.fn.algorithm;
33
34 import java.util.Iterator JavaDoc;
35 import net.sf.jga.fn.UnaryFunctor;
36 import net.sf.jga.fn.comparison.EqualTo;
37 import net.sf.jga.fn.comparison.Equality;
38 import net.sf.jga.util.FindIterator;
39
40 /**
41  * Locates values in an iteration.
42  * <p>
43  * Copyright &copy; 2003-2005 David A. Hall
44  *
45  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
46  * @deprecated
47  */

48
49 public class Find<T> extends FindIteratorFunctor<T> {
50     
51     static final long serialVersionUID = -556722539704270804L;
52     
53     // the functor used to determine if an element is the one being searched for
54
private UnaryFunctor<T,Boolean JavaDoc> _eq;
55
56     /**
57      * Builds a Find functor that looks for the given value, using the value's
58      * equals() method.
59      */

60     public Find (T value) {
61         this(new EqualTo<T>().bind2nd(value));
62     }
63
64     /**
65      * Builds a Find functor that looks for the given value, using the given
66      * Equality predicate.
67      */

68     public Find (Equality<T> eq, T value) {
69         this(eq.bind2nd(value));
70     }
71
72     /**
73      * Builds a Find functor that looks for values for which the given predicate
74      * returns TRUE.
75      */

76     public Find (UnaryFunctor<T,Boolean JavaDoc> eq) {
77         _eq = eq;
78     }
79
80     /**
81      * Returns the functor used to determine if an element is the one being
82      * searched for.
83      */

84     public UnaryFunctor<T,Boolean JavaDoc> getComparisonFn() {
85         return _eq;
86     }
87
88     /**
89      * Locates the first/next element that meets the given criteria.
90      * @return an Iterator whose next() [if it hasNext()] will return an
91      * element that meets the given criteria. If no such element exists, then
92      * the returned iterator's hasNext() is false.
93      */

94     public FindIterator<T> fn (Iterator JavaDoc<? extends T> iterator) {
95         FindIterator<T> finder = wrap(iterator);
96         finder.findNext(_eq);
97         return finder;
98     }
99
100     /**
101      * Calls the Visitor's <code>visit(Find)</code> method, if it
102      * implements the nested Visitor interface.
103      */

104     public void accept(net.sf.jga.fn.Visitor v) {
105         if (v instanceof Find.Visitor)
106             ((Find.Visitor)v).visit(this);
107         else
108             v.visit(this);
109     }
110
111     // Object overrides
112

113     public String JavaDoc toString() {
114         return "Find["+_eq+"]";
115     }
116     
117     // AcyclicVisitor
118

119     /**
120      * Interface for classes that may interpret an <b>Find</b> functor.
121      */

122     public interface Visitor extends net.sf.jga.fn.Visitor {
123         public void visit(Find host);
124     }
125 }
126
Popular Tags