commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1453516 - in /commons/proper/collections/trunk/src: changes/ main/java/org/apache/commons/collections/ main/java/org/apache/commons/collections/comparators/ test/java/org/apache/commons/collections/comparators/
Date Wed, 06 Mar 2013 19:51:27 GMT
Author: tn
Date: Wed Mar  6 19:51:26 2013
New Revision: 1453516

URL: http://svn.apache.org/r1453516
Log:
[COLLECTIONS-372] Allow different input/output types for TransformingComparator.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ComparatorUtils.java
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1453516&r1=1453515&r2=1453516&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Wed Mar  6 19:51:26 2013
@@ -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>

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ComparatorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ComparatorUtils.java?rev=1453516&r1=1453515&r2=1453516&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ComparatorUtils.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/ComparatorUtils.java
Wed Mar  6 19:51:26 2013
@@ -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);
     }
 
     /**

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java?rev=1453516&r1=1453515&r2=1453516&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java
(original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections/comparators/TransformingComparator.java
Wed Mar  6 19:51:26 2013
@@ -35,15 +35,15 @@ import org.apache.commons.collections.Tr
  * @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> i
      * @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> i
      * @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> i
      * @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> i
             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);
         }

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java?rev=1453516&r1=1453515&r2=1453516&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java
(original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections/comparators/TransformingComparatorTest.java
Wed Mar  6 19:51:26 2013
@@ -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 
 
     @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



Mime
View raw message