New Transformed*Map factory decorateTransform() that transforms any existing entries in the map
rfe 30959 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@219347 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b915fdc02a
commit
9f53a81368
|
@ -68,6 +68,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
|
|||
<li>CollectionUtils/ListUtils - retainAll/removeAll that don't change original colllection</li>
|
||||
<li>BlockingBuffer - now includes stack trace if InterupttedException occurs [33700]</li>
|
||||
<li>BlockingBuffer - new methods that allow get and remove with a timeout [27691]</li>
|
||||
<li>Transformed*Map - new factory decorateTransform() that transforms any existing entries in the map [30959]</li>
|
||||
</ul>
|
||||
|
||||
<center><h3>BUG FIXES</h3></center>
|
||||
|
@ -98,6 +99,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
|
|||
<center><h3>JAVADOC</h3></center>
|
||||
<ul>
|
||||
<li>MapUtils.safeAddToMap - Better comment</li>
|
||||
<li>MapUtils.transformed*Map - Better comment</li>
|
||||
<li>ListOrderedSet.decorate(List) - Better comment [32073]</li>
|
||||
</ul>
|
||||
</body>
|
||||
|
|
|
@ -1264,11 +1264,19 @@ public class MapUtils {
|
|||
/**
|
||||
* Returns a transformed map backed by the given map.
|
||||
* <p>
|
||||
* This method returns a new map (decorating the specified map) that
|
||||
* will transform any new entries added to it.
|
||||
* Existing entries in the specified map will not be transformed.
|
||||
* If you want that behaviour, see {@link TransformedMap#decorateTransform}.
|
||||
* <p>
|
||||
* Each object is passed through the transformers as it is added to the
|
||||
* Map. It is important not to use the original map after invoking this
|
||||
* method, as it is a backdoor for adding untransformed objects.
|
||||
* <p>
|
||||
* If there are any elements already in the map being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param map the map to transform, must not be null
|
||||
* @param map the map to transform, must not be null, typically empty
|
||||
* @param keyTransformer the transformer for the map keys, null means no transformation
|
||||
* @param valueTransformer the transformer for the map values, null means no transformation
|
||||
* @return a transformed map backed by the given map
|
||||
|
@ -1499,11 +1507,19 @@ public class MapUtils {
|
|||
/**
|
||||
* Returns a transformed sorted map backed by the given map.
|
||||
* <p>
|
||||
* This method returns a new sorted map (decorating the specified map) that
|
||||
* will transform any new entries added to it.
|
||||
* Existing entries in the specified map will not be transformed.
|
||||
* If you want that behaviour, see {@link TransformedSortedMap#decorateTransform}.
|
||||
* <p>
|
||||
* Each object is passed through the transformers as it is added to the
|
||||
* Map. It is important not to use the original map after invoking this
|
||||
* method, as it is a backdoor for adding untransformed objects.
|
||||
* <p>
|
||||
* If there are any elements already in the map being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param map the map to transform, must not be null
|
||||
* @param map the map to transform, must not be null, typically empty
|
||||
* @param keyTransformer the transformer for the map keys, null means no transformation
|
||||
* @param valueTransformer the transformer for the map values, null means no transformation
|
||||
* @return a transformed map backed by the given map
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2003-2004 The Apache Software Foundation
|
||||
* Copyright 2003-2005 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -56,16 +56,40 @@ public class TransformedMap
|
|||
* <p>
|
||||
* If there are any elements already in the map being decorated, they
|
||||
* are NOT transformed.
|
||||
* Constrast this with {@link #decorateTransform}.
|
||||
*
|
||||
* @param map the map to decorate, must not be null
|
||||
* @param keyTransformer the transformer to use for key conversion, null means no conversion
|
||||
* @param valueTransformer the transformer to use for value conversion, null means no conversion
|
||||
* @param keyTransformer the transformer to use for key conversion, null means no transformation
|
||||
* @param valueTransformer the transformer to use for value conversion, null means no transformation
|
||||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
public static Map decorate(Map map, Transformer keyTransformer, Transformer valueTransformer) {
|
||||
return new TransformedMap(map, keyTransformer, valueTransformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming map that will transform
|
||||
* existing contents of the specified map.
|
||||
* <p>
|
||||
* If there are any elements already in the map being decorated, they
|
||||
* will be transformed by this method.
|
||||
* Constrast this with {@link #decorate}.
|
||||
*
|
||||
* @param map the map to decorate, must not be null
|
||||
* @param keyTransformer the transformer to use for key conversion, null means no transformation
|
||||
* @param valueTransformer the transformer to use for value conversion, null means no transformation
|
||||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
public static Map decorateTransform(Map map, Transformer keyTransformer, Transformer valueTransformer) {
|
||||
TransformedMap decorated = new TransformedMap(map, keyTransformer, valueTransformer);
|
||||
if (map.size() > 0) {
|
||||
Map transformed = decorated.transformMap(map);
|
||||
decorated.clear();
|
||||
decorated.getMap().putAll(transformed); // avoids double transformation
|
||||
}
|
||||
return decorated;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2003-2004 The Apache Software Foundation
|
||||
* Copyright 2003-2005 The Apache Software Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,6 +16,7 @@
|
|||
package org.apache.commons.collections.map;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
|
@ -47,6 +48,7 @@ public class TransformedSortedMap
|
|||
* <p>
|
||||
* If there are any elements already in the map being decorated, they
|
||||
* are NOT transformed.
|
||||
* Constrast this with {@link #decorateTransform}.
|
||||
*
|
||||
* @param map the map to decorate, must not be null
|
||||
* @param keyTransformer the predicate to validate the keys, null means no transformation
|
||||
|
@ -57,6 +59,29 @@ public class TransformedSortedMap
|
|||
return new TransformedSortedMap(map, keyTransformer, valueTransformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming sorted map that will transform
|
||||
* existing contents of the specified map.
|
||||
* <p>
|
||||
* If there are any elements already in the map being decorated, they
|
||||
* will be transformed by this method.
|
||||
* Constrast this with {@link #decorate}.
|
||||
*
|
||||
* @param map the map to decorate, must not be null
|
||||
* @param keyTransformer the transformer to use for key conversion, null means no transformation
|
||||
* @param valueTransformer the transformer to use for value conversion, null means no transformation
|
||||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
public static SortedMap decorateTransform(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) {
|
||||
TransformedSortedMap decorated = new TransformedSortedMap(map, keyTransformer, valueTransformer);
|
||||
if (map.size() > 0) {
|
||||
Map transformed = decorated.transformMap(map);
|
||||
decorated.clear();
|
||||
decorated.getMap().putAll(transformed); // avoids double transformation
|
||||
}
|
||||
return decorated;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
|
|
|
@ -97,6 +97,38 @@ public class TestTransformedMap extends AbstractTestMap {
|
|||
assertEquals(new Integer(88), map.get(entry.getKey()));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testFactory_Decorate() {
|
||||
Map base = new HashMap();
|
||||
base.put("A", "1");
|
||||
base.put("B", "2");
|
||||
base.put("C", "3");
|
||||
|
||||
Map trans = TransformedMap.decorate(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(3, trans.size());
|
||||
assertEquals("1", trans.get("A"));
|
||||
assertEquals("2", trans.get("B"));
|
||||
assertEquals("3", trans.get("C"));
|
||||
trans.put("D", "4");
|
||||
assertEquals(new Integer(4), trans.get("D"));
|
||||
}
|
||||
|
||||
public void testFactory_decorateTransform() {
|
||||
Map base = new HashMap();
|
||||
base.put("A", "1");
|
||||
base.put("B", "2");
|
||||
base.put("C", "3");
|
||||
|
||||
Map trans = TransformedMap.decorateTransform(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(3, trans.size());
|
||||
assertEquals(new Integer(1), trans.get("A"));
|
||||
assertEquals(new Integer(2), trans.get("B"));
|
||||
assertEquals(new Integer(3), trans.get("C"));
|
||||
trans.put("D", "4");
|
||||
assertEquals(new Integer(4), trans.get("D"));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public String getCompatibilityVersion() {
|
||||
return "3.1";
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ package org.apache.commons.collections.map;
|
|||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
@ -108,6 +109,38 @@ public class TestTransformedSortedMap extends AbstractTestSortedMap {
|
|||
assertEquals(new Integer(88), map.get(entry.getKey()));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testFactory_Decorate() {
|
||||
SortedMap base = new TreeMap();
|
||||
base.put("A", "1");
|
||||
base.put("B", "2");
|
||||
base.put("C", "3");
|
||||
|
||||
SortedMap trans = TransformedSortedMap.decorate(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(3, trans.size());
|
||||
assertEquals("1", trans.get("A"));
|
||||
assertEquals("2", trans.get("B"));
|
||||
assertEquals("3", trans.get("C"));
|
||||
trans.put("D", "4");
|
||||
assertEquals(new Integer(4), trans.get("D"));
|
||||
}
|
||||
|
||||
public void testFactory_decorateTransform() {
|
||||
SortedMap base = new TreeMap();
|
||||
base.put("A", "1");
|
||||
base.put("B", "2");
|
||||
base.put("C", "3");
|
||||
|
||||
SortedMap trans = TransformedSortedMap.decorateTransform(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(3, trans.size());
|
||||
assertEquals(new Integer(1), trans.get("A"));
|
||||
assertEquals(new Integer(2), trans.get("B"));
|
||||
assertEquals(new Integer(3), trans.get("C"));
|
||||
trans.put("D", "4");
|
||||
assertEquals(new Integer(4), trans.get("D"));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public String getCompatibilityVersion() {
|
||||
return "3.1";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue