KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ============================================================================
2
// $Id: FindRepeated.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

33 package net.sf.jga.fn.algorithm;
34
35 import java.util.Iterator JavaDoc;
36 import net.sf.jga.fn.UnaryFunctor;
37 import net.sf.jga.fn.UnaryFunctor;
38 import net.sf.jga.fn.comparison.EqualTo;
39 import net.sf.jga.fn.comparison.Equality;
40 import net.sf.jga.util.EmptyIterator;
41 import net.sf.jga.util.LookAheadIterator;
42
43 /**
44  * Locates runs of repeated values in an iteration.
45  * <p>
46  * Copyright &copy; 2003-2005 David A. Hall
47  *
48  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
49  * @deprecated
50  */

51
52 public class FindRepeated<T> extends LookAheadFunctor<T> {
53     
54     static final long serialVersionUID = 2382887791885942503L;
55
56     // the size of the run sought
57
private int _repeatCount;
58
59     // functor used to determine if the element should be included in the run
60
private UnaryFunctor<T,Boolean JavaDoc> _eq;
61
62     /**
63      * Builds a FindRepeated functor that will look for a run of the given size,
64      * using the equals() method.
65      */

66     public FindRepeated (int count, T value) {
67         this(count, new EqualTo<T>().bind2nd(value));
68     }
69
70     /**
71      * Builds a FindRepeated functor that will look for a run of the given size,
72      * using the given equality functor.
73      */

74     public FindRepeated (int count, T value, Equality<T> eq) {
75         this(count, eq.bind2nd(value));
76     }
77
78     /**
79      * Builds a FindRepeated functor that will look for a run of the given size,
80      * using the given functor. The functor is expected to return TRUE if the
81      * element should be included in the run.
82      */

83     public FindRepeated (int count, UnaryFunctor<T,Boolean JavaDoc> eq){
84         if (count < 0)
85             throw new IllegalArgumentException JavaDoc("count < 0");
86         
87         _repeatCount = count;
88         _eq = eq;
89     }
90
91     /**
92      * Returns the length of the run being sought
93      */

94     public int getRunLength() {
95         return _repeatCount;
96     }
97
98     /**
99      * Returns the functor used to determine if the element should be included
100      * in the run
101      */

102     public UnaryFunctor<T,Boolean JavaDoc> getComparisonFn() {
103         return _eq;
104     }
105
106     /**
107      * Locates the first/next run of the given length containing elements that
108      * meet the given criteria.
109      * @return an Iterator whose next() [if it hasNext()] points at the first
110      * element in the desired run. If no such run of elements exists, then the
111      * returned iterator's hasNext() will be false.
112      */

113     public LookAheadIterator<T> fn (Iterator JavaDoc<? extends T> iterator) {
114         // return early if the input iterator is already finished,
115
if (!iterator.hasNext() || _repeatCount == 0) {
116             return new LookAheadIterator<T>(iterator, 1);
117         }
118
119         LookAheadIterator<T> lai = wrap(iterator, _repeatCount);
120         
121     OUTER:
122         while (lai.hasNextPlus(_repeatCount)) {
123
124             // ... examine the contents of the look ahead buffer ...
125
for (int i = 1; i <= _repeatCount; ++i) {
126                 T arg = lai.peek(i);
127                 
128                 // ... and if we find something in the buffer that isn't
129
// 'equal', then we'll advance past that point in the iterator
130
// and try again
131
if ( ! _eq.fn(arg)) {
132                     for (int j = i; j > 0; --j) {
133                         lai.next();
134                     }
135
136                     continue OUTER;
137                 }
138             }
139
140             // If we safely got off the end of the INNER loop, then we must
141
// have found the point we're looking for.
142
return lai;
143         }
144
145         // didn't find anything, make an iterator that will return false.
146
return new LookAheadIterator<T>(new EmptyIterator<T>(), 1);
147     }
148
149     /**
150      * Calls the Visitor's <code>visit(FindRepeated)</code> method, if it
151      * implements the nested Visitor interface.
152      */

153     public void accept(net.sf.jga.fn.Visitor v) {
154         if (v instanceof FindRepeated.Visitor)
155             ((FindRepeated.Visitor)v).visit(this);
156         else
157             v.visit(this);
158     }
159
160     // Object overrides
161

162     public String JavaDoc toString() {
163         return "FindRepeated["+_eq+"]";
164     }
165     
166     // AcyclicVisitor
167

168     /**
169      * Interface for classes that may interpret an <b>FindRepeated</b> functor.
170      */

171     public interface Visitor extends net.sf.jga.fn.Visitor {
172         public void visit(FindRepeated host);
173     }
174 }
175
Popular Tags