Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/script/SimpleBindings.java
38829 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.script;2627import java.util.Map;28import java.util.HashMap;29import java.util.Collection;30import java.util.Set;3132/**33* A simple implementation of Bindings backed by34* a <code>HashMap</code> or some other specified <code>Map</code>.35*36* @author Mike Grogan37* @since 1.638*/39public class SimpleBindings implements Bindings {4041/**42* The <code>Map</code> field stores the attributes.43*/44private Map<String,Object> map;4546/**47* Constructor uses an existing <code>Map</code> to store the values.48* @param m The <code>Map</code> backing this <code>SimpleBindings</code>.49* @throws NullPointerException if m is null50*/51public SimpleBindings(Map<String,Object> m) {52if (m == null) {53throw new NullPointerException();54}55this.map = m;56}5758/**59* Default constructor uses a <code>HashMap</code>.60*/61public SimpleBindings() {62this(new HashMap<String,Object>());63}6465/**66* Sets the specified key/value in the underlying <code>map</code> field.67*68* @param name Name of value69* @param value Value to set.70*71* @return Previous value for the specified key. Returns null if key was previously72* unset.73*74* @throws NullPointerException if the name is null.75* @throws IllegalArgumentException if the name is empty.76*/77public Object put(String name, Object value) {78checkKey(name);79return map.put(name,value);80}8182/**83* <code>putAll</code> is implemented using <code>Map.putAll</code>.84*85* @param toMerge The <code>Map</code> of values to add.86*87* @throws NullPointerException88* if toMerge map is null or if some key in the map is null.89* @throws IllegalArgumentException90* if some key in the map is an empty String.91*/92public void putAll(Map<? extends String, ? extends Object> toMerge) {93if (toMerge == null) {94throw new NullPointerException("toMerge map is null");95}96for (Map.Entry<? extends String, ? extends Object> entry : toMerge.entrySet()) {97String key = entry.getKey();98checkKey(key);99put(key, entry.getValue());100}101}102103/** {@inheritDoc} */104public void clear() {105map.clear();106}107108/**109* Returns <tt>true</tt> if this map contains a mapping for the specified110* key. More formally, returns <tt>true</tt> if and only if111* this map contains a mapping for a key <tt>k</tt> such that112* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be113* at most one such mapping.)114*115* @param key key whose presence in this map is to be tested.116* @return <tt>true</tt> if this map contains a mapping for the specified117* key.118*119* @throws NullPointerException if key is null120* @throws ClassCastException if key is not String121* @throws IllegalArgumentException if key is empty String122*/123public boolean containsKey(Object key) {124checkKey(key);125return map.containsKey(key);126}127128/** {@inheritDoc} */129public boolean containsValue(Object value) {130return map.containsValue(value);131}132133/** {@inheritDoc} */134public Set<Map.Entry<String, Object>> entrySet() {135return map.entrySet();136}137138/**139* Returns the value to which this map maps the specified key. Returns140* <tt>null</tt> if the map contains no mapping for this key. A return141* value of <tt>null</tt> does not <i>necessarily</i> indicate that the142* map contains no mapping for the key; it's also possible that the map143* explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>144* operation may be used to distinguish these two cases.145*146* <p>More formally, if this map contains a mapping from a key147* <tt>k</tt> to a value <tt>v</tt> such that <tt>(key==null ? k==null :148* key.equals(k))</tt>, then this method returns <tt>v</tt>; otherwise149* it returns <tt>null</tt>. (There can be at most one such mapping.)150*151* @param key key whose associated value is to be returned.152* @return the value to which this map maps the specified key, or153* <tt>null</tt> if the map contains no mapping for this key.154*155* @throws NullPointerException if key is null156* @throws ClassCastException if key is not String157* @throws IllegalArgumentException if key is empty String158*/159public Object get(Object key) {160checkKey(key);161return map.get(key);162}163164/** {@inheritDoc} */165public boolean isEmpty() {166return map.isEmpty();167}168169/** {@inheritDoc} */170public Set<String> keySet() {171return map.keySet();172}173174/**175* Removes the mapping for this key from this map if it is present176* (optional operation). More formally, if this map contains a mapping177* from key <tt>k</tt> to value <tt>v</tt> such that178* <code>(key==null ? k==null : key.equals(k))</code>, that mapping179* is removed. (The map can contain at most one such mapping.)180*181* <p>Returns the value to which the map previously associated the key, or182* <tt>null</tt> if the map contained no mapping for this key. (A183* <tt>null</tt> return can also indicate that the map previously184* associated <tt>null</tt> with the specified key if the implementation185* supports <tt>null</tt> values.) The map will not contain a mapping for186* the specified key once the call returns.187*188* @param key key whose mapping is to be removed from the map.189* @return previous value associated with specified key, or <tt>null</tt>190* if there was no mapping for key.191*192* @throws NullPointerException if key is null193* @throws ClassCastException if key is not String194* @throws IllegalArgumentException if key is empty String195*/196public Object remove(Object key) {197checkKey(key);198return map.remove(key);199}200201/** {@inheritDoc} */202public int size() {203return map.size();204}205206/** {@inheritDoc} */207public Collection<Object> values() {208return map.values();209}210211private void checkKey(Object key) {212if (key == null) {213throw new NullPointerException("key can not be null");214}215if (!(key instanceof String)) {216throw new ClassCastException("key should be a String");217}218if (key.equals("")) {219throw new IllegalArgumentException("key can not be empty");220}221}222}223224225