KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > script > SimpleBindings


1 /*
2  * @(#)SimpleBindings.java 1.4 05/11/17 14:24:14
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
6  */

7
8 package javax.script;
9
10 import java.util.Map JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Set JavaDoc;
14
15 /**
16  * A simple implementation of Bindings backed by
17  * a <code>HashMap</code> or some other specified <code>Map</code>.
18  *
19  * @author Mike Grogan
20  * @version 1.0
21  * @since 1.6
22  */

23 public class SimpleBindings implements Bindings {
24
25     /**
26      * The <code>Map</code> field stores the attributes.
27      */

28     private Map JavaDoc<String JavaDoc,Object JavaDoc> map;
29     
30     /**
31      * Constructor uses an existing <code>Map</code> to store the values.
32      * @param m The <code>Map</code> backing this <code>SimpleBindings</code>.
33      * @throws NullPointerException if m is null
34      */

35     public SimpleBindings(Map JavaDoc<String JavaDoc,Object JavaDoc> m) {
36         if (m == null) {
37             throw new NullPointerException JavaDoc();
38         }
39         this.map = m;
40     }
41     
42     /**
43      * Default constructor uses a <code>HashMap</code>.
44      */

45     public SimpleBindings() {
46         this(new HashMap JavaDoc<String JavaDoc,Object JavaDoc>());
47     }
48
49     /**
50      * Sets the specified key/value in the underlying <code>map</code> field.
51      *
52      * @param name Name of value
53      * @param value Value to set.
54      *
55      * @return Previous value for the specified key. Returns null if key was previously
56      * unset.
57      *
58      * @throws NullPointerException if the name is null.
59      * @throws IllegalArgumentException if the name is empty.
60      */

61     public Object JavaDoc put(String JavaDoc name, Object JavaDoc value) {
62         checkKey(name);
63         return map.put(name,value);
64     }
65     
66     /**
67      * <code>putAll</code> is implemented using <code>Map.putAll</code>.
68      *
69      * @param toMerge The <code>Map</code> of values to add.
70      *
71      * @throws NullPointerException
72      * if toMerge map is null or if some key in the map is null.
73      * @throws IllegalArgumentException
74      * if some key in the map is an empty String.
75      */

76     public void putAll(Map JavaDoc<? extends String JavaDoc, ? extends Object JavaDoc> toMerge) {
77         if (toMerge == null) {
78             throw new NullPointerException JavaDoc("toMerge map is null");
79         }
80         for (Map.Entry JavaDoc<? extends String JavaDoc, ? extends Object JavaDoc> entry : toMerge.entrySet()) {
81             String JavaDoc key = entry.getKey();
82             checkKey(key);
83             put(key, entry.getValue());
84         }
85     }
86     
87     /** {@inheritDoc} */
88     public void clear() {
89         map.clear();
90     }
91     
92     /**
93      * Returns <tt>true</tt> if this map contains a mapping for the specified
94      * key. More formally, returns <tt>true</tt> if and only if
95      * this map contains a mapping for a key <tt>k</tt> such that
96      * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
97      * at most one such mapping.)
98      *
99      * @param key key whose presence in this map is to be tested.
100      * @return <tt>true</tt> if this map contains a mapping for the specified
101      * key.
102      *
103      * @throws NullPointerException if key is null
104      * @throws ClassCastException if key is not String
105      * @throws IllegalArgumentException if key is empty String
106      */

107     public boolean containsKey(Object JavaDoc key) {
108         checkKey(key);
109         return map.containsKey(key);
110     }
111     
112     /** {@inheritDoc} */
113     public boolean containsValue(Object JavaDoc value) {
114         return map.containsValue(value);
115     }
116     
117     /** {@inheritDoc} */
118     public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc, Object JavaDoc>> entrySet() {
119         return map.entrySet();
120     }
121     
122     /**
123      * Returns the value to which this map maps the specified key. Returns
124      * <tt>null</tt> if the map contains no mapping for this key. A return
125      * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
126      * map contains no mapping for the key; it's also possible that the map
127      * explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>
128      * operation may be used to distinguish these two cases.
129      *
130      * <p>More formally, if this map contains a mapping from a key
131      * <tt>k</tt> to a value <tt>v</tt> such that <tt>(key==null ? k==null :
132      * key.equals(k))</tt>, then this method returns <tt>v</tt>; otherwise
133      * it returns <tt>null</tt>. (There can be at most one such mapping.)
134      *
135      * @param key key whose associated value is to be returned.
136      * @return the value to which this map maps the specified key, or
137      * <tt>null</tt> if the map contains no mapping for this key.
138      *
139      * @throws NullPointerException if key is null
140      * @throws ClassCastException if key is not String
141      * @throws IllegalArgumentException if key is empty String
142      */

143     public Object JavaDoc get(Object JavaDoc key) {
144         checkKey(key);
145         return map.get(key);
146     }
147     
148     /** {@inheritDoc} */
149     public boolean isEmpty() {
150         return map.isEmpty();
151     }
152     
153     /** {@inheritDoc} */
154     public Set JavaDoc<String JavaDoc> keySet() {
155         return map.keySet();
156     }
157  
158     /**
159      * Removes the mapping for this key from this map if it is present
160      * (optional operation). More formally, if this map contains a mapping
161      * from key <tt>k</tt> to value <tt>v</tt> such that
162      * <code>(key==null ? k==null : key.equals(k))</code>, that mapping
163      * is removed. (The map can contain at most one such mapping.)
164      *
165      * <p>Returns the value to which the map previously associated the key, or
166      * <tt>null</tt> if the map contained no mapping for this key. (A
167      * <tt>null</tt> return can also indicate that the map previously
168      * associated <tt>null</tt> with the specified key if the implementation
169      * supports <tt>null</tt> values.) The map will not contain a mapping for
170      * the specified key once the call returns.
171      *
172      * @param key key whose mapping is to be removed from the map.
173      * @return previous value associated with specified key, or <tt>null</tt>
174      * if there was no mapping for key.
175      *
176      * @throws NullPointerException if key is null
177      * @throws ClassCastException if key is not String
178      * @throws IllegalArgumentException if key is empty String
179      */

180     public Object JavaDoc remove(Object JavaDoc key) {
181         checkKey(key);
182         return map.remove(key);
183     }
184     
185     /** {@inheritDoc} */
186     public int size() {
187         return map.size();
188     }
189     
190     /** {@inheritDoc} */
191     public Collection JavaDoc<Object JavaDoc> values() {
192         return map.values();
193     }
194
195     private void checkKey(Object JavaDoc key) {
196         if (key == null) {
197             throw new NullPointerException JavaDoc("key can not be null");
198         }
199         if (!(key instanceof String JavaDoc)) {
200             throw new ClassCastException JavaDoc("key should be a String");
201         }
202         if (key.equals("")) {
203             throw new IllegalArgumentException JavaDoc("key can not be empty");
204         }
205     }
206 }
207
Popular Tags