[utilities COMMIT] in /java-support/trunk/src: main/java/net/shibboleth/utilities/java/support/collection/ValueTypeIn...
noreply at shibboleth.net
noreply at shibboleth.net
Thu Jan 5 11:55:59 GMT 2012
Author: rdw
Date: Thu Jan 5 11:55:59 2012
New Revision: 191
URL: http://svn.shibboleth.net/view/utilities?rev=191&view=rev
Log:
Add equals() and hashCode() to ValueTypeIndexedMap, plus test.
Also align javadoc with code.
Modified:
java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMap.java
java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMapTest.java
Modified: java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMap.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMap.java?rev=191&r1=190&r2=191&view=diff
==============================================================================
--- java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMap.java (original)
+++ java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMap.java Thu Jan 5 11:55:59 2012
@@ -214,7 +214,7 @@
*
* @param <SubType> type of values to include in the returned map
* @param type type of values to return
- * @return sub map of entries whose value is of type SubType or null if the specified type is not a valid type for
+ * @return sub map of entries whose value is of type SubType or empty if the specified type is not a valid type for
* this map.
*/
@SuppressWarnings("unchecked")
@@ -231,6 +231,24 @@
}
/** {@inheritDoc} */
+ public int hashCode() {
+ return map.hashCode();
+ }
+
+ /** {@inheritDoc} */
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (obj == null || this.getClass() != obj.getClass()) {
+ return false;
+ }
+
+ return map.equals(((ValueTypeIndexedMap<?,?>) obj).map);
+ }
+
+ /** {@inheritDoc} */
public String toString() {
return map.toString();
}
Modified: java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMapTest.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMapTest.java?rev=191&r1=190&r2=191&view=diff
==============================================================================
--- java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMapTest.java (original)
+++ java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/collection/ValueTypeIndexedMapTest.java Thu Jan 5 11:55:59 2012
@@ -112,5 +112,31 @@
Assert.assertEquals(map.subMap(String.class).size(), 1);
Assert.assertEquals(map.subMap(null).size(), 2);
}
+
+ /* Test equals and hashcode */
+ @Test
+ public void testEqualsHashCode() {
+ ValueTypeIndexedMap<String, Object> other = new ValueTypeIndexedMap<String, Object>();
+ other.setTypes(Arrays.asList(new Class[] {Integer.class}));
+ other.rebuildIndex();
+
+ Assert.assertEquals(map, other, "Empty maps should be the same");
+ Assert.assertEquals(map.hashCode(), other.hashCode(), "Empty maps have same hash code");
+
+ map.put("i1", Integer.parseInt("4"));
+ map.put("s1", "first string");
+ map.put("s2", "second string");
+ other.put("i1", Integer.parseInt("4"));
+
+ Assert.assertNotSame(map, other, "Different maps should differ");
+ Assert.assertNotSame(map.hashCode(), other.hashCode(), "Different maps should have different hash codes");
+
+ map.remove("s1");
+ other.put("s2", "second string");
+
+ Assert.assertEquals(map, other, "Similar maps should be equals");
+ Assert.assertEquals(map.hashCode(), other.hashCode(), "Similar maps should have the same hash codes");
+
+ }
}
More information about the commits
mailing list