Implement copy-on-write of the wrapped map.
This commit is contained in:
parent
fad3f06b5c
commit
4a72e955ac
1 changed files with 49 additions and 0 deletions
|
@ -19,6 +19,7 @@ package helma.util;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Map that wraps another map and can be set to read-only.
|
* A Map that wraps another map and can be set to read-only.
|
||||||
|
@ -31,6 +32,12 @@ public class WrappedMap implements Map {
|
||||||
// is this map readonly?
|
// is this map readonly?
|
||||||
boolean readonly = false;
|
boolean readonly = false;
|
||||||
|
|
||||||
|
// isolate changes from the wrapped map?
|
||||||
|
boolean copyOnWrite = false;
|
||||||
|
|
||||||
|
public final static int READ_ONLY = 1;
|
||||||
|
public final static int COPY_ON_WRITE = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
|
@ -42,6 +49,18 @@ public class WrappedMap implements Map {
|
||||||
wrapped = map;
|
wrapped = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public WrappedMap(Map map, int mode) {
|
||||||
|
this(map);
|
||||||
|
if (mode == READ_ONLY) {
|
||||||
|
readonly = true;
|
||||||
|
} else if (mode == COPY_ON_WRITE) {
|
||||||
|
copyOnWrite = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the readonly flag on or off
|
* Set the readonly flag on or off
|
||||||
*/
|
*/
|
||||||
|
@ -56,6 +75,20 @@ public class WrappedMap implements Map {
|
||||||
return readonly;
|
return readonly;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the copyOnWrite flag on or off
|
||||||
|
*/
|
||||||
|
public void setCopyOnWrite(boolean cow) {
|
||||||
|
copyOnWrite = cow;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is this map copyOnWrite?
|
||||||
|
*/
|
||||||
|
public boolean isCopyOnWrite() {
|
||||||
|
return copyOnWrite;
|
||||||
|
}
|
||||||
|
|
||||||
// Methods from interface java.util.Map -
|
// Methods from interface java.util.Map -
|
||||||
// these are just proxies to the wrapped map, except for
|
// these are just proxies to the wrapped map, except for
|
||||||
// readonly checks on modifiers.
|
// readonly checks on modifiers.
|
||||||
|
@ -86,6 +119,10 @@ public class WrappedMap implements Map {
|
||||||
if (readonly) {
|
if (readonly) {
|
||||||
throw new RuntimeException("Attempt to modify readonly map");
|
throw new RuntimeException("Attempt to modify readonly map");
|
||||||
}
|
}
|
||||||
|
if (copyOnWrite) {
|
||||||
|
wrapped = new HashMap (wrapped);
|
||||||
|
copyOnWrite = false;
|
||||||
|
}
|
||||||
return wrapped.put(key, value);
|
return wrapped.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,6 +130,10 @@ public class WrappedMap implements Map {
|
||||||
if (readonly) {
|
if (readonly) {
|
||||||
throw new RuntimeException("Attempt to modify readonly map");
|
throw new RuntimeException("Attempt to modify readonly map");
|
||||||
}
|
}
|
||||||
|
if (copyOnWrite) {
|
||||||
|
wrapped = new HashMap (wrapped);
|
||||||
|
copyOnWrite = false;
|
||||||
|
}
|
||||||
return wrapped.remove(key);
|
return wrapped.remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +141,10 @@ public class WrappedMap implements Map {
|
||||||
if (readonly) {
|
if (readonly) {
|
||||||
throw new RuntimeException("Attempt to modify readonly map");
|
throw new RuntimeException("Attempt to modify readonly map");
|
||||||
}
|
}
|
||||||
|
if (copyOnWrite) {
|
||||||
|
wrapped = new HashMap (wrapped);
|
||||||
|
copyOnWrite = false;
|
||||||
|
}
|
||||||
wrapped.putAll(t);
|
wrapped.putAll(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +152,10 @@ public class WrappedMap implements Map {
|
||||||
if (readonly) {
|
if (readonly) {
|
||||||
throw new RuntimeException("Attempt to modify readonly map");
|
throw new RuntimeException("Attempt to modify readonly map");
|
||||||
}
|
}
|
||||||
|
if (copyOnWrite) {
|
||||||
|
wrapped = new HashMap (wrapped);
|
||||||
|
copyOnWrite = false;
|
||||||
|
}
|
||||||
wrapped.clear();
|
wrapped.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue