net.sf.collections15.comparators
Class TransformingComparator<I,O>

java.lang.Object
  extended by net.sf.collections15.comparators.TransformingComparator<I,O>
All Implemented Interfaces:
java.util.Comparator<I>

public class TransformingComparator<I,O>
extends java.lang.Object
implements java.util.Comparator<I>

Decorates another Comparator with transformation behavior. That is, the input values are transformed, and the results are passed to the underlying Comparator's. compare method.

Since:
Collections15 1.0
Author:
Chris Lambrou (port to Java 5.0)
See Also:
Transformer, ComparableComparator

Field Summary
protected  java.util.Comparator<O> decorated
          The decorated Comparator.
protected  Transformer<I,O> transformer
          The Transformer being used.
 
Constructor Summary
protected TransformingComparator(Transformer<I,O> transformer, java.util.Comparator<O> decorated)
          Constructs an instance with the given Transformer and Comparator.
 
Method Summary
 int compare(I obj1, I obj2)
          Returns the result of comparing the values from the transform operation.
static
<I,O> TransformingComparator<I,O>
decorate(Transformer<I,O> transformer, java.util.Comparator<O> decorated)
          Returns an instance whose ordering is determined by applying the odereing of the specified Comparator to the output of the specified Transformer.
static
<I,O extends java.lang.Comparable>
TransformingComparator<I,O>
getInstance(Transformer<I,O> transformer)
          Returns an instance whose ordering is based on the natural ordering of the output of the specified Transformer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Field Detail

decorated

protected java.util.Comparator<O> decorated
The decorated Comparator.


transformer

protected Transformer<I,O> transformer
The Transformer being used.

Constructor Detail

TransformingComparator

protected TransformingComparator(Transformer<I,O> transformer,
                                 java.util.Comparator<O> decorated)
Constructs an instance with the given Transformer and Comparator.

Parameters:
transformer - The Transformer used to transform the compared objects.
decorated - The decorated Comparator that is applied to the transformed comparison objects.
Throws:
java.lang.IllegalArgumentException - If either argument is null.
Method Detail

getInstance

public static <I,O extends java.lang.Comparable> TransformingComparator<I,O> getInstance(Transformer<I,O> transformer)
Returns an instance whose ordering is based on the natural ordering of the output of the specified Transformer.

Parameters:
transformer - The Transformer used to transform the compared objects prior to applying the natural sorting order of the resulting transformed output.

decorate

public static <I,O> TransformingComparator<I,O> decorate(Transformer<I,O> transformer,
                                                         java.util.Comparator<O> decorated)
Returns an instance whose ordering is determined by applying the odereing of the specified Comparator to the output of the specified Transformer.

Parameters:
transformer - The Transformer used to transform the compared objects.
decorated - The decorated Comparator that is applied to the transformed comparison objects.

compare

public int compare(I obj1,
                   I obj2)
Returns the result of comparing the values from the transform operation.

Specified by:
compare in interface java.util.Comparator<I>
Parameters:
obj1 - The first object to transform then compare.
obj2 - The second object to transform then compare.
Returns:
A negative value if obj1 is less than obj2. A positive value if obj1 is greater than obj2. Zero if the two objects are equivalent with respect to the ordering.


Copyright © 2001-2005 SourceForge.net. All Rights Reserved.