Add new equals and hashCode methods that aid implementors of the interfaces
from Neil O'Toole git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130990 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a73594a4b2
commit
de8be58655
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/ListUtils.java,v 1.13 2003/01/10 20:21:23 rwaldhoff Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/ListUtils.java,v 1.14 2003/04/04 20:40:28 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -69,12 +69,13 @@ import java.util.ListIterator;
|
|||
* instances.
|
||||
*
|
||||
* @since Commons Collections 1.0
|
||||
* @version $Revision: 1.13 $ $Date: 2003/01/10 20:21:23 $
|
||||
* @version $Revision: 1.14 $ $Date: 2003/04/04 20:40:28 $
|
||||
*
|
||||
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
|
||||
* @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 class ListUtils {
|
|||
public ListUtils() {
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Returns a new list containing all elements that are contained in
|
||||
* both given lists.
|
||||
|
@ -169,7 +171,89 @@ public class ListUtils {
|
|||
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 @@ public class ListUtils {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of a list that checks (predicates) each entry.
|
||||
*/
|
||||
static class PredicatedList
|
||||
extends CollectionUtils.PredicatedCollection
|
||||
implements List {
|
||||
|
@ -288,7 +374,9 @@ public class ListUtils {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of a list that has a fixed size.
|
||||
*/
|
||||
static class FixedSizeList
|
||||
extends CollectionUtils.UnmodifiableCollection
|
||||
implements List {
|
||||
|
@ -356,7 +444,9 @@ public class ListUtils {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of a list that creates objects on demand.
|
||||
*/
|
||||
static class LazyList
|
||||
extends CollectionUtils.CollectionWrapper
|
||||
implements List {
|
||||
|
@ -451,7 +541,7 @@ public class ListUtils {
|
|||
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Returns a synchronized list backed by the given list.
|
||||
* <p>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/SetUtils.java,v 1.9 2003/01/10 20:21:23 rwaldhoff Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/SetUtils.java,v 1.10 2003/04/04 20:40:28 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -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;
|
||||
|
@ -67,11 +69,12 @@ import java.util.TreeSet;
|
|||
* Provides static utility methods and decorators for {@link Set}
|
||||
* and {@link SortedSet} instances.
|
||||
*
|
||||
* @version $Revision: 1.9 $ $Date: 2003/01/10 20:21:23 $
|
||||
* @version $Revision: 1.10 $ $Date: 2003/04/04 20:40:28 $
|
||||
* @since Commons Collection 2.1
|
||||
*
|
||||
* @author Paul Jack
|
||||
* @author Stephen Colebourne
|
||||
* @author Neil O'Toole
|
||||
*/
|
||||
public class SetUtils {
|
||||
|
||||
|
@ -93,7 +96,77 @@ public class SetUtils {
|
|||
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 @@ public class SetUtils {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of a sorted set that checks new entries.
|
||||
*/
|
||||
static class PredicatedSortedSet
|
||||
extends PredicatedSet
|
||||
implements SortedSet {
|
||||
|
@ -146,6 +221,7 @@ public class SetUtils {
|
|||
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Returns a synchronized set backed by the given set.
|
||||
* <p>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestListUtils.java,v 1.5 2002/10/12 22:36:22 scolebourne Exp $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2002/10/12 22:36:22 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestListUtils.java,v 1.6 2003/04/04 20:42:02 scolebourne Exp $
|
||||
* $Revision: 1.6 $
|
||||
* $Date: 2003/04/04 20:42:02 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -61,12 +61,18 @@
|
|||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for ListUtils.
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Neil O'Toole
|
||||
*/
|
||||
public class TestListUtils extends BulkTest {
|
||||
|
||||
public TestListUtils(String name) {
|
||||
|
@ -126,7 +132,34 @@ public class TestListUtils extends BulkTest {
|
|||
assertEquals(6, list.size());
|
||||
}
|
||||
|
||||
|
||||
public void testEquals() {
|
||||
Collection data = Arrays.asList( new String[] { "a", "b", "c" });
|
||||
|
||||
List a = new ArrayList( data );
|
||||
List b = new ArrayList( data );
|
||||
|
||||
assertEquals(true, a.equals(b));
|
||||
assertEquals(true, ListUtils.equals(a, b));
|
||||
a.clear();
|
||||
assertEquals(false, ListUtils.equals(a, b));
|
||||
assertEquals(false, ListUtils.equals(a, null));
|
||||
assertEquals(false, ListUtils.equals(null, b));
|
||||
assertEquals(true, ListUtils.equals(null, null));
|
||||
}
|
||||
|
||||
public void testHashCode() {
|
||||
Collection data = Arrays.asList( new String[] { "a", "b", "c" });
|
||||
|
||||
List a = new ArrayList( data );
|
||||
List b = new ArrayList( data );
|
||||
|
||||
assertEquals(true, a.hashCode() == b.hashCode());
|
||||
assertEquals(true, a.hashCode() == ListUtils.hashCode(a));
|
||||
assertEquals(true, b.hashCode() == ListUtils.hashCode(b));
|
||||
assertEquals(true, ListUtils.hashCode(a) == ListUtils.hashCode(b));
|
||||
a.clear();
|
||||
assertEquals(false, ListUtils.hashCode(a) == ListUtils.hashCode(b));
|
||||
assertEquals(0, ListUtils.hashCode(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestSetUtils.java,v 1.3 2003/02/19 20:33:11 scolebourne Exp $
|
||||
* $Revision: 1.3 $
|
||||
* $Date: 2003/02/19 20:33:11 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestSetUtils.java,v 1.4 2003/04/04 20:42:03 scolebourne Exp $
|
||||
* $Revision: 1.4 $
|
||||
* $Date: 2003/04/04 20:42:03 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -60,6 +60,7 @@
|
|||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
@ -68,7 +69,10 @@ import junit.framework.Test;
|
|||
|
||||
|
||||
/**
|
||||
* Tests for SetUtils.
|
||||
* Tests for SetUtils.
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Neil O'Toole
|
||||
*/
|
||||
public class TestSetUtils extends BulkTest {
|
||||
|
||||
|
@ -110,7 +114,34 @@ public class TestSetUtils extends BulkTest {
|
|||
};
|
||||
}
|
||||
|
||||
public void testEquals() {
|
||||
Collection data = Arrays.asList( new String[] { "a", "b", "c" });
|
||||
|
||||
Set a = new HashSet( data );
|
||||
Set b = new HashSet( data );
|
||||
|
||||
assertEquals(true, a.equals(b));
|
||||
assertEquals(true, SetUtils.equals(a, b));
|
||||
a.clear();
|
||||
assertEquals(false, SetUtils.equals(a, b));
|
||||
assertEquals(false, SetUtils.equals(a, null));
|
||||
assertEquals(false, SetUtils.equals(null, b));
|
||||
assertEquals(true, SetUtils.equals(null, null));
|
||||
}
|
||||
|
||||
public void testHashCode() {
|
||||
Collection data = Arrays.asList( new String[] { "a", "b", "c" });
|
||||
|
||||
Set a = new HashSet( data );
|
||||
Set b = new HashSet( data );
|
||||
|
||||
assertEquals(true, a.hashCode() == b.hashCode());
|
||||
assertEquals(true, a.hashCode() == SetUtils.hashCode(a));
|
||||
assertEquals(true, b.hashCode() == SetUtils.hashCode(b));
|
||||
assertEquals(true, SetUtils.hashCode(a) == SetUtils.hashCode(b));
|
||||
a.clear();
|
||||
assertEquals(false, SetUtils.hashCode(a) == SetUtils.hashCode(b));
|
||||
assertEquals(0, SetUtils.hashCode(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue