KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ============================================================================
2
// $Id: FindAdjacent.java,v 1.19 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 net.sf.jga.fn.BinaryFunctor;
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.LookAheadIterator;
39 import java.util.Iterator JavaDoc;
40
41
42 /**
43  * Locates pairs of adjacent values in an iteration.
44  * <p>
45  * Copyright &copy; 2003-2005 David A. Hall
46  *
47  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
48  * @deprecated
49  */

50
51 public class FindAdjacent<T> extends LookAheadFunctor<T> {
52
53     static final long serialVersionUID = 6357244961374202731L;
54     
55     // The functor used to determine if two adjacent values are the same
56
private BinaryFunctor<T,T,Boolean JavaDoc> _eq;
57
58     /**
59      * Builds a FindAdjacent functor that uses the equals() method to determine
60      * if adjacent values are the same.
61      */

62     public FindAdjacent () {
63         this(new EqualTo<T>());
64     }
65
66     /**
67      * Builds a FindAdjacent functor that uses the given predicate to determine
68      * if adjacent values are the same. The functor argument is expected to
69      * compare two values and return TRUE if they are considered to be equal..
70      */

71     public FindAdjacent (BinaryFunctor<T,T,Boolean JavaDoc> eq) {
72         _eq = eq;
73     }
74
75     /**
76      * Returns the functor used to determine if two adjacent values are the same
77      */

78     public BinaryFunctor<T,T,Boolean JavaDoc> getComparisonFn() {
79         return _eq;
80     }
81
82     /**
83      * Locates the first/next pair of adjacent elements in an iteration that
84      * are the same value.
85      * @return an iterator whose next() [if it hasNext()] points to the first of
86      * a pair of adjacent equivalent values. If no such pair exists, then the
87      * iterator's hasNext() will be false.
88      */

89     public LookAheadIterator<T> fn (Iterator<? extends T> iterator) {
90         // return early If the input iterator is finished,
91
if (!iterator.hasNext()) {
92             return new LookAheadIterator<T>(iterator, 1);
93         }
94         
95         LookAheadIterator<T> lai = wrap(iterator, 2);
96         while (lai.hasNextPlus(2)) {
97             T arg1 = lai.peek(1);
98             T arg2 = lai.peek(2);
99             if (_eq.fn(arg1, arg2)) {
100                 return lai;
101             }
102
103             lai.next();
104         }
105
106         // didn't find anything, so we advance our working iterator off the end
107
// and return it.
108
lai.next();
109         return lai;
110     }
111
112
113     /**
114      * Calls the Visitor's <code>visit(FindAdjacent)</code> method, if it
115      * implements the nested Visitor interface.
116      */

117     public void accept(net.sf.jga.fn.Visitor v) {
118         if (v instanceof FindAdjacent.Visitor)
119             ((FindAdjacent.Visitor)v).visit(this);
120         else
121             v.visit(this);
122     }
123
124     // Object overrides
125

126     public String JavaDoc toString() {
127         return "FindAdjacent["+_eq+"]";
128     }
129     
130     // AcyclicVisitor
131

132     /**
133      * Interface for classes that may interpret an <b>FindAdjacent</b> functor.
134      */

135     public interface Visitor extends net.sf.jga.fn.Visitor {
136         public void visit(FindAdjacent host);
137     }
138 }
139
Popular Tags