commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m..@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/comparators ReverseComparator.java
Date Wed, 20 Mar 2002 00:25:37 GMT
mas         02/03/19 16:25:37

  Modified:    collections/src/java/org/apache/commons/collections/comparators
                        ReverseComparator.java
  Log:
  Fix deviation from Comparator contract
  [sgn(compare(x, y)) == -sgn(compare(y, x))]
  
  Eliminate multiplication by switching order of objects passed to
  underlying comparator.
  
  Revision  Changes    Path
  1.6       +11 -12    jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ReverseComparator.java
  
  Index: ReverseComparator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ReverseComparator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ReverseComparator.java	1 Mar 2002 19:18:49 -0000	1.5
  +++ ReverseComparator.java	20 Mar 2002 00:25:37 -0000	1.6
  @@ -61,20 +61,19 @@
    * Reverses the order of another comparator.
    *
    * @author bayard@generationjava.com
  - * @version $Id: ReverseComparator.java,v 1.5 2002/03/01 19:18:49 morgand Exp $
  + * @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
  + * @version $Id: ReverseComparator.java,v 1.6 2002/03/20 00:25:37 mas Exp $
    */
   public class ReverseComparator implements Comparator,Serializable {
   
       private Comparator comparator;
   
       /**
  -     * Creates a reverse comparator that will invert any List.
  -     * It merely reverses the existing order; it does not
  -     * reorder the List according to reverse "natural" order.
  -     * In many cases, a faster alternative to this is
  -     * the reverse(List) method of java.util.Collection.
  +     * Creates a comparator that compares objects based on the inverse of their
  +     * natural ordering.
        */
       public ReverseComparator() {
  +        this(null);
       }
   
       /**
  @@ -82,15 +81,15 @@
        * of the passed in comparator.
        */
       public ReverseComparator(Comparator comparator) {
  -        this.comparator = comparator;
  +        if(comparator != null) {
  +            this.comparator = comparator;
  +        } else {
  +            this.comparator = ComparableComparator.getInstance();
  +        }
       }
   
       public int compare(Object o1, Object o2) {
  -        if(comparator == null) {
  -            return -1;
  -        } else {
  -            return -1*comparator.compare(o1,o2);
  -        }
  +        return comparator.compare(o2, o1);
       }
   
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message