001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: root $
013: * $Revision: 8604 $
014: * $Date: 2007-09-28 15:16:38 +0200 (Fre, 28 Sep 2007) $
015: */
016:
017: package helma.util;
018:
019: import java.util.Map;
020: import java.util.Set;
021: import java.util.Collection;
022:
023: /**
024: * A Map that wraps another map. We use this class to be able to
025: * wrap maps as native objects within a scripting engine rather
026: * than exposing them through Java reflection.
027: * Additionally, instances of this class can be set to readonly
028: * so that the original map can't be modified.
029: */
030: public class WrappedMap implements Map {
031:
032: // the wrapped map
033: protected Map wrapped = null;
034:
035: // is this map readonly?
036: protected boolean readonly = false;
037:
038: /**
039: * Constructor
040: */
041: public WrappedMap(Map map) {
042: this (map, false);
043: }
044:
045: /**
046: * Constructor
047: */
048: public WrappedMap(Map map, boolean readonly) {
049: if (map == null) {
050: throw new NullPointerException(
051: "null Map passed to WrappedMap constructor");
052: }
053: wrapped = map;
054: this .readonly = readonly;
055: }
056:
057: /**
058: * Set the readonly flag on or off
059: */
060: public void setReadonly(boolean readonly) {
061: this .readonly = readonly;
062: }
063:
064: /**
065: * Is this map readonly?
066: */
067: public boolean isReadonly() {
068: return readonly;
069: }
070:
071: // Methods from interface java.util.Map -
072: // these are just proxies to the wrapped map, except for
073: // readonly checks on modifiers.
074:
075: public int size() {
076: return wrapped.size();
077: }
078:
079: public boolean isEmpty() {
080: return wrapped.isEmpty();
081: }
082:
083: public boolean containsKey(Object key) {
084: return wrapped.containsKey(key);
085: }
086:
087: public boolean containsValue(Object value) {
088: return wrapped.containsValue(value);
089: }
090:
091: public Object get(Object key) {
092: return wrapped.get(key);
093: }
094:
095: // Modification Operations - check for readonly
096:
097: public Object put(Object key, Object value) {
098: if (readonly) {
099: throw new RuntimeException("Attempt to modify readonly map");
100: }
101: return wrapped.put(key, value);
102: }
103:
104: public Object remove(Object key) {
105: if (readonly) {
106: throw new RuntimeException("Attempt to modify readonly map");
107: }
108: return wrapped.remove(key);
109: }
110:
111: public void putAll(Map t) {
112: if (readonly) {
113: throw new RuntimeException("Attempt to modify readonly map");
114: }
115: wrapped.putAll(t);
116: }
117:
118: public void clear() {
119: if (readonly) {
120: throw new RuntimeException("Attempt to modify readonly map");
121: }
122: wrapped.clear();
123: }
124:
125: // Views
126:
127: public Set keySet() {
128: return wrapped.keySet();
129: }
130:
131: public Collection values() {
132: return wrapped.values();
133: }
134:
135: public Set entrySet() {
136: return wrapped.entrySet();
137: }
138:
139: // Comparison and hashing
140:
141: public boolean equals(Object o) {
142: return wrapped.equals(o);
143: }
144:
145: public int hashCode() {
146: return wrapped.hashCode();
147: }
148:
149: // toString
150:
151: public String toString() {
152: return wrapped.toString();
153: }
154:
155: }
|