net.sf.jga.fn.algorithm
Class FindMismatch<T>

java.lang.Object
  extended by net.sf.jga.fn.UnaryFunctor<java.util.Iterator<? extends T>,LookAheadIterator<T>>
      extended by net.sf.jga.fn.algorithm.LookAheadFunctor<T>
          extended by net.sf.jga.fn.algorithm.FindMismatch<T>
All Implemented Interfaces:
java.io.Serializable, Visitable

public class FindMismatch<T>
extends LookAheadFunctor<T>

Locates the next element in an iteration that is not equal to the corresponding element in a given collection.

To Serialize a FindMismatch, the generic parameter T must be serializable.

Copyright © 2003 David A. Hall

Author:
David A. Hall
See Also:
Serialized Form

Nested Class Summary
static interface FindMismatch.Visitor
          Interface for classes that may interpret a FindMismatch functor
 
Constructor Summary
FindMismatch(java.util.Collection<? extends T> elements)
          Builds a FindMismatch functor that uses !equals() to compare elements.
FindMismatch(java.util.Collection<? extends T> elements, BinaryFunctor<T,T,java.lang.Boolean> neq)
          Builds a FindMismatch functor that uses the given functor to compare elements.
FindMismatch(java.util.Iterator<? extends T> iter, BinaryFunctor<T,T,java.lang.Boolean> neq)
          Builds one-time use FindMismatch finder that will test against the contents of an iteration rather than a collection.
 
Method Summary
 void accept(Visitor v)
          Calls the Visitor's visit(FindMismatch) method, if it implements the nested Visitor interface.
 LookAheadIterator<T> fn(java.util.Iterator<? extends T> iterator)
          Locates the first/next element in an iteration that is not the same as the corresponding element in the given collection/iteration.
 BinaryFunctor<T,T,java.lang.Boolean> getComparisonFn()
          Returns the functor that is used to test matching elements
 java.util.Collection<? extends T> getElements()
          Returns the collection against which the argument will be compared.
 T getMismatchedElement()
          Returns the mismatched element in the given collection/iteration on the last call to fn().
 java.lang.String toString()
           
 
Methods inherited from class net.sf.jga.fn.algorithm.LookAheadFunctor
wrap
 
Methods inherited from class net.sf.jga.fn.UnaryFunctor
bind, compose, compose, generate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FindMismatch

public FindMismatch(java.util.Collection<? extends T> elements)
Builds a FindMismatch functor that uses !equals() to compare elements. If the collection is null, an empty collection will be used for comparison.


FindMismatch

public FindMismatch(java.util.Collection<? extends T> elements,
                    BinaryFunctor<T,T,java.lang.Boolean> neq)
Builds a FindMismatch functor that uses the given functor to compare elements. The functor is expected to compare two arguments and return TRUE if they are not equal. If the collection is null, an empty collection will be used for comparison.


FindMismatch

public FindMismatch(java.util.Iterator<? extends T> iter,
                    BinaryFunctor<T,T,java.lang.Boolean> neq)
Builds one-time use FindMismatch finder that will test against the contents of an iteration rather than a collection. The functor is expected to compare two arguments and return TRUE if they are not equal.

A FindMismatch built this way cannot be used more than once as the contents of the iteration will be consumed in the first execution.

Method Detail

getElements

public java.util.Collection<? extends T> getElements()
Returns the collection against which the argument will be compared. When called on a FindMismatch built with an iteration rather than a collection, this method will return null


getComparisonFn

public BinaryFunctor<T,T,java.lang.Boolean> getComparisonFn()
Returns the functor that is used to test matching elements


getMismatchedElement

public T getMismatchedElement()
Returns the mismatched element in the given collection/iteration on the last call to fn(). If the iterator passed to the last call to fn() had no mismatch for the length of the collection, then this method returns null.

Throws:
java.lang.IllegalStateException - if called before a call to fn.

fn

public LookAheadIterator<T> fn(java.util.Iterator<? extends T> iterator)
Locates the first/next element in an iteration that is not the same as the corresponding element in the given collection/iteration.

Specified by:
fn in class UnaryFunctor<java.util.Iterator<? extends T>,LookAheadIterator<T>>
Returns:
an iterator whose next() [if it hasNext()] points to the next element in the iteration that does not match the corresponding element in the given collection. If no such element exists and the iteration is longer than the the given collection, then the returned iterator's next() points to the first element that does not exist in the given collection. If no mismatched element exists and the collection is at least as long as the iteration, then the returned iterator's hasNext() will be false.

accept

public void accept(Visitor v)
Calls the Visitor's visit(FindMismatch) method, if it implements the nested Visitor interface.

Specified by:
accept in interface Visitable
Overrides:
accept in class UnaryFunctor<java.util.Iterator<? extends T>,LookAheadIterator<T>>

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2002-2005 David A. Hall. All Rights Reserved.