mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
Utility methods to get details of property changes (map comparison).
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@34824 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -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<QName, Serializable>
|
||||
// Done
|
||||
return new Pair<Map<QName, Serializable>, Map<QName, Serializable>>(beforeDelta, afterDelta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to get properties which were added as part of a change.
|
||||
*
|
||||
* @param before the properties before (may be <code>null</code>).
|
||||
* @param after the properties after (may be <code>null</code>).
|
||||
* @return a map of values that were added along with their new values.
|
||||
*
|
||||
* @since Odin
|
||||
*/
|
||||
public static Map<QName, Serializable> getAddedProperties(Map<QName, Serializable> before, Map<QName, Serializable> after)
|
||||
{
|
||||
Map<QName, Serializable> result = new HashMap<QName, Serializable>();
|
||||
|
||||
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 <code>null</code>).
|
||||
* @param after the properties after (may be <code>null</code>).
|
||||
* @return a map of values that were removed along with their old values.
|
||||
*
|
||||
* @since Odin
|
||||
*/
|
||||
public static Map<QName, Serializable> getRemovedProperties(Map<QName, Serializable> before, Map<QName, Serializable> after)
|
||||
{
|
||||
Map<QName, Serializable> result = new HashMap<QName, Serializable>();
|
||||
|
||||
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 <code>null</code>).
|
||||
* @param after the properties after (may be <code>null</code>).
|
||||
* @return a map of values that were changed along with their new values.
|
||||
*
|
||||
* @since Odin
|
||||
*/
|
||||
public static Map<QName, Serializable> getChangedProperties(Map<QName, Serializable> before, Map<QName, Serializable> after)
|
||||
{
|
||||
Map<QName, Serializable> result = new HashMap<QName, Serializable>();
|
||||
|
||||
if (before != null && after != null)
|
||||
{
|
||||
Map<QName, Serializable> intersection = new HashMap<QName, Serializable>();
|
||||
|
||||
intersection.putAll(after);
|
||||
intersection.keySet().retainAll(before.keySet());
|
||||
|
||||
for (Entry<QName, Serializable> 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 <code>null</code>).
|
||||
* @param after the properties after (may be <code>null</code>).
|
||||
* @return a map of values that were unchanged along with their values.
|
||||
*
|
||||
* @since Odin
|
||||
*/
|
||||
public static Map<QName, Serializable> getUnchangedProperties(Map<QName, Serializable> before, Map<QName, Serializable> after)
|
||||
{
|
||||
Map<QName, Serializable> result = new HashMap<QName, Serializable>();
|
||||
|
||||
if (before != null && after != null)
|
||||
{
|
||||
Map<QName, Serializable> intersection = new HashMap<QName, Serializable>();
|
||||
|
||||
intersection.putAll(before);
|
||||
intersection.keySet().retainAll(after.keySet());
|
||||
|
||||
for (Entry<QName, Serializable> entry : intersection.entrySet())
|
||||
{
|
||||
if ( EqualsHelper.nullSafeEquals(before.get(entry.getKey()), after.get(entry.getKey())))
|
||||
{
|
||||
result.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
99
source/java/org/alfresco/util/PropertyMapTest.java
Normal file
99
source/java/org/alfresco/util/PropertyMapTest.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<QName, Serializable> beforeTestMap, afterTestMap;
|
||||
|
||||
@BeforeClass public static void initTestMaps()
|
||||
{
|
||||
beforeTestMap = new HashMap<QName, Serializable>();
|
||||
afterTestMap = new HashMap<QName, Serializable>();
|
||||
|
||||
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<QName, Serializable> expected = new HashMap<QName, Serializable>();
|
||||
expected.put(ContentModel.PROP_DESCRIPTION, "hasBeenAdded");
|
||||
|
||||
assertEquals(expected, PropertyMap.getAddedProperties(beforeTestMap, afterTestMap));
|
||||
}
|
||||
|
||||
@Test public void propertiesChanged() throws Exception
|
||||
{
|
||||
Map<QName, Serializable> expected = new HashMap<QName, Serializable>();
|
||||
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<QName, Serializable> expected = new HashMap<QName, Serializable>();
|
||||
expected.put(ContentModel.PROP_TITLE, "willBeRemoved");
|
||||
|
||||
assertEquals(expected, PropertyMap.getRemovedProperties(beforeTestMap, afterTestMap));
|
||||
}
|
||||
|
||||
@Test public void propertiesUnchanged() throws Exception
|
||||
{
|
||||
Map<QName, Serializable> expected = new HashMap<QName, Serializable>();
|
||||
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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user