Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.scripting/share/classes/javax/script/SimpleBindings.java
41153 views
1
/*
2
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.script;
27
28
import java.util.Map;
29
import java.util.HashMap;
30
import java.util.Collection;
31
import java.util.Objects;
32
import java.util.Set;
33
34
/**
35
* A simple implementation of Bindings backed by
36
* a {@code HashMap} or some other specified {@code Map}.
37
*
38
* @author Mike Grogan
39
* @since 1.6
40
*/
41
public class SimpleBindings implements Bindings {
42
43
/**
44
* The {@code Map} field stores the attributes.
45
*/
46
private final Map<String,Object> map;
47
48
/**
49
* Constructor uses an existing {@code Map} to store the values.
50
* @param m The {@code Map} backing this {@code SimpleBindings}.
51
* @throws NullPointerException if m is null
52
*/
53
public SimpleBindings(Map<String,Object> m) {
54
this.map = Objects.requireNonNull(m);
55
}
56
57
/**
58
* Default constructor uses a {@code HashMap}.
59
*/
60
public SimpleBindings() {
61
this(new HashMap<>());
62
}
63
64
/**
65
* Sets the specified key/value in the underlying {@code map} field.
66
*
67
* @param name Name of value
68
* @param value Value to set.
69
*
70
* @return Previous value for the specified key. Returns null if key was previously
71
* unset.
72
*
73
* @throws NullPointerException if the name is null.
74
* @throws IllegalArgumentException if the name is empty.
75
*/
76
public Object put(String name, Object value) {
77
checkKey(name);
78
return map.put(name,value);
79
}
80
81
/**
82
* {@code putAll} is implemented using {@code Map.putAll}.
83
*
84
* @param toMerge The {@code Map} of values to add.
85
*
86
* @throws NullPointerException
87
* if toMerge map is null or if some key in the map is null.
88
* @throws IllegalArgumentException
89
* if some key in the map is an empty String.
90
*/
91
public void putAll(Map<? extends String, ? extends Object> toMerge) {
92
Objects.requireNonNull(toMerge, "toMerge map is null");
93
for (Map.Entry<? extends String, ? extends Object> entry : toMerge.entrySet()) {
94
String key = entry.getKey();
95
checkKey(key);
96
put(key, entry.getValue());
97
}
98
}
99
100
/** {@inheritDoc} */
101
public void clear() {
102
map.clear();
103
}
104
105
/**
106
* Returns {@code true} if this map contains a mapping for the specified
107
* key. More formally, returns {@code true} if and only if
108
* this map contains a mapping for a key {@code k} such that
109
* {@code (key==null ? k==null : key.equals(k))}. (There can be
110
* at most one such mapping.)
111
*
112
* @param key key whose presence in this map is to be tested.
113
* @return {@code true} if this map contains a mapping for the specified
114
* key.
115
*
116
* @throws NullPointerException if key is null
117
* @throws ClassCastException if key is not String
118
* @throws IllegalArgumentException if key is empty String
119
*/
120
public boolean containsKey(Object key) {
121
checkKey(key);
122
return map.containsKey(key);
123
}
124
125
/** {@inheritDoc} */
126
public boolean containsValue(Object value) {
127
return map.containsValue(value);
128
}
129
130
/** {@inheritDoc} */
131
public Set<Map.Entry<String, Object>> entrySet() {
132
return map.entrySet();
133
}
134
135
/**
136
* Returns the value to which this map maps the specified key. Returns
137
* {@code null} if the map contains no mapping for this key. A return
138
* value of {@code null} does not <i>necessarily</i> indicate that the
139
* map contains no mapping for the key; it's also possible that the map
140
* explicitly maps the key to {@code null}. The {@code containsKey}
141
* operation may be used to distinguish these two cases.
142
*
143
* <p>More formally, if this map contains a mapping from a key
144
* {@code k} to a value {@code v} such that
145
* {@code (key==null ? k==null : key.equals(k))},
146
* then this method returns {@code v}; otherwise
147
* it returns {@code null}. (There can be at most one such mapping.)
148
*
149
* @param key key whose associated value is to be returned.
150
* @return the value to which this map maps the specified key, or
151
* {@code null} if the map contains no mapping for this key.
152
*
153
* @throws NullPointerException if key is null
154
* @throws ClassCastException if key is not String
155
* @throws IllegalArgumentException if key is empty String
156
*/
157
public Object get(Object key) {
158
checkKey(key);
159
return map.get(key);
160
}
161
162
/** {@inheritDoc} */
163
public boolean isEmpty() {
164
return map.isEmpty();
165
}
166
167
/** {@inheritDoc} */
168
public Set<String> keySet() {
169
return map.keySet();
170
}
171
172
/**
173
* Removes the mapping for this key from this map if it is present
174
* (optional operation). More formally, if this map contains a mapping
175
* from key {@code k} to value {@code v} such that
176
* {@code (key==null ? k==null : key.equals(k))}, that mapping
177
* is removed. (The map can contain at most one such mapping.)
178
*
179
* <p>Returns the value to which the map previously associated the key, or
180
* {@code null} if the map contained no mapping for this key. (A
181
* {@code null} return can also indicate that the map previously
182
* associated {@code null} with the specified key if the implementation
183
* supports {@code null} values.) The map will not contain a mapping for
184
* the specified key once the call returns.
185
*
186
* @param key key whose mapping is to be removed from the map.
187
* @return previous value associated with specified key, or {@code null}
188
* if there was no mapping for key.
189
*
190
* @throws NullPointerException if key is null
191
* @throws ClassCastException if key is not String
192
* @throws IllegalArgumentException if key is empty String
193
*/
194
public Object remove(Object key) {
195
checkKey(key);
196
return map.remove(key);
197
}
198
199
/** {@inheritDoc} */
200
public int size() {
201
return map.size();
202
}
203
204
/** {@inheritDoc} */
205
public Collection<Object> values() {
206
return map.values();
207
}
208
209
private void checkKey(Object key) {
210
Objects.requireNonNull(key, "key can not be null");
211
if (!(key instanceof String)) {
212
throw new ClassCastException("key should be a String");
213
}
214
if (key.equals("")) {
215
throw new IllegalArgumentException("key can not be empty");
216
}
217
}
218
}
219
220