Added utility method to produce a delta between node property maps

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@16356 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-09-18 00:16:32 +00:00
parent b0f551db34
commit 9fc752d9a7

View File

@@ -27,7 +27,9 @@ package org.alfresco.util;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.alfresco.service.namespace.QName;
@@ -71,4 +73,51 @@ public class PropertyMap extends HashMap<QName, Serializable>
{
super();
}
/**
* Utility method to work out what changed between two property maps and to generate a difference map.
*
* @param before the properties before (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.
* <tt>null</tt> values will have keys in the map, but no value.
*
* @since 3.2
*/
public static Map<QName, Serializable> getDelta(Map<QName, Serializable> before, Map<QName, Serializable> after)
{
// Shortcuts
if (before == null && after == null)
{
return Collections.emptyMap();
}
else if (before == null || before.isEmpty())
{
return after;
}
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
Map<QName, Serializable> diff = new HashMap<QName, Serializable>(after);
// Remove all entries that didn't change to leave the new or modified values
diff.entrySet().removeAll(before.entrySet());
// Now find all values values that were removed and put null values in
Set<QName> beforeKeysCopy = new HashSet<QName>(before.keySet());
beforeKeysCopy.removeAll(after.keySet());
for (QName qname : beforeKeysCopy)
{
diff.put(qname, null);
}
// Done
return diff;
}
}