commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections SetUtils.java ListUtils.java
Date Fri, 04 Apr 2003 20:40:28 GMT
scolebourne    2003/04/04 12:40:28

  Modified:    collections/src/java/org/apache/commons/collections
                        SetUtils.java ListUtils.java
  Log:
  Add new equals and hashCode methods that aid implementors of the interfaces
  from Neil O'Toole
  
  Revision  Changes    Path
  1.10      +79 -3     jakarta-commons/collections/src/java/org/apache/commons/collections/SetUtils.java
  
  Index: SetUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/SetUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SetUtils.java	10 Jan 2003 20:21:23 -0000	1.9
  +++ SetUtils.java	4 Apr 2003 20:40:28 -0000	1.10
  @@ -57,8 +57,10 @@
    */
   package org.apache.commons.collections;
   
  +import java.util.Set;
   import java.util.Collections;
   import java.util.Comparator;
  +import java.util.Iterator;
   import java.util.Set;
   import java.util.SortedSet;
   import java.util.TreeSet;
  @@ -72,6 +74,7 @@
    * 
    * @author Paul Jack
    * @author Stephen Colebourne
  + * @author Neil O'Toole
    */
   public class SetUtils {
   
  @@ -93,7 +96,77 @@
       public SetUtils() {
       }
   
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Tests two sets for equality as per the <code>equals()</code> contract
  +     * in {@link java.util.Set#equals(java.lang.Object)}.
  +     * <p>
  +     * This method is useful for implementing <code>Set</code> when you cannot
  +     * extend AbstractSet.
  +     * <p>
  +     * The relevant text (slightly paraphrased as this is a static method) is:
  +     * <blockquote>
  +     * <p>Two sets are considered equal if they have
  +     * the same size, and every member of the first set is contained in
  +     * the second. This ensures that the <tt>equals</tt> method works
  +     * properly across different implementations of the <tt>Set</tt>
  +     * interface.</p>
  +     * 
  +     * <p>
  +     * This implementation first checks if the two sets are the same object: 
  +     * if so it returns <tt>true</tt>.  Then, it checks if the two sets are
  +     * identical in size; if not, it returns false. If so, it returns
  +     * <tt>a.containsAll((Collection) b)</tt>.</p>
  +     * </blockquote>
  +     * 
  +     * @see java.util.Set
  +     * @param set1  the first set, may be null
  +     * @param set2  the second set, may be null
  +     * @return whether the sets are equal by value comparison
  +     */
  +    public static boolean equals(final Set set1, final Set set2) {
  +        if (set1 == set2) {
  +            return true;
  +        }
  +        if (set1 == null || set2 == null || set1.size() != set2.size()) {
  +            return false;
  +        }
  +
  +        return set1.containsAll(set2);
  +    }
   
  +    /**
  +     * Generates a hashcode using the algorithm specified in 
  +     * {@link java.util.Set#hashCode()}.
  +     * <p>
  +     * This method is useful for implementing <code>Set</code> when you cannot
  +     * extend AbstractSet.
  +     * 
  +     * @see java.util.Set#hashCode()
  +     * @param set  the set to calculate the hashcode for, may be null
  +     * @return the hash code
  +     */
  +    public static int hashCode(final Set set) {
  +        if (set == null) {
  +            return 0;
  +        }
  +        int hashCode = 0;
  +        Iterator it = set.iterator();
  +        Object obj = null;
  +
  +        while (it.hasNext()) {
  +            obj = it.next();
  +            if (obj != null) {
  +                hashCode += obj.hashCode();
  +            }
  +        }
  +        return hashCode;
  +    }
  +    
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Implementation of a set that checks new entries.
  +     */
       static class PredicatedSet 
               extends CollectionUtils.PredicatedCollection
               implements Set {
  @@ -104,7 +177,9 @@
   
       }
   
  -
  +    /**
  +     * Implementation of a sorted set that checks new entries.
  +     */
       static class PredicatedSortedSet 
               extends PredicatedSet 
               implements SortedSet {
  @@ -146,6 +221,7 @@
   
       }
   
  +    //-----------------------------------------------------------------------
       /**
        * Returns a synchronized set backed by the given set.
        * <p>
  
  
  
  1.14      +96 -6     jakarta-commons/collections/src/java/org/apache/commons/collections/ListUtils.java
  
  Index: ListUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ListUtils.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ListUtils.java	10 Jan 2003 20:21:23 -0000	1.13
  +++ ListUtils.java	4 Apr 2003 20:40:28 -0000	1.14
  @@ -75,6 +75,7 @@
    * @author  <a href="mailto:donaldp@apache.org">Peter Donald</a>
    * @author Paul Jack
    * @author Stephen Colebourne
  + * @author Neil O'Toole
    */
   public class ListUtils {
   
  @@ -91,6 +92,7 @@
       public ListUtils() {
       }
   
  +    //-----------------------------------------------------------------------
       /**
        * Returns a new list containing all elements that are contained in
        * both given lists.
  @@ -169,7 +171,89 @@
           return result;
       }
   
  +    /**
  +     * Tests two lists for value-equality as per the equality contract in
  +     * {@link java.util.List#equals(java.lang.Object)}.
  +     * <p>
  +     * This method is useful for implementing <code>List</code> when you cannot
  +     * extend AbstractList.
  +     * <p>
  +     * The relevant text (slightly paraphrased as this is a static method) is:
  +     * <blockquote>
  +     * Compares the two list objects for equality.  Returns
  +     * <tt>true</tt> if and only if both
  +     * lists have the same size, and all corresponding pairs of elements in
  +     * the two lists are <i>equal</i>.  (Two elements <tt>e1</tt>
and
  +     * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null
:
  +     * e1.equals(e2))</tt>.)  In other words, two lists are defined to be
  +     * equal if they contain the same elements in the same order.  This
  +     * definition ensures that the equals method works properly across
  +     * different implementations of the <tt>List</tt> interface.
  +     * </blockquote>
  +     *
  +     * <b>Note:</b> The behaviour of this method is undefined if the lists
are
  +     * modified during the equals comparison.
  +     * 
  +     * @see java.util.List
  +     * @param list1  the first list, may be null
  +     * @param list2  the second list, may be null
  +     * @return whether the lists are equal by value comparison
  +     */
  +    public static boolean equals(final List list1, final List list2) {
  +        if (list1 == list2) {
  +            return true;
  +        }
  +        if (list1 == null || list2 == null || list1.size() != list2.size()) {
  +            return false;
  +        }
  +
  +        Iterator it1 = list1.iterator();
  +        Iterator it2 = list2.iterator();
  +        Object obj1 = null;
  +        Object obj2 = null;
   
  +        while (it1.hasNext() && it2.hasNext()) {
  +            obj1 = it1.next();
  +            obj2 = it2.next();
  +
  +            if (!(obj1 == null ? obj2 == null : obj1.equals(obj2))) {
  +                return false;
  +            }
  +        }
  +
  +        return !(it1.hasNext() || it2.hasNext());
  +    }
  +    
  +    /**
  +     * Generates a hashcode using the algorithm specified in 
  +     * {@link java.util.List#hashCode()}.
  +     * <p>
  +     * This method is useful for implementing <code>List</code> when you cannot
  +     * extend AbstractList.
  +     * 
  +     * @see java.util.List#hashCode()
  +     * @param list  the list to generate the hashCode for, may be null
  +     * @return the hash code
  +     */
  +    public static int hashCode(final List list) {
  +        if (list == null) {
  +            return 0;
  +        }
  +        int hashCode = 1;
  +        Iterator it = list.iterator();
  +        Object obj = null;
  +        
  +        while (it.hasNext()) {
  +            obj = it.next();
  +            hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
  +        }
  +        return hashCode;
  +    }   
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Implementation of a ListIterator that wraps an original.
  +     */
       static class ListIteratorWrapper 
               implements ListIterator {
   
  @@ -217,7 +301,9 @@
   
       }
   
  -
  +    /**
  +     * Implementation of a list that checks (predicates) each entry.
  +     */
       static class PredicatedList 
               extends CollectionUtils.PredicatedCollection
               implements List {
  @@ -288,7 +374,9 @@
   
       }
   
  -
  +    /**
  +     * Implementation of a list that has a fixed size.
  +     */
       static class FixedSizeList 
               extends CollectionUtils.UnmodifiableCollection
               implements List {
  @@ -356,7 +444,9 @@
   
       }
   
  -
  +    /**
  +     * Implementation of a list that creates objects on demand.
  +     */
       static class LazyList 
               extends CollectionUtils.CollectionWrapper 
               implements List {
  @@ -451,7 +541,7 @@
   
       }
   
  -
  +    //-----------------------------------------------------------------------
       /**
        * Returns a synchronized list backed by the given list.
        * <p>
  
  
  

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


Mime
View raw message