KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

57
58 public class FindMismatch<T> extends LookAheadFunctor<T> {
59     
60     static final long serialVersionUID = -5406536818520367561L;
61     
62     // The collection of elements against which an iteration will be compared
63
private Collection JavaDoc<? extends T> _elements;
64
65     // functor used to compare an element in the iteration and the collection
66
private BinaryFunctor<T,T,Boolean JavaDoc> _bf;
67     
68     // The iterator used for a given call to fn
69
private LookAheadIterator<T> _elemIter;
70
71     // The element that was not matched
72
private T _mismatch;
73     
74     /**
75      * Builds a FindMismatch functor that uses &#33;equals() to compare elements.
76      * If the collection is null, an empty collection will be used for
77      * comparison.
78      */

79     public FindMismatch(Collection JavaDoc<? extends T> elements) {
80         this(elements, new NotEqualTo<T>());
81     }
82
83     /**
84      * Builds a FindMismatch functor that uses the given functor to compare
85      * elements. The functor is expected to compare two arguments and return
86      * TRUE if they are <b>not</b> equal. If the collection is null, an empty
87      * collection will be used for comparison.
88      */

89     public FindMismatch(Collection JavaDoc<? extends T> elements,
90                         BinaryFunctor<T,T,Boolean JavaDoc> neq)
91     {
92         _elements = (elements == null) ? new Vector JavaDoc<T>() : elements;
93         _bf = neq;
94     }
95
96     /**
97      * Builds one-time use FindMismatch finder that will test against the
98      * contents of an iteration rather than a collection. The functor is
99      * expected to compare two arguments and return TRUE if they are <b>not</b>
100      * equal.
101      * <p>
102      * A FindMismatch built this way cannot be used more than once as the
103      * contents of the iteration will be consumed in the first execution.
104      */

105     public FindMismatch(Iterator<? extends T> iter,
106                         BinaryFunctor<T,T,Boolean JavaDoc> neq)
107     {
108         if (iter == null)
109             iter = new EmptyIterator<T>();
110
111         _elemIter = new LookAheadIterator<T>(iter);
112         _bf = neq;
113     }
114
115     /**
116      * Returns the collection against which the argument will be compared. When
117      * called on a FindMismatch built with an iteration rather than a collection,
118      * this method will return null
119      */

120     public Collection JavaDoc<? extends T> getElements() {
121         return (_elements == null) ? null :
122             Collections.unmodifiableCollection(_elements);
123     }
124     
125     /**
126      * Returns the functor that is used to test matching elements
127      */

128     
129     public BinaryFunctor<T,T,Boolean JavaDoc> getComparisonFn() {
130         return _bf;
131     }
132
133     /**
134      * Returns the mismatched element in the given collection/iteration on the
135      * last call to fn(). If the iterator passed to the last call to fn() had
136      * no mismatch for the length of the collection, then this method returns
137      * null.
138      * @throws IllegalStateException if called before a call to fn.
139      */

140
141     // NOTE: the signature changed to fix bug 919269. The old signature was
142
// public LookAheadIterator<T> getMismatchedElement
143

144     public T getMismatchedElement() {
145         if (_elemIter == null)
146             throw new IllegalStateException JavaDoc();
147
148         return _mismatch;
149     }
150
151
152     // UnaryFunctor Interface
153

154     /**
155      * Locates the first/next element in an iteration that is not the same as
156      * the corresponding element in the given collection/iteration.
157      * @return an iterator whose next() [if it hasNext()] points to the next
158      * element in the iteration that does not match the corresponding element in
159      * the given collection. If no such element exists and the iteration is
160      * longer than the the given collection, then the returned iterator's next()
161      * points to the first element that does not exist in the given collection.
162      * If no mismatched element exists and the collection is at least as long as
163      * the iteration, then the returned iterator's hasNext() will be false.
164      */

165     public LookAheadIterator<T> fn(Iterator<? extends T> iterator) {
166         if (_elemIter == null)
167             _elemIter = new LookAheadIterator<T>(_elements.iterator());
168         
169         LookAheadIterator<T> lai = wrap(iterator, 1);
170         while (lai.hasNextPlus(1) && _elemIter.hasNextPlus(1)) {
171             T arg1 = lai.peek(1);
172             T arg2 = _elemIter.peek(1);
173             if (_bf.fn(arg1,arg2)) {
174                 _mismatch = _elemIter.next();
175                 return lai;
176             }
177             lai.next();
178             _elemIter.next();
179         }
180
181         return lai;
182     }
183
184     /**
185      * Calls the Visitor's <code>visit(FindMismatch)</code> method, if it
186      * implements the nested Visitor interface.
187      */

188     public void accept(net.sf.jga.fn.Visitor v) {
189         if (v instanceof FindMismatch.Visitor)
190             ((FindMismatch.Visitor)v).visit(this);
191         else
192             v.visit(this);
193     }
194     
195     // Object overrides
196

197     public String JavaDoc toString() {
198         return "FindMismatch";
199     }
200     
201     // AcyclicVisitor
202

203     /**
204      * Interface for classes that may interpret a <b>FindMismatch</b>
205      * functor
206      */

207     public interface Visitor extends net.sf.jga.fn.Visitor {
208         public void visit(FindMismatch host);
209     }
210
211 }
212
Popular Tags