You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/01/07 19:40:47 UTC

cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/comparators ComparableComparator.java

rwaldhoff    2003/01/07 10:40:45

  Modified:    collections/src/java/org/apache/commons/collections/comparators
                        ComparableComparator.java
  Log:
  support equals as per the Comparator contract
  support hashCode for this equals as per the Object contract
  
  Revision  Changes    Path
  1.6       +31 -4     jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparableComparator.java
  
  Index: ComparableComparator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/ComparableComparator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ComparableComparator.java	12 Jun 2002 03:59:17 -0000	1.5
  +++ ComparableComparator.java	7 Jan 2003 18:40:45 -0000	1.6
  @@ -3,7 +3,7 @@
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -55,13 +55,13 @@
    */
   
   import java.io.Serializable;
  -import java.lang.Comparable;
   import java.util.Comparator;
   
   /**
    * A Comparator that compares Comparable objects.
    * Throws ClassCastExceptions if the objects are not 
  - * Comparable, or if they are null.
  + * Comparable, or if either is null.
  + * 
    * Throws ClassCastException if the compareTo of both 
    * objects do not provide an inverse result of each other 
    * as per the Comparable javadoc.  This Comparator is useful, for example,
  @@ -70,7 +70,7 @@
    *
    * @since 2.0
    * @author bayard@generationjava.com
  - * @version $Id$
  + * @version $Revision$ $Date$
    */
   public class ComparableComparator implements Comparator,Serializable {
   
  @@ -141,4 +141,31 @@
           }
       }
   
  +    /**
  +     * Implement a hash code for this comparator that is consistent with
  +     * {@link #equals}.
  +     *
  +     * @return a hash code for this comparator.
  +     * @since Collections 2.2
  +     */
  +    public int hashCode() {
  +        return "ComparableComparator".hashCode();
  +    }
  +
  +    /**
  +     * Returns <code>true</code> iff <i>that</i> Object is 
  +     * is a {@link Comparator} whose ordering is known to be 
  +     * equivalent to mine.
  +     * <p>
  +     * This implementation returns <code>true</code>
  +     * iff <code><i>that</i>.{@link Object#getClass getClass()}</code>
  +     * equals <code>this.getClass()</code>.  Subclasses may want to override
  +     * this behavior to remain consistent with the {@link Comparator.equals}
  +     * contract.
  +     * @since Collections 2.2
  +     */
  +    public boolean equals(Object obj) {
  +        return (this == obj) || 
  +               ((null != obj) && (obj.getClass().equals(this.getClass())));
  +    }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/comparat ors ComparableComparator.java

Posted by Rich Dougherty <ri...@rd.gen.nz>.
>   +    /**
>   +     * Implement a hash code for this comparator that is consistent with
>   +     * {@link #equals}.
>   +     *
>   +     * @return a hash code for this comparator.
>   +     * @since Collections 2.2
>   +     */
>   +    public int hashCode() {
>   +        return "ComparableComparator".hashCode();
>   +    }

You could make that a little bit clearer by making it a constant. ie

  private static final HASH_CODE = "ComparableComparator".hashCode();

And it'll improve performance. ;-)

Rich