[jboss-cvs] JBossCache/src/org/jboss/cache/util ...
Manik Surtani
msurtani at jboss.com
Wed Jan 3 12:50:31 EST 2007
User: msurtani
Date: 07/01/03 12:50:31
Modified: src/org/jboss/cache/util Util.java
Log:
added more rich nodeModified callback
Revision Changes Path
1.2 +95 -6 JBossCache/src/org/jboss/cache/util/Util.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Util.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/util/Util.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- Util.java 25 Oct 2006 04:44:42 -0000 1.1
+++ Util.java 3 Jan 2007 17:50:31 -0000 1.2
@@ -21,11 +21,14 @@
*/
package org.jboss.cache.util;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* General utility methods used throughout the JBC code base.
*
* @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public final class Util
{
@@ -49,7 +52,93 @@
return cl.loadClass(classname);
}
- /** Prevent instantiation */
- private Util() {}
+ /**
+ * Prevent instantiation
+ */
+ private Util()
+ {
+ }
+
+ /**
+ * Calculates the diffs between data maps passed in to {@link org.jboss.cache.CacheListener#nodeModified(org.jboss.cache.Fqn,boolean,boolean,java.util.Map)}
+ * before and after modification. This only makes sense if the modification type is {@link org.jboss.cache.CacheListener.ModificationType#PUT_MAP}. See the javadoc on
+ * {@link org.jboss.cache.CacheListener#nodeModified(org.jboss.cache.Fqn,boolean,boolean,java.util.Map)} for more details.
+ *
+ * @param pre map of data before the node was modified
+ * @param post Map of data after the node was modified
+ * @return MapModifications containing the differences.
+ */
+ public static MapModifications diffNodeData(Map<Object, Object> pre, Map<Object, Object> post)
+ {
+ MapModifications mods = new MapModifications();
+
+ // let's start with what's been added and modified.
+ for (Object key : post.keySet())
+ {
+ if (pre.containsKey(key))
+ {
+ if (!post.get(key).equals(pre.get(key)))
+ {
+ mods.modifiedEntries.put(key, post.get(key));
+ }
+ }
+ else
+ {
+ mods.addedEntries.put(key, post.get(key));
+ }
+ }
+
+ // now the removed entries.
+ for (Object key : pre.keySet())
+ {
+ if (!post.containsKey(key))
+ {
+ mods.removedEntries.put(key, pre.get(key));
+ }
+ }
+
+ return mods;
+ }
+
+ /**
+ * Static inner class that holds 3 maps - for data added, removed and modified.
+ */
+ public static class MapModifications
+ {
+ public final Map<Object, Object> addedEntries = new HashMap<Object, Object>();
+ public final Map<Object, Object> removedEntries = new HashMap<Object, Object>();
+ public final Map<Object, Object> modifiedEntries = new HashMap<Object, Object>();
+
+
+ public boolean equals(Object o)
+ {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ MapModifications that = (MapModifications) o;
+
+ if (addedEntries != null ? !addedEntries.equals(that.addedEntries) : that.addedEntries != null) return false;
+ if (modifiedEntries != null ? !modifiedEntries.equals(that.modifiedEntries) : that.modifiedEntries != null)
+ return false;
+ if (removedEntries != null ? !removedEntries.equals(that.removedEntries) : that.removedEntries != null)
+ return false;
+
+ return true;
+ }
+
+ public int hashCode()
+ {
+ int result;
+ result = (addedEntries != null ? addedEntries.hashCode() : 0);
+ result = 31 * result + (removedEntries != null ? removedEntries.hashCode() : 0);
+ result = 31 * result + (modifiedEntries != null ? modifiedEntries.hashCode() : 0);
+ return result;
+ }
+
+ public String toString()
+ {
+ return "Added Entries " + addedEntries + " Removeed Entries " + removedEntries + " Modified Entries " + modifiedEntries;
+ }
+ }
}
More information about the jboss-cvs-commits
mailing list