001: /*
002: * %W% %E% %U%
003: *
004: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
005: * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
006: */
007:
008: package javax.script;
009:
010: import java.util.Map;
011: import java.util.HashMap;
012: import java.util.Collection;
013: import java.util.Set;
014:
015: /**
016: * A simple implementation of Bindings backed by
017: * a <code>HashMap</code> or some other specified <code>Map</code>.
018: *
019: * @author Mike Grogan
020: * @version 1.0
021: * @since 1.6
022: */
023: public class SimpleBindings implements Bindings {
024:
025: /**
026: * The <code>Map</code> field stores the attributes.
027: */
028: private Map<String, Object> map;
029:
030: /**
031: * Constructor uses an existing <code>Map</code> to store the values.
032: * @param m The <code>Map</code> backing this <code>SimpleBindings</code>.
033: * @throws NullPointerException if m is null
034: */
035: public SimpleBindings(Map<String, Object> m) {
036: if (m == null) {
037: throw new NullPointerException();
038: }
039: this .map = m;
040: }
041:
042: /**
043: * Default constructor uses a <code>HashMap</code>.
044: */
045: public SimpleBindings() {
046: this (new HashMap<String, Object>());
047: }
048:
049: /**
050: * Sets the specified key/value in the underlying <code>map</code> field.
051: *
052: * @param name Name of value
053: * @param value Value to set.
054: *
055: * @return Previous value for the specified key. Returns null if key was previously
056: * unset.
057: *
058: * @throws <code>NullPointerException</code> if the name is null.
059: * @throws <code>IllegalArgumentException</code> if the name is empty.
060: */
061: public Object put(String name, Object value) {
062: checkKey(name);
063: map.put(name, value);
064: return value;
065: }
066:
067: /**
068: * <code>putAll</code> is implemented using <code>Map.putAll</code>.
069: *
070: * @param toMerge The <code>Map</code> of values to add.
071: *
072: * @throws <code>NullPointerException</code> if some key in the specified <code>Map</code>
073: * is null.
074: * @throws <code>IllegalArgumentException</code> if some key in the specified <code>Map</code> is empty String.
075: */
076: public void putAll(Map<? extends String, ? extends Object> toMerge) {
077: for (Map.Entry<? extends String, ? extends Object> entry : toMerge
078: .entrySet()) {
079: String key = entry.getKey();
080: checkKey(key);
081: put(key, entry.getValue());
082: }
083: }
084:
085: /** {@inheritDoc} */
086: public void clear() {
087: map.clear();
088: }
089:
090: /** {@inheritDoc} */
091: public boolean containsKey(Object key) {
092: checkKey(key);
093: return map.containsKey(key);
094: }
095:
096: /** {@inheritDoc} */
097: public boolean containsValue(Object value) {
098: return map.containsValue(value);
099: }
100:
101: /** {@inheritDoc} */
102: public Set<Map.Entry<String, Object>> entrySet() {
103: return map.entrySet();
104: }
105:
106: /** {@inheritDoc} */
107: public Object get(Object key) {
108: checkKey(key);
109: return map.get(key);
110: }
111:
112: /** {@inheritDoc} */
113: public boolean isEmpty() {
114: return map.isEmpty();
115: }
116:
117: /** {@inheritDoc} */
118: public Set<String> keySet() {
119: return map.keySet();
120: }
121:
122: /** {@inheritDoc} */
123: public Object remove(Object key) {
124: checkKey(key);
125: return map.remove(key);
126: }
127:
128: /** {@inheritDoc} */
129: public int size() {
130: return map.size();
131: }
132:
133: /** {@inheritDoc} */
134: public Collection<Object> values() {
135: return map.values();
136: }
137:
138: private void checkKey(Object key) {
139: if (key == null) {
140: throw new NullPointerException("key can not be null");
141: }
142: if (!(key instanceof String)) {
143: throw new ClassCastException("key should be a String");
144: }
145: if (key.equals("")) {
146: throw new IllegalArgumentException("key can not be empty");
147: }
148: }
149: }
|