001: // ============================================================================
002: // $Id: FindAdjacent.java,v 1.19 2006/12/05 04:52:38 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032: package net.sf.jga.fn.algorithm;
033:
034: import net.sf.jga.fn.BinaryFunctor;
035: import net.sf.jga.fn.UnaryFunctor;
036: import net.sf.jga.fn.comparison.EqualTo;
037: import net.sf.jga.fn.comparison.Equality;
038: import net.sf.jga.util.LookAheadIterator;
039: import java.util.Iterator;
040:
041: /**
042: * Locates pairs of adjacent values in an iteration.
043: * <p>
044: * Copyright © 2003-2005 David A. Hall
045: *
046: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
047: * @deprecated
048: */
049:
050: public class FindAdjacent<T> extends LookAheadFunctor<T> {
051:
052: static final long serialVersionUID = 6357244961374202731L;
053:
054: // The functor used to determine if two adjacent values are the same
055: private BinaryFunctor<T, T, Boolean> _eq;
056:
057: /**
058: * Builds a FindAdjacent functor that uses the equals() method to determine
059: * if adjacent values are the same.
060: */
061: public FindAdjacent() {
062: this (new EqualTo<T>());
063: }
064:
065: /**
066: * Builds a FindAdjacent functor that uses the given predicate to determine
067: * if adjacent values are the same. The functor argument is expected to
068: * compare two values and return TRUE if they are considered to be equal..
069: */
070: public FindAdjacent(BinaryFunctor<T, T, Boolean> eq) {
071: _eq = eq;
072: }
073:
074: /**
075: * Returns the functor used to determine if two adjacent values are the same
076: */
077: public BinaryFunctor<T, T, Boolean> getComparisonFn() {
078: return _eq;
079: }
080:
081: /**
082: * Locates the first/next pair of adjacent elements in an iteration that
083: * are the same value.
084: * @return an iterator whose next() [if it hasNext()] points to the first of
085: * a pair of adjacent equivalent values. If no such pair exists, then the
086: * iterator's hasNext() will be false.
087: */
088: public LookAheadIterator<T> fn(Iterator<? extends T> iterator) {
089: // return early If the input iterator is finished,
090: if (!iterator.hasNext()) {
091: return new LookAheadIterator<T>(iterator, 1);
092: }
093:
094: LookAheadIterator<T> lai = wrap(iterator, 2);
095: while (lai.hasNextPlus(2)) {
096: T arg1 = lai.peek(1);
097: T arg2 = lai.peek(2);
098: if (_eq.fn(arg1, arg2)) {
099: return lai;
100: }
101:
102: lai.next();
103: }
104:
105: // didn't find anything, so we advance our working iterator off the end
106: // and return it.
107: lai.next();
108: return lai;
109: }
110:
111: /**
112: * Calls the Visitor's <code>visit(FindAdjacent)</code> method, if it
113: * implements the nested Visitor interface.
114: */
115: public void accept(net.sf.jga.fn.Visitor v) {
116: if (v instanceof FindAdjacent.Visitor)
117: ((FindAdjacent.Visitor) v).visit(this );
118: else
119: v.visit(this );
120: }
121:
122: // Object overrides
123:
124: public String toString() {
125: return "FindAdjacent[" + _eq + "]";
126: }
127:
128: // AcyclicVisitor
129:
130: /**
131: * Interface for classes that may interpret an <b>FindAdjacent</b> functor.
132: */
133: public interface Visitor extends net.sf.jga.fn.Visitor {
134: public void visit(FindAdjacent host);
135: }
136: }
|