[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
This commit is contained in:
Thomas Neidhart 2013-03-06 19:51:26 +00:00
parent 0ba8b8194e
commit cdfd6b7a67
4 changed files with 21 additions and 18 deletions

View File

@ -22,6 +22,9 @@
<body> <body>
<release version="4.0" date="TBA" description="Next release"> <release version="4.0" date="TBA" description="Next release">
<action issue="COLLECTIONS-372" dev="tn" type="change">
TransformingComparator now supports different types for its input/output values.
</action>
<action issue="COLLECTIONS-446" dev="tn" type="add" due-to="Matt Lachman"> <action issue="COLLECTIONS-446" dev="tn" type="add" due-to="Matt Lachman">
Added method "CollectionUtils#isEqualCollection(Collection, Collection, Equator)". Added method "CollectionUtils#isEqualCollection(Collection, Collection, Equator)".
</action> </action>

View File

@ -202,20 +202,21 @@ public class ComparatorUtils {
* by the given transformer before they are compared by the given * by the given transformer before they are compared by the given
* comparator. * comparator.
* *
* @param <E> the object type to compare * @param <I> the input object type of the transformed comparator
* @param <O> the object type of the decorated comparator
* @param comparator the sort order to use * @param comparator the sort order to use
* @param transformer the transformer to use * @param transformer the transformer to use
* @return a comparator that transforms its input objects before comparing them * @return a comparator that transforms its input objects before comparing them
* @see TransformingComparator * @see TransformingComparator
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <E> Comparator<E> transformedComparator(Comparator<E> comparator, public static <I, O> Comparator<I> transformedComparator(Comparator<O> comparator,
final Transformer<? super E, ? extends E> transformer) { final Transformer<? super I, ? extends O> transformer) {
if (comparator == null) { if (comparator == null) {
comparator = NATURAL_COMPARATOR; comparator = NATURAL_COMPARATOR;
} }
return new TransformingComparator<E>(transformer, comparator); return new TransformingComparator<I, O>(transformer, comparator);
} }
/** /**

View File

@ -35,15 +35,15 @@ import org.apache.commons.collections.Transformer;
* @see org.apache.commons.collections.Transformer * @see org.apache.commons.collections.Transformer
* @see org.apache.commons.collections.comparators.ComparableComparator * @see org.apache.commons.collections.comparators.ComparableComparator
*/ */
public class TransformingComparator<E> implements Comparator<E>, Serializable { public class TransformingComparator<I, O> implements Comparator<I>, Serializable {
/** Serialization version from Collections 4.0. */ /** Serialization version from Collections 4.0. */
private static final long serialVersionUID = 3456940356043606220L; private static final long serialVersionUID = 3456940356043606220L;
/** The decorated comparator. */ /** The decorated comparator. */
protected final Comparator<E> decorated; protected final Comparator<O> decorated;
/** The transformer being used. */ /** The transformer being used. */
protected final Transformer<? super E, ? extends E> transformer; protected final Transformer<? super I, ? extends O> transformer;
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
@ -53,7 +53,7 @@ public class TransformingComparator<E> implements Comparator<E>, Serializable {
* @param transformer what will transform the arguments to <code>compare</code> * @param transformer what will transform the arguments to <code>compare</code>
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public TransformingComparator(final Transformer<? super E, ? extends E> transformer) { public TransformingComparator(final Transformer<? super I, ? extends O> transformer) {
this(transformer, ComparatorUtils.NATURAL_COMPARATOR); this(transformer, ComparatorUtils.NATURAL_COMPARATOR);
} }
@ -63,8 +63,8 @@ public class TransformingComparator<E> implements Comparator<E>, Serializable {
* @param transformer what will transform the arguments to <code>compare</code> * @param transformer what will transform the arguments to <code>compare</code>
* @param decorated the decorated Comparator * @param decorated the decorated Comparator
*/ */
public TransformingComparator(final Transformer<? super E, ? extends E> transformer, public TransformingComparator(final Transformer<? super I, ? extends O> transformer,
final Comparator<E> decorated) { final Comparator<O> decorated) {
this.decorated = decorated; this.decorated = decorated;
this.transformer = transformer; this.transformer = transformer;
} }
@ -77,9 +77,9 @@ public class TransformingComparator<E> implements Comparator<E>, Serializable {
* @param obj2 the second object to transform then compare * @param obj2 the second object to transform then compare
* @return negative if obj1 is less, positive if greater, zero if equal * @return negative if obj1 is less, positive if greater, zero if equal
*/ */
public int compare(final E obj1, final E obj2) { public int compare(final I obj1, final I obj2) {
final E value1 = this.transformer.transform(obj1); final O value1 = this.transformer.transform(obj1);
final E value2 = this.transformer.transform(obj2); final O value2 = this.transformer.transform(obj2);
return this.decorated.compare(value1, value2); return this.decorated.compare(value1, value2);
} }
@ -119,7 +119,7 @@ public class TransformingComparator<E> implements Comparator<E>, Serializable {
return false; return false;
} }
if (object.getClass().equals(this.getClass())) { 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) && return null == decorated ? null == comp.decorated : decorated.equals(comp.decorated) &&
null == transformer ? null == comp.transformer : transformer.equals(comp.transformer); null == transformer ? null == comp.transformer : transformer.equals(comp.transformer);
} }

View File

@ -20,6 +20,7 @@ import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.TransformerUtils; import org.apache.commons.collections.TransformerUtils;
/** /**
@ -43,10 +44,8 @@ public class TransformingComparatorTest extends AbstractComparatorTest<Integer>
@Override @Override
public Comparator<Integer> makeObject() { public Comparator<Integer> makeObject() {
final Comparator<Integer> decorated = new ComparableComparator<Integer>(); final Comparator<String> decorated = new ComparableComparator<String>();
final TransformingComparator<Integer> comparator = return ComparatorUtils.transformedComparator(decorated, TransformerUtils.<Integer>stringValueTransformer());
new TransformingComparator<Integer>(TransformerUtils.<Integer>nopTransformer(), decorated);
return comparator;
} }
@Override @Override