001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.scxml.env;
018:
019: import java.io.Serializable;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.commons.scxml.Context;
026:
027: /**
028: * Simple Context wrapping a map of variables.
029: *
030: */
031: public class SimpleContext implements Context, Serializable {
032:
033: /** Serial version UID. */
034: private static final long serialVersionUID = 1L;
035: /** Implementation independent log category. */
036: private Log log = LogFactory.getLog(Context.class);
037: /** The parent Context to this Context. */
038: private Context parent;
039: /** The Map of variables and their values in this Context. */
040: private Map vars;
041:
042: /**
043: * Constructor.
044: *
045: */
046: public SimpleContext() {
047: this (null, null);
048: }
049:
050: /**
051: * Constructor.
052: *
053: * @param parent A parent Context, can be null
054: */
055: public SimpleContext(final Context parent) {
056: this (parent, null);
057: }
058:
059: /**
060: * Constructor.
061: *
062: * @param initialVars A pre-populated initial variables map
063: */
064: public SimpleContext(final Map initialVars) {
065: this (null, initialVars);
066: }
067:
068: /**
069: * Constructor.
070: *
071: * @param parent A parent Context, can be null
072: * @param initialVars A pre-populated initial variables map
073: */
074: public SimpleContext(final Context parent, final Map initialVars) {
075: this .parent = parent;
076: if (initialVars == null) {
077: this .vars = new HashMap();
078: } else {
079: this .vars = initialVars;
080: }
081: }
082:
083: /**
084: * Assigns a new value to an existing variable or creates a new one.
085: * The method searches the chain of parent Contexts for variable
086: * existence.
087: *
088: * @param name The variable name
089: * @param value The variable value
090: * @see org.apache.commons.scxml.Context#set(String, Object)
091: */
092: public void set(final String name, final Object value) {
093: if (vars.containsKey(name)) { //first try to override local
094: setLocal(name, value);
095: } else if (parent != null && parent.has(name)) { //then check for global
096: parent.set(name, value);
097: } else { //otherwise create a new local variable
098: setLocal(name, value);
099: }
100: }
101:
102: /**
103: * Get the value of this variable; delegating to parent.
104: *
105: * @param name The variable name
106: * @return Object The variable value
107: * @see org.apache.commons.scxml.Context#get(java.lang.String)
108: */
109: public Object get(final String name) {
110: if (vars.containsKey(name)) {
111: return vars.get(name);
112: } else if (parent != null) {
113: return parent.get(name);
114: } else {
115: return null;
116: }
117: }
118:
119: /**
120: * Check if this variable exists, delegating to parent.
121: *
122: * @param name The variable name
123: * @return boolean true if this variable exists
124: * @see org.apache.commons.scxml.Context#has(java.lang.String)
125: */
126: public boolean has(final String name) {
127: if (vars.containsKey(name)) {
128: return true;
129: } else if (parent != null && parent.has(name)) {
130: return true;
131: }
132: return false;
133: }
134:
135: /**
136: * Clear this Context.
137: *
138: * @see org.apache.commons.scxml.Context#reset()
139: */
140: public void reset() {
141: vars.clear();
142: }
143:
144: /**
145: * Get the parent Context, may be null.
146: *
147: * @return Context The parent Context
148: * @see org.apache.commons.scxml.Context#getParent()
149: */
150: public Context getParent() {
151: return parent;
152: }
153:
154: /**
155: * Assigns a new value to an existing variable or creates a new one.
156: * The method allows to shaddow a variable of the same name up the
157: * Context chain.
158: *
159: * @param name The variable name
160: * @param value The variable value
161: * @see org.apache.commons.scxml.Context#setLocal(String, Object)
162: */
163: public void setLocal(final String name, final Object value) {
164: vars.put(name, value);
165: if (log.isDebugEnabled() && !name.equals("_ALL_STATES")) {
166: log.debug(name + " = " + String.valueOf(value));
167: }
168: }
169:
170: /**
171: * Set the variables map.
172: *
173: * @param vars The new Map of variables.
174: */
175: protected void setVars(final Map vars) {
176: this .vars = vars;
177: }
178:
179: /**
180: * Get the Map of all local variables in this Context.
181: *
182: * @return Returns the vars.
183: */
184: public Map getVars() {
185: return vars;
186: }
187:
188: /**
189: * Set the log used by this <code>Context</code> instance.
190: *
191: * @param log The new log.
192: */
193: protected void setLog(final Log log) {
194: this .log = log;
195: }
196:
197: /**
198: * Get the log used by this <code>Context</code> instance.
199: *
200: * @return Log The log being used.
201: */
202: protected Log getLog() {
203: return log;
204: }
205:
206: }
|