[jboss-cvs] JBossAS SVN: r104946 - in projects/cluster/ha-server-api/trunk/src: test/java/org/jboss/test/ha/framework/server and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue May 18 17:39:30 EDT 2010
Author: bstansberry at jboss.com
Date: 2010-05-18 17:39:29 -0400 (Tue, 18 May 2010)
New Revision: 104946
Modified:
projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/SimpleCachableMarshalledValue.java
projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/SimpleCachableMarshalledValueUnitTestCase.java
Log:
[JBCLUSTER-270] Make it possible to avoid invoking on wrapped object
Modified: projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/SimpleCachableMarshalledValue.java
===================================================================
--- projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/SimpleCachableMarshalledValue.java 2010-05-18 20:14:02 UTC (rev 104945)
+++ projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/SimpleCachableMarshalledValue.java 2010-05-18 21:39:29 UTC (rev 104946)
@@ -44,6 +44,19 @@
public class SimpleCachableMarshalledValue
implements java.io.Externalizable, CachableMarshalledValue
{
+ /**
+ * Value returned by {@link #hashCode()} if <code>true</code> was passed
+ * as the <code>ignoreHashCode</code> param to the constructor.
+ */
+ public static final int OUR_HASH_CODE = Integer.MAX_VALUE;
+
+ /**
+ * Value returned by {@link #hashCode()} if <code>false</code> was passed
+ * to the constructor as the <code>ignoreHashCode</code> param and <code>null</code>
+ * was passed as the <code>obj</code> param.
+ */
+ public static final int NULL_HASH_CODE = 0;
+
/**
* The serialized form of the value.
*/
@@ -54,7 +67,7 @@
*/
private Serializable raw;
- private int hashCode;
+ private int hashCode = Integer.MAX_VALUE;
private transient ObjectStreamSource objectStreamSource;
@@ -66,18 +79,72 @@
super();
}
+ /**
+ * Create a new SimpleCachableMarshalledValue. Same as
+ * <code SimpleCachableMarshalledValue(obj, false)</code>.
+ *
+ * @param obj the object to wrap
+ */
public SimpleCachableMarshalledValue(Serializable obj)
{
+ this(obj, false);
+ }
+
+ /**
+ * Create a new SimpleCachableMarshalledValue.
+ *
+ * @param obj the object to wrap. May be <code>null</code>
+ *
+ * @param ignoreHashCode <code>true</code> if this object is not expected to be
+ * used as a key in any hashmap/HashSet like structures, where its hashcode
+ * should be the same as <code>obj</code>'s hashcode. If <code>false</code>
+ * this constructor will call <code>obj.hashCode()</code> and
+ * cache the value.
+ */
+ public SimpleCachableMarshalledValue(Serializable obj, boolean ignoreHashCode)
+ {
this.raw = obj;
- if (this.raw != null)
+ if (!ignoreHashCode)
{
- this.hashCode = raw.hashCode();
+ if (this.raw != null)
+ {
+ this.hashCode = raw.hashCode();
+ }
+ else
+ {
+ this.hashCode = 0;
+ }
}
}
+ /**
+ * Create a new SimpleCachableMarshalledValue. Same as
+ * <code SimpleCachableMarshalledValue(obj, streamSource, false)</code>.
+ *
+ * @param obj the object to wrap. May be <code>null</code>
+ * @param streamSource source for object streams used for serialization and
+ * deserialization. May be <code>null</code>
+ */
public SimpleCachableMarshalledValue(Serializable obj, ObjectStreamSource streamSource)
{
- this(obj);
+ this(obj, streamSource, false);
+ }
+
+ /**
+ * Create a new SimpleCachableMarshalledValue.
+ *
+ * @param obj the object to wrap. May be <code>null</code>
+ * @param streamSource source for object streams used for serialization and
+ * deserialization. May be <code>null</code>
+ * @param ignoreHashCode <code>true</code> if this object is not expected to be
+ * used as a key in any hashmap/HashSet like structures, where its hashcode
+ * should be the same as <code>obj</code>'s hashcode. If <code>false</code>
+ * this constructor will call <code>obj.hashCode()</code> and
+ * cache the value.
+ */
+ public SimpleCachableMarshalledValue(Serializable obj, ObjectStreamSource streamSource, boolean ignoreHashCode)
+ {
+ this(obj, ignoreHashCode);
this.objectStreamSource = streamSource;
}
@@ -140,9 +207,11 @@
}
/**
- * Return a hash code for the wrapped object.
+ * Returns either the hashcode of the wrapped object or {@link Integer#MAX_VALUE};
+ * which depends on whether the <code>ignoreHashCode</code>
+ * param was set to <code>true</code> in this object's constructor.
*
- * @return the serialized form value hash.
+ * @return either the hashcode of the wrapped object or {@link Integer#MAX_VALUE}
*/
@Override
public int hashCode()
@@ -199,7 +268,16 @@
{
StringBuilder sb = new StringBuilder(getClass().getName());
sb.append("{raw=");
- sb.append(raw == null ? "null" : raw);
+ if (raw == null)
+ {
+ sb.append("null");
+ }
+ else
+ {
+ sb.append(raw.getClass().getName());
+ sb.append('@');
+ sb.append(System.identityHashCode(raw));
+ }
sb.append("serialized=");
sb.append(serializedForm==null ? "false" : "true");
sb.append('}');
Modified: projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/SimpleCachableMarshalledValueUnitTestCase.java
===================================================================
--- projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/SimpleCachableMarshalledValueUnitTestCase.java 2010-05-18 20:14:02 UTC (rev 104945)
+++ projects/cluster/ha-server-api/trunk/src/test/java/org/jboss/test/ha/framework/server/SimpleCachableMarshalledValueUnitTestCase.java 2010-05-18 21:39:29 UTC (rev 104946)
@@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.io.Serializable;
import org.jboss.ha.framework.server.SimpleCachableMarshalledValue;
import org.jboss.util.id.GUID;
@@ -163,6 +164,27 @@
mv = new SimpleCachableMarshalledValue(null);
assertEquals(0, mv.hashCode());
+
+ FragileObject fragile = new FragileObject();
+ mv = new SimpleCachableMarshalledValue(fragile, true);
+
+ assertEquals(SimpleCachableMarshalledValue.OUR_HASH_CODE, mv.hashCode());
+
+ copy = replicate(mv);
+ assertEquals(mv.hashCode(), copy.hashCode());
+
+ mv = new SimpleCachableMarshalledValue(null);
+
+ assertEquals(SimpleCachableMarshalledValue.NULL_HASH_CODE, mv.hashCode());
+
+ copy = replicate(mv);
+ assertEquals(mv.hashCode(), copy.hashCode());
+ mv = new SimpleCachableMarshalledValue(null, true);
+
+ assertEquals(SimpleCachableMarshalledValue.OUR_HASH_CODE, mv.hashCode());
+
+ copy = replicate(mv);
+ assertEquals(mv.hashCode(), copy.hashCode());
}
/**
@@ -177,6 +199,13 @@
SimpleCachableMarshalledValue copy = replicate(mv);
assertNotNull(copy.toString());
+ FragileObject fragile = new FragileObject();
+ mv = new SimpleCachableMarshalledValue(fragile, true);
+ assertNotNull(mv.toString());
+
+ copy = replicate(mv);
+ assertNotNull(copy.toString());
+
mv = new SimpleCachableMarshalledValue(null);
assertNotNull(mv.toString());
}
@@ -210,4 +239,33 @@
ois.close();
}
}
+
+ /**
+ * A class that throws exceptions if any of its methods are invoked. Used
+ * to test that such methods are not invoked.
+ */
+ private static class FragileObject implements Serializable
+ {
+ /** The serialVersionUID */
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public int hashCode()
+ {
+ throw new RuntimeException("Someone called hashCode()!!");
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ throw new RuntimeException("Someone called equals()!!");
+ }
+
+ @Override
+ public String toString()
+ {
+ throw new RuntimeException("Someone called toString()!!");
+ }
+
+ }
}
More information about the jboss-cvs-commits
mailing list