[CJ-dev] other-projects/marmalade/src/main/java/org/marmalade/util ScopedMap.java,1.1,1.2
Brought to you by:
johnqueso
From: John C. <joh...@co...> - 2004-03-27 04:35:39
|
Update of /cvsroot/commonjava/other-projects/marmalade/src/main/java/org/marmalade/util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7841/src/main/java/org/marmalade/util Modified Files: ScopedMap.java Log Message: added test for ScopedMap (it works!) and added OGNL implementation of ExpressionEvaluator (not tested yet). Index: ScopedMap.java =================================================================== RCS file: /cvsroot/commonjava/other-projects/marmalade/src/main/java/org/marmalade/util/ScopedMap.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- ScopedMap.java 26 Mar 2004 05:13:35 -0000 1.1 +++ ScopedMap.java 27 Mar 2004 04:24:33 -0000 1.2 @@ -4,6 +4,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -16,7 +17,6 @@ private Map superMap; private Map thisMap; - private int superMapSize; private ScopedMapEntriesSet keySet; private ScopedMapEntriesSet valueSet; private ScopedMapEntriesSet entrySet; @@ -32,10 +32,9 @@ private void init(Map superMap){ Map m = superMap; if(m == null){m = Collections.EMPTY_MAP;} - this.superMap = Collections.unmodifiableMap(m); + this.superMap = new HashMap(m); - this.superMapSize = superMap.size(); - this.thisMap = new HashMap(superMap); + this.thisMap = new HashMap(); this.entrySet = new ScopedMapEntriesSet(superMap, thisMap, null); this.keySet = new ScopedMapEntriesSet(superMap, thisMap, Boolean.TRUE); @@ -49,7 +48,7 @@ } public int size() { - return superMapSize + thisMap.size(); + return superMap.size() + thisMap.size(); } public void clear() { @@ -58,15 +57,15 @@ } public boolean isEmpty() { - return superMapSize < 1 || thisMap.isEmpty(); + return superMap.isEmpty() && thisMap.isEmpty(); } public boolean containsKey(Object key) { - return thisMap.containsKey(key) || (superMap != null && superMap.containsKey(key)); + return thisMap.containsKey(key) || superMap.containsKey(key); } public boolean containsValue(Object value) { - return thisMap.containsValue(value) || (superMap != null && superMap.containsValue(value)); + return thisMap.containsValue(value) || superMap.containsValue(value); } public Collection values() { @@ -74,8 +73,16 @@ } public void putAll(Map t) { - thisMap.putAll(t); - update(); + boolean changed = false; + for (Iterator it = t.keySet().iterator(); it.hasNext();) { + Object key = it.next(); + if(!superMap.containsKey(key)){ + thisMap.put(key, t.get(key)); + changed = true; + } + } + + if(changed){update();} } public Set entrySet() { @@ -89,7 +96,7 @@ public Object get(Object key) { Object result = thisMap.get(key); if(result == null){ - superMap.get(key); + result = superMap.get(key); } return result; @@ -104,8 +111,11 @@ } public Object put(Object key, Object value) { - Object result = thisMap.put(key, value); - update(); + Object result = null; + if(!superMap.containsKey(key)){ + result = thisMap.put(key, value); + update(); + } return result; } |