Path: blob/master/src/java.scripting/share/classes/javax/script/SimpleBindings.java
41153 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.Objects;31import java.util.Set;3233/**34* A simple implementation of Bindings backed by35* a {@code HashMap} or some other specified {@code Map}.36*37* @author Mike Grogan38* @since 1.639*/40public class SimpleBindings implements Bindings {4142/**43* The {@code Map} field stores the attributes.44*/45private final Map<String,Object> map;4647/**48* Constructor uses an existing {@code Map} to store the values.49* @param m The {@code Map} backing this {@code SimpleBindings}.50* @throws NullPointerException if m is null51*/52public SimpleBindings(Map<String,Object> m) {53this.map = Objects.requireNonNull(m);54}5556/**57* Default constructor uses a {@code HashMap}.58*/59public SimpleBindings() {60this(new HashMap<>());61}6263/**64* Sets the specified key/value in the underlying {@code map} field.65*66* @param name Name of value67* @param value Value to set.68*69* @return Previous value for the specified key. Returns null if key was previously70* unset.71*72* @throws NullPointerException if the name is null.73* @throws IllegalArgumentException if the name is empty.74*/75public Object put(String name, Object value) {76checkKey(name);77return map.put(name,value);78}7980/**81* {@code putAll} is implemented using {@code Map.putAll}.82*83* @param toMerge The {@code Map} of values to add.84*85* @throws NullPointerException86* if toMerge map is null or if some key in the map is null.87* @throws IllegalArgumentException88* if some key in the map is an empty String.89*/90public void putAll(Map<? extends String, ? extends Object> toMerge) {91Objects.requireNonNull(toMerge, "toMerge map is null");92for (Map.Entry<? extends String, ? extends Object> entry : toMerge.entrySet()) {93String key = entry.getKey();94checkKey(key);95put(key, entry.getValue());96}97}9899/** {@inheritDoc} */100public void clear() {101map.clear();102}103104/**105* Returns {@code true} if this map contains a mapping for the specified106* key. More formally, returns {@code true} if and only if107* this map contains a mapping for a key {@code k} such that108* {@code (key==null ? k==null : key.equals(k))}. (There can be109* at most one such mapping.)110*111* @param key key whose presence in this map is to be tested.112* @return {@code true} if this map contains a mapping for the specified113* key.114*115* @throws NullPointerException if key is null116* @throws ClassCastException if key is not String117* @throws IllegalArgumentException if key is empty String118*/119public boolean containsKey(Object key) {120checkKey(key);121return map.containsKey(key);122}123124/** {@inheritDoc} */125public boolean containsValue(Object value) {126return map.containsValue(value);127}128129/** {@inheritDoc} */130public Set<Map.Entry<String, Object>> entrySet() {131return map.entrySet();132}133134/**135* Returns the value to which this map maps the specified key. Returns136* {@code null} if the map contains no mapping for this key. A return137* value of {@code null} does not <i>necessarily</i> indicate that the138* map contains no mapping for the key; it's also possible that the map139* explicitly maps the key to {@code null}. The {@code containsKey}140* operation may be used to distinguish these two cases.141*142* <p>More formally, if this map contains a mapping from a key143* {@code k} to a value {@code v} such that144* {@code (key==null ? k==null : key.equals(k))},145* then this method returns {@code v}; otherwise146* it returns {@code null}. (There can be at most one such mapping.)147*148* @param key key whose associated value is to be returned.149* @return the value to which this map maps the specified key, or150* {@code null} if the map contains no mapping for this key.151*152* @throws NullPointerException if key is null153* @throws ClassCastException if key is not String154* @throws IllegalArgumentException if key is empty String155*/156public Object get(Object key) {157checkKey(key);158return map.get(key);159}160161/** {@inheritDoc} */162public boolean isEmpty() {163return map.isEmpty();164}165166/** {@inheritDoc} */167public Set<String> keySet() {168return map.keySet();169}170171/**172* Removes the mapping for this key from this map if it is present173* (optional operation). More formally, if this map contains a mapping174* from key {@code k} to value {@code v} such that175* {@code (key==null ? k==null : key.equals(k))}, that mapping176* is removed. (The map can contain at most one such mapping.)177*178* <p>Returns the value to which the map previously associated the key, or179* {@code null} if the map contained no mapping for this key. (A180* {@code null} return can also indicate that the map previously181* associated {@code null} with the specified key if the implementation182* supports {@code null} values.) The map will not contain a mapping for183* the specified key once the call returns.184*185* @param key key whose mapping is to be removed from the map.186* @return previous value associated with specified key, or {@code null}187* if there was no mapping for key.188*189* @throws NullPointerException if key is null190* @throws ClassCastException if key is not String191* @throws IllegalArgumentException if key is empty String192*/193public Object remove(Object key) {194checkKey(key);195return map.remove(key);196}197198/** {@inheritDoc} */199public int size() {200return map.size();201}202203/** {@inheritDoc} */204public Collection<Object> values() {205return map.values();206}207208private void checkKey(Object key) {209Objects.requireNonNull(key, "key can not be null");210if (!(key instanceof String)) {211throw new ClassCastException("key should be a String");212}213if (key.equals("")) {214throw new IllegalArgumentException("key can not be empty");215}216}217}218219220