You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ma...@apache.org on 2002/06/09 21:23:53 UTC

cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/comparators TestNullComparator.java TestComparator.java

mas         2002/06/09 12:23:53

  Modified:    collections/src/test/org/apache/commons/collections
                        TestAll.java
               collections/src/test/org/apache/commons/collections/comparators
                        TestComparator.java
  Added:       collections/data/test NullComparator.version2.obj1
                        NullComparator.version2.obj2
               collections/src/java/org/apache/commons/collections/comparators
                        NullComparator.java
               collections/src/test/org/apache/commons/collections/comparators
                        TestNullComparator.java
  Log:
  Added a comparator that provides explicit control over comparisons with null.
  This can be used to create SortedSets or other sorted collections where nulls
  are sorted first or last.  The basis for this comparator was this discussion on
  the commons mailing list:
  
  http://www.mail-archive.com/commons-dev%40jakarta.apache.org/msg07762.html
  
  Revision  Changes    Path
  1.1                  jakarta-commons/collections/data/test/NullComparator.version2.obj1
  
  	<<Binary file>>
  
  
  1.1                  jakarta-commons/collections/data/test/NullComparator.version2.obj2
  
  	<<Binary file>>
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/comparators/NullComparator.java
  
  Index: NullComparator.java
  ===================================================================
  package org.apache.commons.collections.comparators;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Commons" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.Serializable;
  import java.lang.Comparable;
  import java.util.Comparator;
  
  /**
   *  A Comparator that will compare nulls to be either lower or higher than
   *  other objects.
   *
   *  @author <a href="mas@apache.org">Michael A. Smith</a>
   *  @version $Id: NullComparator.java,v 1.1 2002/06/09 19:23:53 mas Exp $ 
   **/
  public class NullComparator implements Comparator, Serializable {
  
      /**
       *  The comparator to use when comparing two non-<code>null</code> objects.
       **/
      private Comparator nonNullComparator;
  
      /**
       *  Specifies whether a <code>null</code> are compared as higher than
       *  non-<code>null</code> objects.
       **/
      private boolean nullsAreHigh;
  
      /** 
       *  Construct an instance that sorts <code>null</code> higher than any
       *  non-<code>null</code> object it is compared with. When comparing two
       *  non-<code>null</code> objects, the {@link ComparableComparator} is
       *  used.
       **/
      public NullComparator() {
  	this(ComparableComparator.getInstance(), true);
      }
  
      /**
       *  Construct an instance that sorts <code>null</code> higher than any
       *  non-<code>null</code> object it is compared with.  When comparing two
       *  non-<code>null</code> objects, the specified {@link Comparator} is
       *  used.
       *
       *  @param nonNullComparator the comparator to use when comparing two
       *  non-<code>null</code> objects.  This argument cannot be
       *  <code>null</code>
       *
       *  @exception NullPointerException if <code>nonNullComparator</code> is
       *  <code>null</code>
       **/
      public NullComparator(Comparator nonNullComparator) {
  	this(nonNullComparator, true);
      }
  
      /**
       *  Construct an instance that sorts <code>null</code> higher or lower than
       *  any non-<code>null</code> object it is compared with.  When comparing
       *  two non-<code>null</code> objects, the {@link ComparableCOmparator} is
       *  used.
       *
       *  @param nullsAreHigh a <code>true</code> value indicates that
       *  <code>null</code> should be compared as higher than a
       *  non-<code>null</code> object.  A <code>false</code> value indicates
       *  that <code>null</code> should be compared as lower than a
       *  non-<code>null</code> object.
       **/
      public NullComparator(boolean nullsAreHigh) {
  	this(ComparableComparator.getInstance(), nullsAreHigh);
      }
      
      /**
       *  Cosntruct an instance that sorts <code>null</code> higher or lower than
       *  any non-<code>null</code> object it is compared with.  When comparing
       *  two non-<code>null</code> objects, the specified {@link Comparator} is
       *  used.
       *
       *  @param nonNullComparator the comparator to use when comparing two
       *  non-<code>null</code> objects. This argument cannot be
       *  <code>null</code>
       *
       *  @param nullsAreHigh a <code>true</code> value indicates that
       *  <code>null</code> should be compared as higher than a
       *  non-<code>null</code> object.  A <code>false</code> value indicates
       *  that <code>null</code> should be compared as lower than a
       *  non-<code>null</code> object.
       *
       *  @exception NullPointerException if <code>nonNullComparator</code> is
       *  <code>null</code>
       **/
      public NullComparator(Comparator nonNullComparator, boolean nullsAreHigh) {
  	this.nonNullComparator = nonNullComparator;
  	this.nullsAreHigh = nullsAreHigh;
  
  	if(nonNullComparator == null) {
  	    throw new NullPointerException("null nonNullComparator");
  	}
      }
  
      /**
       *  Perform a comparison between two objects.  If both objects are
       *  <code>null</code>, a <code>0</code> value is returned.  If one object
       *  is <code>null</code> and the other is not, the result is determined on
       *  whether the Comparator was constructed to have nulls as higher or lower
       *  than other objects.  If neither object is <code>null</code>, an
       *  underlying comparator specified in the constructor (or the default) is
       *  used to compare the non-<code>null</code> objects.
       *
       *  @param o1 the first object to compare
       *
       *  @param o2 the object to compare it to.
       *
       *  @return <code>-1</code> if <code>o1</code> is "lower" than (less than,
       *  before, etc.) <code>o2</code>; <code>1</code> if <code>o1</code> is
       *  "higher" than (greater than, after, etc.) <code>o2</code>; or
       *  <code>0</code> if <code>o1</code> and <code>o2</code> are equal.
       **/
      public int compare(Object o1, Object o2) {
  	if(o1 == o2) return 0;
  	if(o1 == null) return (this.nullsAreHigh ? 1 : -1);
  	if(o2 == null) return (this.nullsAreHigh ? -1 : 1);
  	return this.nonNullComparator.compare(o1, o2);
      }
  
      /**
       *  Implement a hash code for this comparator that is consistent with
       *  {@link #equals(Object)}.
       *
       *  @return a hash code for this comparator.
       **/
      public int hashCode() {
  	return (nullsAreHigh ? -1 : 1) * nonNullComparator.hashCode();
      }
  
      /**
       *  Determines whether the specified object represents a comparator that is
       *  equal to this comparator.
       *
       *  @param o the object to compare this comparator with.
       *
       *  @return <code>true</code> if the specified object is a NullComparator
       *  with equivalant <code>null</code> comparison behavior
       *  (i.e. <code>null</code> high or low) and with equivalent underlying
       *  non-<code>null</code> object comparators.
       **/
      public boolean equals(Object obj) {
  	if(obj == null) return false;
  	if(obj == this) return true;
  	if(!obj.getClass().equals(this.getClass())) return false;
  
  	NullComparator other = (NullComparator)obj;
  	
  	return ((this.nullsAreHigh == other.nullsAreHigh) &&
  		(this.nonNullComparator.equals(other.nonNullComparator)));
      }
  
      private static final long serialVersionUID = -5820772575483504339L;
  }
  
  
  
  1.27      +5 -4      jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- TestAll.java	4 Jun 2002 16:01:27 -0000	1.26
  +++ TestAll.java	9 Jun 2002 19:23:53 -0000	1.27
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v 1.26 2002/06/04 16:01:27 rwaldhoff Exp $
  - * $Revision: 1.26 $
  - * $Date: 2002/06/04 16:01:27 $
  + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v 1.27 2002/06/09 19:23:53 mas Exp $
  + * $Revision: 1.27 $
  + * $Date: 2002/06/09 19:23:53 $
    *
    * ====================================================================
    *
  @@ -67,7 +67,7 @@
   /**
    * Entry point for all Collections tests.
    * @author Rodney Waldhoff
  - * @version $Id: TestAll.java,v 1.26 2002/06/04 16:01:27 rwaldhoff Exp $
  + * @version $Id: TestAll.java,v 1.27 2002/06/09 19:23:53 mas Exp $
    */
   public class TestAll extends TestCase {
       public TestAll(String testName) {
  @@ -101,6 +101,7 @@
           suite.addTest(TestLRUMap.suite());
           suite.addTest(TestMultiHashMap.suite());
           suite.addTest(TestReverseComparator.suite());
  +	suite.addTest(TestNullComparator.suite());
           suite.addTest(TestSequencedHashMap.suite());
           suite.addTest(TestSingletonIterator.suite());
           suite.addTest(TestTreeBag.suite());
  
  
  
  1.4       +21 -9     jakarta-commons/collections/src/test/org/apache/commons/collections/comparators/TestComparator.java
  
  Index: TestComparator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/comparators/TestComparator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestComparator.java	1 Mar 2002 23:30:58 -0000	1.3
  +++ TestComparator.java	9 Jun 2002 19:23:53 -0000	1.4
  @@ -1,6 +1,7 @@
   package org.apache.commons.collections.comparators;
   
   import java.io.IOException;
  +import java.io.FileNotFoundException;
   import java.io.Serializable;
   import java.util.Collections;
   import java.util.Comparator;
  @@ -133,17 +134,28 @@
        */
       public void testComparatorCompatibility() throws IOException, ClassNotFoundException {
           Comparator comparator = null;
  -        /*
  -         * Create canonical objects with this code
  -        comparator = makeComparator();
  -        
  -        writeExternalFormToDisk((Serializable) comparator, 
  -                                getCanonicalComparatorName(comparator));
  -        */
   
           // test to make sure the canonical form has been preserved
  -        comparator = 
  -            (Comparator) readExternalFormFromDisk(getCanonicalComparatorName(makeComparator()));
  +	try {
  +	    comparator = 
  +		(Comparator) readExternalFormFromDisk
  +		(getCanonicalComparatorName(makeComparator()));
  +	} catch (FileNotFoundException exception) {
  +
  +	    boolean autoCreateSerialized = false;
  +
  +	    if(autoCreateSerialized) {
  +		comparator = makeComparator();
  +		String fileName = getCanonicalComparatorName(comparator);
  +		writeExternalFormToDisk((Serializable) comparator, fileName);
  +		fail("Serialized form could not be found.  A serialized version " +
  +		     "has now been written (and should be added to CVS): " + fileName);
  +	    } else {
  +		fail("The Serialized form could be located to test serialization " +
  +		     "compatibility: " + exception.getMessage());
  +	    }
  +	}
  +
           
           // make sure the canonical form produces the ordering we currently
           // expect
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/comparators/TestNullComparator.java
  
  Index: TestNullComparator.java
  ===================================================================
  package org.apache.commons.collections.comparators;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Commons" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.Comparator;
  import java.util.LinkedList;
  import java.util.List;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   *  Test the NullComparator 
   *
   *  @author <a href="mas@apache.org">Michael A. Smith</a>
   *  @version $Id: TestNullComparator.java,v 1.1 2002/06/09 19:23:53 mas Exp $
   **/
  public abstract class TestNullComparator extends TestComparator {
  
      public TestNullComparator(String testName) {
          super(testName);
      }
  
      public static Test suite() {
  	TestSuite suite = new TestSuite(TestNullComparator.class.getName());
  	suite.addTest(new TestSuite(TestNullComparator1.class));
  	suite.addTest(new TestSuite(TestNullComparator2.class));
  	return suite;
      }
  
      /**
       *  Test the NullComparator with nulls high, using comparable comparator
       **/
      public static class TestNullComparator1 extends TestNullComparator {
  
  	public TestNullComparator1(String testName) {
  	    super(testName);
  	}
  
  	public Comparator makeComparator() {
  	    return new NullComparator();
  	}
  	
  	public List getComparableObjectsOrdered() {
  	    List list = new LinkedList();
  	    list.add(new Integer(1));
  	    list.add(new Integer(2));
  	    list.add(new Integer(3));
  	    list.add(new Integer(4));
  	    list.add(new Integer(5));
  	    list.add(null);
  	    return list;
  	}
  
  	public String getCanonicalComparatorName(Object object) {
  	    return super.getCanonicalComparatorName(object) + "1";
  	}
      }
  
      /**
       *  Test the NullComparator with nulls low using the comparable comparator
       **/
      public static class TestNullComparator2 extends TestNullComparator {
  
  	public TestNullComparator2(String testName) {
  	    super(testName);
  	}
  
  	public Comparator makeComparator() {
  	    return new NullComparator(false);
  	}
  	
  	public List getComparableObjectsOrdered() {
  	    List list = new LinkedList();
  	    list.add(null);
  	    list.add(new Integer(1));
  	    list.add(new Integer(2));
  	    list.add(new Integer(3));
  	    list.add(new Integer(4));
  	    list.add(new Integer(5));
  	    return list;
  	}
  
  	public String getCanonicalComparatorName(Object object) {
  	    return super.getCanonicalComparatorName(object) + "2";
  	}
      }
  }
  
  
  

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