diff --git a/source/java/org/alfresco/util/PropertyMap.java b/source/java/org/alfresco/util/PropertyMap.java index 4185783ca1..078d93586e 100644 --- a/source/java/org/alfresco/util/PropertyMap.java +++ b/source/java/org/alfresco/util/PropertyMap.java @@ -22,9 +22,9 @@ import java.io.Serializable; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import org.alfresco.service.namespace.QName; -import org.alfresco.util.Pair; /** * Property map helper class. @@ -101,4 +101,112 @@ public class PropertyMap extends HashMap // Done return new Pair, Map>(beforeDelta, afterDelta); } + + /** + * Utility method to get properties which were added as part of a change. + * + * @param before the properties before (may be null). + * @param after the properties after (may be null). + * @return a map of values that were added along with their new values. + * + * @since Odin + */ + public static Map getAddedProperties(Map before, Map after) + { + Map result = new HashMap(); + + if (before != null && after != null) + { + result.putAll(after); + result.keySet().removeAll(before.keySet()); + } + + return result; + } + + /** + * Utility method to get properties which were removed as part of a change. + * + * @param before the properties before (may be null). + * @param after the properties after (may be null). + * @return a map of values that were removed along with their old values. + * + * @since Odin + */ + public static Map getRemovedProperties(Map before, Map after) + { + Map result = new HashMap(); + + if (before != null && after != null) + { + result.putAll(before); + result.keySet().removeAll(after.keySet()); + } + + return result; + } + + /** + * Utility method to get properties which were changed (but not added or removed) as part of a change. + * + * @param before the properties before (may be null). + * @param after the properties after (may be null). + * @return a map of values that were changed along with their new values. + * + * @since Odin + */ + public static Map getChangedProperties(Map before, Map after) + { + Map result = new HashMap(); + + if (before != null && after != null) + { + Map intersection = new HashMap(); + + intersection.putAll(after); + intersection.keySet().retainAll(before.keySet()); + + for (Entry entry : intersection.entrySet()) + { + if ( ! EqualsHelper.nullSafeEquals(before.get(entry.getKey()), after.get(entry.getKey()))) + { + result.put(entry.getKey(), entry.getValue()); + } + } + } + + return result; + } + + /** + * Utility method to get properties which were unchanged as part of a change. + * + * @param before the properties before (may be null). + * @param after the properties after (may be null). + * @return a map of values that were unchanged along with their values. + * + * @since Odin + */ + public static Map getUnchangedProperties(Map before, Map after) + { + Map result = new HashMap(); + + if (before != null && after != null) + { + Map intersection = new HashMap(); + + intersection.putAll(before); + intersection.keySet().retainAll(after.keySet()); + + for (Entry entry : intersection.entrySet()) + { + if ( EqualsHelper.nullSafeEquals(before.get(entry.getKey()), after.get(entry.getKey()))) + { + result.put(entry.getKey(), entry.getValue()); + } + } + } + + return result; + } } diff --git a/source/java/org/alfresco/util/PropertyMapTest.java b/source/java/org/alfresco/util/PropertyMapTest.java new file mode 100644 index 0000000000..2fd057f576 --- /dev/null +++ b/source/java/org/alfresco/util/PropertyMapTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2005-2012 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.util; + +import static org.junit.Assert.assertEquals; + +import java.io.Serializable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.alfresco.model.ContentModel; +import org.alfresco.service.namespace.QName; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Some tests for {@link PropertyMap} + * + * @author Neil Mc Erlean + * @since Odin + */ +public class PropertyMapTest +{ + private static Map beforeTestMap, afterTestMap; + + @BeforeClass public static void initTestMaps() + { + beforeTestMap = new HashMap(); + afterTestMap = new HashMap(); + + beforeTestMap.put(ContentModel.PROP_NAME, "willBeChanged"); + beforeTestMap.put(ContentModel.PROP_ADDRESSEE, "willBeNulled"); + beforeTestMap.put(ContentModel.PROP_TITLE, "willBeRemoved"); + beforeTestMap.put(ContentModel.PROP_COUNTER, "unchanging"); + + afterTestMap.put(ContentModel.PROP_NAME, "hasBeenChanged"); + afterTestMap.put(ContentModel.PROP_ADDRESSEE, null); + afterTestMap.put(ContentModel.PROP_DESCRIPTION, "hasBeenAdded"); + afterTestMap.put(ContentModel.PROP_COUNTER, "unchanging"); + } + + @Test public void propertiesAdded() throws Exception + { + Map expected = new HashMap(); + expected.put(ContentModel.PROP_DESCRIPTION, "hasBeenAdded"); + + assertEquals(expected, PropertyMap.getAddedProperties(beforeTestMap, afterTestMap)); + } + + @Test public void propertiesChanged() throws Exception + { + Map expected = new HashMap(); + expected.put(ContentModel.PROP_NAME, "hasBeenChanged"); + expected.put(ContentModel.PROP_ADDRESSEE, null); + + assertEquals(expected, PropertyMap.getChangedProperties(beforeTestMap, afterTestMap)); + } + + @Test public void propertiesRemoved() throws Exception + { + Map expected = new HashMap(); + expected.put(ContentModel.PROP_TITLE, "willBeRemoved"); + + assertEquals(expected, PropertyMap.getRemovedProperties(beforeTestMap, afterTestMap)); + } + + @Test public void propertiesUnchanged() throws Exception + { + Map expected = new HashMap(); + expected.put(ContentModel.PROP_COUNTER, "unchanging"); + + assertEquals(expected, PropertyMap.getUnchangedProperties(beforeTestMap, afterTestMap)); + } + + @Test public void nullMapsShouldntCauseExceptions() throws Exception + { + assertEquals(Collections.emptyMap(), PropertyMap.getAddedProperties(null, null)); + assertEquals(Collections.emptyMap(), PropertyMap.getRemovedProperties(null, null)); + assertEquals(Collections.emptyMap(), PropertyMap.getChangedProperties(null, null)); + assertEquals(Collections.emptyMap(), PropertyMap.getUnchangedProperties(null, null)); + } +}