net.sf.jga.fn.comparison
Class ComparatorFn<T>

java.lang.Object
  extended by net.sf.jga.fn.Functor<R>
      extended by net.sf.jga.fn.BinaryFunctor<T,T,java.lang.Integer>
          extended by net.sf.jga.fn.comparison.ComparatorFn<T>
All Implemented Interfaces:
java.io.Serializable, java.util.Comparator<T>, Visitable

public class ComparatorFn<T>
extends BinaryFunctor<T,T,java.lang.Integer>
implements java.util.Comparator<T>

Functor wrapper around Comparator object. Allows Comparators to be used anywhere a Functor returning an Integer could have been used. Also implements Comparator as well, so an instance of this class could be used anywhere that the constructor argument could be used.

To Serialize a ComparatorFn, the Comparator given at construction must be Serializable.

Copyright © 2002-2005 David A. Hall

Author:
David A. Hall
See Also:
Serialized Form

Nested Class Summary
static interface ComparatorFn.Visitor
          Interface for classes that may interpret a ComparatorFn functor.
 
Constructor Summary
ComparatorFn(java.util.Comparator<T> comp)
          Builds the ComparatorFn wrapped around the given Comparator.
 
Method Summary
 void accept(Visitor v)
          Calls the Visitor's visit(ComperatorFn) method, if it implements the nested Visitor interface.
 int compare(T x, T y)
           
 java.lang.Integer fn(T x, T y)
          Given arguments x and y, return the result of the Comparator's compare(x,y) method, wrapped in an Integer.
 java.util.Comparator<T> getComparator()
          Returns the comparator in use by this functor
 java.lang.String toString()
           
 
Methods inherited from class net.sf.jga.fn.BinaryFunctor
bind, bind0th, bind1st, bind2nd, bindNth, compose, compose, composeNth, distribute, eval, generate, generate1st, generate2nd, generateNth
 
Methods inherited from class net.sf.jga.fn.Functor
composeNth
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

ComparatorFn

public ComparatorFn(java.util.Comparator<T> comp)
Builds the ComparatorFn wrapped around the given Comparator.

Throws:
java.lang.NullPointerException - if no Comparator is passed.
Method Detail

getComparator

public java.util.Comparator<T> getComparator()
Returns the comparator in use by this functor


fn

public java.lang.Integer fn(T x,
                            T y)
Given arguments x and y, return the result of the Comparator's compare(x,y) method, wrapped in an Integer. Whether or not a NullPointerException is thrown if either x or y are null is up to the Comparator

Specified by:
fn in class BinaryFunctor<T,T,java.lang.Integer>
Returns:
the result of the Comparator's compare(x,y) method

accept

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

Specified by:
accept in interface Visitable
Overrides:
accept in class Functor<java.lang.Integer>

compare

public int compare(T x,
                   T y)
Specified by:
compare in interface java.util.Comparator<T>

toString

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


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