[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:
parent
0ba8b8194e
commit
cdfd6b7a67
|
@ -22,6 +22,9 @@
|
|||
<body>
|
||||
|
||||
<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">
|
||||
Added method "CollectionUtils#isEqualCollection(Collection, Collection, Equator)".
|
||||
</action>
|
||||
|
|
|
@ -202,20 +202,21 @@ public class ComparatorUtils {
|
|||
* by the given transformer before they are compared by the given
|
||||
* 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 transformer the transformer to use
|
||||
* @return a comparator that transforms its input objects before comparing them
|
||||
* @see TransformingComparator
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E> Comparator<E> transformedComparator(Comparator<E> comparator,
|
||||
final Transformer<? super E, ? extends E> transformer) {
|
||||
public static <I, O> Comparator<I> transformedComparator(Comparator<O> comparator,
|
||||
final Transformer<? super I, ? extends O> transformer) {
|
||||
|
||||
if (comparator == null) {
|
||||
comparator = NATURAL_COMPARATOR;
|
||||
}
|
||||
return new TransformingComparator<E>(transformer, comparator);
|
||||
return new TransformingComparator<I, O>(transformer, comparator);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<E> implements Comparator<E>, Serializable {
|
||||
public class TransformingComparator<I, O> implements Comparator<I>, Serializable {
|
||||
|
||||
/** Serialization version from Collections 4.0. */
|
||||
private static final long serialVersionUID = 3456940356043606220L;
|
||||
|
||||
/** The decorated comparator. */
|
||||
protected final Comparator<E> decorated;
|
||||
protected final Comparator<O> decorated;
|
||||
/** 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>
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
|
||||
|
@ -63,8 +63,8 @@ public class TransformingComparator<E> implements Comparator<E>, Serializable {
|
|||
* @param transformer what will transform the arguments to <code>compare</code>
|
||||
* @param decorated the decorated Comparator
|
||||
*/
|
||||
public TransformingComparator(final Transformer<? super E, ? extends E> transformer,
|
||||
final Comparator<E> decorated) {
|
||||
public TransformingComparator(final Transformer<? super I, ? extends O> transformer,
|
||||
final Comparator<O> decorated) {
|
||||
this.decorated = decorated;
|
||||
this.transformer = transformer;
|
||||
}
|
||||
|
@ -77,9 +77,9 @@ public class TransformingComparator<E> implements Comparator<E>, 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<E> implements Comparator<E>, 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);
|
||||
}
|
||||
|
|
|
@ -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<Integer>
|
|||
|
||||
@Override
|
||||
public Comparator<Integer> makeObject() {
|
||||
final Comparator<Integer> decorated = new ComparableComparator<Integer>();
|
||||
final TransformingComparator<Integer> comparator =
|
||||
new TransformingComparator<Integer>(TransformerUtils.<Integer>nopTransformer(), decorated);
|
||||
return comparator;
|
||||
final Comparator<String> decorated = new ComparableComparator<String>();
|
||||
return ComparatorUtils.transformedComparator(decorated, TransformerUtils.<Integer>stringValueTransformer());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue