Missed check in

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@16381 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-09-19 14:17:44 +00:00
parent c3bf64c111
commit db8181d496

View File

@@ -27,9 +27,7 @@ package org.alfresco.util;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
@@ -75,49 +73,37 @@ public class PropertyMap extends HashMap<QName, Serializable>
} }
/** /**
* Utility method to work out what changed between two property maps and to generate a difference map. * Utility method to remove unchanged entries from each map.
* *
* @param before the properties before (may be <tt>null</tt>) * @param before the properties before (may be <tt>null</tt>)
* @param after the properties after (may be <tt>null</tt>) * @param after the properties after (may be <tt>null</tt>)
* @return Return a map of values that <b>changed</b> from before to after. * @return Return a map of values that <b>changed</b> from before to after.
* <tt>null</tt> values will have keys in the map, but no value. * The before value is first and the after value is second.
* *
* @since 3.2 * @since 3.2
*/ */
public static Map<QName, Serializable> getDelta(Map<QName, Serializable> before, Map<QName, Serializable> after) public static Pair<Map<QName, Serializable>, Map<QName, Serializable>> getBeforeAndAfterMapsForChanges(
Map<QName, Serializable> before,
Map<QName, Serializable> after)
{ {
// Shortcuts // Shortcuts
if (before == null && after == null) if (before == null)
{ {
return Collections.emptyMap(); before = Collections.emptyMap();
} }
else if (before == null || before.isEmpty()) if (after == null)
{ {
return after; after = Collections.emptyMap();
}
else if (after == null || after.isEmpty())
{
// Return the 'before' map but with no values
Map<QName, Serializable> diff = new HashMap<QName, Serializable>(before.size() * 2);
for (QName qname : before.keySet())
{
diff.put(qname, null);
}
return diff;
} }
// Make a copy of the after values // Get after values that changed
Map<QName, Serializable> diff = new HashMap<QName, Serializable>(after); Map<QName, Serializable> afterDelta = new HashMap<QName, Serializable>(after);
// Remove all entries that didn't change to leave the new or modified values afterDelta.entrySet().removeAll(before.entrySet());
diff.entrySet().removeAll(before.entrySet()); // Get before values that changed
// Now find all values values that were removed and put null values in Map<QName, Serializable> beforeDelta = new HashMap<QName, Serializable>(before);
Set<QName> beforeKeysCopy = new HashSet<QName>(before.keySet()); beforeDelta.entrySet().removeAll(after.entrySet());
beforeKeysCopy.removeAll(after.keySet());
for (QName qname : beforeKeysCopy)
{
diff.put(qname, null);
}
// Done // Done
return diff; return new Pair<Map<QName, Serializable>, Map<QName, Serializable>>(beforeDelta, afterDelta);
} }
} }