blob: 62100bc133e94fd7219caf0c4dc40089754140e0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.script.util;
27import java.util.*;
28import javax.script.Bindings;
29
30/*
31 * Abstract super class for Bindings implementations. Handles
32 * global and local scopes.
33 *
34 * @author Mike Grogan
35 * @since 1.6
36 */
37public abstract class BindingsImpl extends BindingsBase {
38
39 //get method delegates to global if key is not defined in
40 //base class or local scope
41 protected Bindings global = null;
42
43 //get delegates to local scope
44 protected Bindings local = null;
45
46 public void setGlobal(Bindings n) {
47 global = n;
48 }
49
50 public void setLocal(Bindings n) {
51 local = n;
52 }
53
54 public Set<Map.Entry<String, Object>> entrySet() {
55 return new BindingsEntrySet(this);
56 }
57
58 public Object get(Object key) {
59 checkKey(key);
60
61 Object ret = null;
62 if ((local != null) && (null != (ret = local.get(key)))) {
63 return ret;
64 }
65
66 ret = getImpl((String)key);
67
68 if (ret != null) {
69 return ret;
70 } else if (global != null) {
71 return global.get(key);
72 } else {
73 return null;
74 }
75 }
76
77 public Object remove(Object key) {
78 checkKey(key);
79 Object ret = get(key);
80 if (ret != null) {
81 removeImpl((String)key);
82 }
83 return ret;
84 }
85}