From cdfd6b7a6784246360fe77fe3789696b8b93d877 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Wed, 6 Mar 2013 19:51:26 +0000 Subject: [PATCH] [COLLECTIONS-372] Allow different input/output types for TransformingComparator. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1453516 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 +++ .../commons/collections/ComparatorUtils.java | 9 +++++---- .../comparators/TransformingComparator.java | 20 +++++++++---------- .../TransformingComparatorTest.java | 7 +++---- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index eaf07949b..9a6b099a5 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,9 @@ + + TransformingComparator now supports different types for its input/output values. + Added method "CollectionUtils#isEqualCollection(Collection, Collection, Equator)". diff --git a/src/main/java/org/apache/commons/collections/ComparatorUtils.java b/src/main/java/org/apache/commons/collections/ComparatorUtils.java index 9c9fe1e45..9ecb8d9cf 100644 --- a/src/main/java/org/apache/commons/collections/ComparatorUtils.java +++ b/src/main/java/org/apache/commons/collections/ComparatorUtils.java @@ -202,20 +202,21 @@ public class ComparatorUtils { * by the given transformer before they are compared by the given * comparator. * - * @param the object type to compare + * @param the input object type of the transformed comparator + * @param the object type of the decorated comparator * @param comparator the sort order to use * @param transformer the transformer to use * @return a comparator that transforms its input objects before comparing them * @see TransformingComparator */ @SuppressWarnings("unchecked") - public static Comparator transformedComparator(Comparator comparator, - final Transformer transformer) { + public static Comparator transformedComparator(Comparator comparator, + final Transformer transformer) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } - return new TransformingComparator(transformer, comparator); + return new TransformingComparator(transformer, comparator); } /** diff --git a/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java b/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java index 7bb385f17..1d7e9007a 100644 --- a/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java +++ b/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java @@ -35,15 +35,15 @@ import org.apache.commons.collections.Transformer; * @see org.apache.commons.collections.Transformer * @see org.apache.commons.collections.comparators.ComparableComparator */ -public class TransformingComparator implements Comparator, Serializable { +public class TransformingComparator implements Comparator, Serializable { /** Serialization version from Collections 4.0. */ private static final long serialVersionUID = 3456940356043606220L; /** The decorated comparator. */ - protected final Comparator decorated; + protected final Comparator decorated; /** The transformer being used. */ - protected final Transformer transformer; + protected final Transformer transformer; //----------------------------------------------------------------------- /** @@ -53,7 +53,7 @@ public class TransformingComparator implements Comparator, Serializable { * @param transformer what will transform the arguments to compare */ @SuppressWarnings("unchecked") - public TransformingComparator(final Transformer transformer) { + public TransformingComparator(final Transformer transformer) { this(transformer, ComparatorUtils.NATURAL_COMPARATOR); } @@ -63,8 +63,8 @@ public class TransformingComparator implements Comparator, Serializable { * @param transformer what will transform the arguments to compare * @param decorated the decorated Comparator */ - public TransformingComparator(final Transformer transformer, - final Comparator decorated) { + public TransformingComparator(final Transformer transformer, + final Comparator decorated) { this.decorated = decorated; this.transformer = transformer; } @@ -77,9 +77,9 @@ public class TransformingComparator implements Comparator, Serializable { * @param obj2 the second object to transform then compare * @return negative if obj1 is less, positive if greater, zero if equal */ - public int compare(final E obj1, final E obj2) { - final E value1 = this.transformer.transform(obj1); - final E value2 = this.transformer.transform(obj2); + public int compare(final I obj1, final I obj2) { + final O value1 = this.transformer.transform(obj1); + final O value2 = this.transformer.transform(obj2); return this.decorated.compare(value1, value2); } @@ -119,7 +119,7 @@ public class TransformingComparator implements Comparator, Serializable { return false; } if (object.getClass().equals(this.getClass())) { - final TransformingComparator comp = (TransformingComparator) object; + final TransformingComparator comp = (TransformingComparator) object; return null == decorated ? null == comp.decorated : decorated.equals(comp.decorated) && null == transformer ? null == comp.transformer : transformer.equals(comp.transformer); } diff --git a/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java b/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java index e2a4ddc49..e3b61db07 100644 --- a/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java +++ b/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java @@ -20,6 +20,7 @@ import java.util.Comparator; import java.util.LinkedList; import java.util.List; +import org.apache.commons.collections.ComparatorUtils; import org.apache.commons.collections.TransformerUtils; /** @@ -43,10 +44,8 @@ public class TransformingComparatorTest extends AbstractComparatorTest @Override public Comparator makeObject() { - final Comparator decorated = new ComparableComparator(); - final TransformingComparator comparator = - new TransformingComparator(TransformerUtils.nopTransformer(), decorated); - return comparator; + final Comparator decorated = new ComparableComparator(); + return ComparatorUtils.transformedComparator(decorated, TransformerUtils.stringValueTransformer()); } @Override