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