Author: julien(a)jboss.com
Date: 2008-02-01 21:00:40 -0500 (Fri, 01 Feb 2008)
New Revision: 9710
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java
Log:
implemented abstract typed map key set equals and hashcode methods
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java 2008-02-01
23:56:02 UTC (rev 9709)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/util/AbstractTypedMap.java 2008-02-02
02:00:40 UTC (rev 9710)
@@ -182,6 +182,15 @@
return !(left == null || right == null) && converter.equals(left, right);
}
+ /** . */
+ private KeySet keySet;
+
+ /** . */
+ private TypedEntrySet entrySet;
+
+ /** . */
+ private ValueCollection values;
+
public abstract Converter<EK, IK> getKeyConverter();
public abstract Converter<EV, IV> getValueConverter();
@@ -212,7 +221,11 @@
public final Set<EK> keySet()
{
- return new KeySet();
+ if (keySet == null)
+ {
+ keySet = new KeySet();
+ }
+ return keySet;
}
public final EV put(EK ek, EV ev)
@@ -298,7 +311,11 @@
public final Set<Entry<EK, EV>> entrySet()
{
- return new TypedEntrySet();
+ if (entrySet == null)
+ {
+ entrySet = new TypedEntrySet();
+ }
+ return entrySet;
}
public final void putAll(Map<? extends EK, ? extends EV> em)
@@ -309,7 +326,11 @@
public final Collection<EV> values()
{
- return new ValueCollection();
+ if (values == null)
+ {
+ values = new ValueCollection();
+ }
+ return values;
}
/** Compare to parameters objects. */
@@ -665,6 +686,69 @@
{
super(getDelegate().keySet(), getKeyConverter().unwrapper,
getKeyConverter().wrapper);
}
+
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ {
+ return true;
+ }
+ if (obj instanceof Set)
+ {
+ Set that = (Set)obj;
+
+ //
+ if (size() != that.size())
+ {
+ return false;
+ }
+
+ //
+ for (Object o : that)
+ {
+ try
+ {
+ if (!contains(o))
+ {
+ return false;
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ return false;
+ }
+ catch (ClassCastException e)
+ {
+ return false;
+ }
+ catch (NullPointerException e)
+ {
+ return false;
+ }
+ }
+
+ //
+ return true;
+ }
+ return false;
+ }
+
+ public int hashCode()
+ {
+ int hashCode = 0;
+
+ //
+ for (Object o : this)
+ {
+ if (o != null)
+ {
+ hashCode += o.hashCode();
+ }
+ }
+
+ //
+ return hashCode;
+ }
}
private final class ValueCollection extends TypedCollection<EV,IV>
Modified:
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java
===================================================================
---
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java 2008-02-01
23:56:02 UTC (rev 9709)
+++
modules/common/trunk/common/src/test/java/org/jboss/portal/test/common/util/typedmap/TypedMapKeySetTestCase.java 2008-02-02
02:00:40 UTC (rev 9710)
@@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
+import java.util.HashSet;
import org.jboss.portal.common.util.Tools;
@@ -668,7 +669,14 @@
public void testEquals()
{
- // todo
+ Set<String> same = new HashSet<String>();
+ same.add("zero");
+ same.add("one");
+
+ assertTrue(keys.equals(same));
+
+
+
}
public void testHashCode()
Show replies by date