You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/05/27 21:22:27 UTC
svn commit: r948960 - in /pivot/trunk/core:
src/org/apache/pivot/collections/HashMap.java
src/org/apache/pivot/collections/HashSet.java
test/org/apache/pivot/collections/test/HashMapTest.java
test/org/apache/pivot/collections/test/HashSetTest.java
Author: gbrown
Date: Thu May 27 19:22:26 2010
New Revision: 948960
URL: http://svn.apache.org/viewvc?rev=948960&view=rev
Log:
Add equals()/hashCode() to HashMap and HashSet.
Modified:
pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java
pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java
pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java
pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java
Modified: pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java?rev=948960&r1=948959&r2=948960&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/HashMap.java Thu May 27 19:22:26 2010
@@ -427,6 +427,47 @@ public class HashMap<K, V> implements Ma
}
@Override
+ @SuppressWarnings("unchecked")
+ public boolean equals(Object o) {
+ boolean equals = false;
+
+ if (o instanceof Map<?, ?>) {
+ Map<K, V> map = (Map<K, V>)o;
+
+ if (getCount() == map.getCount()) {
+ for (K key : this) {
+ V value = get(key);
+
+ if (value == null) {
+ equals = (map.containsKey(key)
+ && map.get(key) == null);
+ } else {
+ equals = value.equals(map.get(key));
+ }
+
+ if (!equals) {
+ break;
+ }
+ }
+
+ }
+ }
+
+ return equals;
+ }
+
+ @Override
+ public int hashCode() {
+ int hashCode = 0;
+
+ for (K key : this) {
+ hashCode += key.hashCode();
+ }
+
+ return hashCode;
+ }
+
+ @Override
public String toString() {
StringBuilder sb = new StringBuilder();
Modified: pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java?rev=948960&r1=948959&r2=948960&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/HashSet.java Thu May 27 19:22:26 2010
@@ -187,6 +187,40 @@ public class HashSet<E> implements Set<E
}
@Override
+ @SuppressWarnings("unchecked")
+ public boolean equals(Object o) {
+ boolean equals = false;
+
+ if (o instanceof Set<?>) {
+ Set<E> set = (Set<E>)o;
+
+ if (getCount() == set.getCount()) {
+ for (E element : this) {
+ equals = set.contains(element);
+
+ if (!equals) {
+ break;
+ }
+ }
+
+ }
+ }
+
+ return equals;
+ }
+
+ @Override
+ public int hashCode() {
+ int hashCode = 0;
+
+ for (E element : this) {
+ hashCode += element.hashCode();
+ }
+
+ return hashCode;
+ }
+
+ @Override
public String toString() {
StringBuilder sb = new StringBuilder();
Modified: pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java?rev=948960&r1=948959&r2=948960&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/collections/test/HashMapTest.java Thu May 27 19:22:26 2010
@@ -17,7 +17,7 @@
package org.apache.pivot.collections.test;
- import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -197,4 +197,35 @@ public class HashMapTest {
System.out.println(key);
}
}
+
+ @Test
+ public void equalsTest() {
+ HashMap<String, String> map1 = new HashMap<String, String>();
+ map1.put("a", "one");
+ map1.put("b", "two");
+ map1.put("c", "three");
+
+ HashMap<String, String> map2 = new HashMap<String, String>();
+ map2.put("a", "one");
+ map2.put("b", "two");
+ map2.put("c", "three");
+
+ // Same
+ assertTrue(map1.equals(map2));
+
+ // Different values
+ map2.put("c", "four");
+ assertFalse(map1.equals(map2));
+
+ map1.put("c", null);
+ assertFalse(map1.equals(map2));
+
+ // Null comparison
+ map2.put("c", null);
+ assertTrue(map1.equals(map2));
+
+ // Different lengths
+ map2.put("d", "four");
+ assertFalse(map1.equals(map2));
+ }
}
Modified: pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java?rev=948960&r1=948959&r2=948960&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java (original)
+++ pivot/trunk/core/test/org/apache/pivot/collections/test/HashSetTest.java Thu May 27 19:22:26 2010
@@ -76,4 +76,29 @@ public class HashSetTest {
}
assertEquals(0, set.getCount());
}
+
+ @Test
+ public void equalsTest() {
+ HashSet<String> set1 = new HashSet<String>();
+ set1.add("one");
+ set1.add("two");
+ set1.add("three");
+
+ HashSet<String> set2 = new HashSet<String>();
+ set2.add("one");
+ set2.add("two");
+ set2.add("three");
+
+ // Same
+ assertTrue(set1.equals(set2));
+
+ // Different values
+ set2.remove("three");
+ set2.add("four");
+ assertFalse(set1.equals(set2));
+
+ // Different lengths
+ set2.add("three");
+ assertFalse(set1.equals(set2));
+ }
}