Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
SimpleContext |
|
| 1.8571428571428572;1.857 |
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.commons.scxml.env; |
|
18 | ||
19 | import java.io.Serializable; |
|
20 | import java.util.HashMap; |
|
21 | import java.util.Map; |
|
22 | ||
23 | import org.apache.commons.logging.Log; |
|
24 | import org.apache.commons.logging.LogFactory; |
|
25 | import org.apache.commons.scxml.Context; |
|
26 | ||
27 | /** |
|
28 | * Simple Context wrapping a map of variables. |
|
29 | * |
|
30 | */ |
|
31 | public class SimpleContext implements Context, Serializable { |
|
32 | ||
33 | /** Serial version UID. */ |
|
34 | private static final long serialVersionUID = 1L; |
|
35 | /** Implementation independent log category. */ |
|
36 | 402 | private Log log = LogFactory.getLog(Context.class); |
37 | /** The parent Context to this Context. */ |
|
38 | private Context parent; |
|
39 | /** The Map of variables and their values in this Context. */ |
|
40 | private Map vars; |
|
41 | ||
42 | /** |
|
43 | * Constructor. |
|
44 | * |
|
45 | */ |
|
46 | public SimpleContext() { |
|
47 | 65 | this(null, null); |
48 | 65 | } |
49 | ||
50 | /** |
|
51 | * Constructor. |
|
52 | * |
|
53 | * @param parent A parent Context, can be null |
|
54 | */ |
|
55 | public SimpleContext(final Context parent) { |
|
56 | 195 | this(parent, null); |
57 | 195 | } |
58 | /** |
|
59 | * Constructor. |
|
60 | * |
|
61 | * @param initialVars A pre-populated initial variables map |
|
62 | */ |
|
63 | public SimpleContext(final Map initialVars) { |
|
64 | 129 | this(null, initialVars); |
65 | 129 | } |
66 | ||
67 | /** |
|
68 | * Constructor. |
|
69 | * |
|
70 | * @param parent A parent Context, can be null |
|
71 | * @param initialVars A pre-populated initial variables map |
|
72 | */ |
|
73 | 394 | public SimpleContext(final Context parent, final Map initialVars) { |
74 | 394 | this.parent = parent; |
75 | 394 | if (initialVars == null) { |
76 | 260 | this.vars = new HashMap(); |
77 | } else { |
|
78 | 134 | this.vars = initialVars; |
79 | } |
|
80 | 394 | } |
81 | ||
82 | /** |
|
83 | * Assigns a new value to an existing variable or creates a new one. |
|
84 | * The method searches the chain of parent Contexts for variable |
|
85 | * existence. |
|
86 | * |
|
87 | * @param name The variable name |
|
88 | * @param value The variable value |
|
89 | * @see org.apache.commons.scxml.Context#set(String, Object) |
|
90 | */ |
|
91 | public void set(final String name, final Object value) { |
|
92 | 57 | if (vars.containsKey(name)) { //first try to override local |
93 | 28 | setLocal(name, value); |
94 | 29 | } else if (parent != null && parent.has(name)) { //then check for global |
95 | 23 | parent.set(name, value); |
96 | } else { //otherwise create a new local variable |
|
97 | 6 | setLocal(name, value); |
98 | } |
|
99 | 57 | } |
100 | ||
101 | /** |
|
102 | * Get the value of this variable; delegating to parent. |
|
103 | * |
|
104 | * @param name The variable name |
|
105 | * @return Object The variable value |
|
106 | * @see org.apache.commons.scxml.Context#get(java.lang.String) |
|
107 | */ |
|
108 | public Object get(final String name) { |
|
109 | 505 | if (vars.containsKey(name)) { |
110 | 337 | return vars.get(name); |
111 | 168 | } else if (parent != null) { |
112 | 64 | return parent.get(name); |
113 | } else { |
|
114 | 104 | return null; |
115 | } |
|
116 | } |
|
117 | ||
118 | /** |
|
119 | * Check if this variable exists, delegating to parent. |
|
120 | * |
|
121 | * @param name The variable name |
|
122 | * @return boolean true if this variable exists |
|
123 | * @see org.apache.commons.scxml.Context#has(java.lang.String) |
|
124 | */ |
|
125 | public boolean has(final String name) { |
|
126 | 78 | if (vars.containsKey(name)) { |
127 | 50 | return true; |
128 | 28 | } else if (parent != null && parent.has(name)) { |
129 | 23 | return true; |
130 | } |
|
131 | 5 | return false; |
132 | } |
|
133 | ||
134 | /** |
|
135 | * Clear this Context. |
|
136 | * |
|
137 | * @see org.apache.commons.scxml.Context#reset() |
|
138 | */ |
|
139 | public void reset() { |
|
140 | 11 | vars.clear(); |
141 | 11 | } |
142 | ||
143 | /** |
|
144 | * Get the parent Context, may be null. |
|
145 | * |
|
146 | * @return Context The parent Context |
|
147 | * @see org.apache.commons.scxml.Context#getParent() |
|
148 | */ |
|
149 | public Context getParent() { |
|
150 | 311 | return parent; |
151 | } |
|
152 | ||
153 | /** |
|
154 | * Assigns a new value to an existing variable or creates a new one. |
|
155 | * The method allows to shaddow a variable of the same name up the |
|
156 | * Context chain. |
|
157 | * |
|
158 | * @param name The variable name |
|
159 | * @param value The variable value |
|
160 | * @see org.apache.commons.scxml.Context#setLocal(String, Object) |
|
161 | */ |
|
162 | public void setLocal(final String name, final Object value) { |
|
163 | 1424 | vars.put(name, value); |
164 | 1424 | if (log.isDebugEnabled() && !name.equals("_ALL_STATES")) { |
165 | 0 | log.debug(name + " = " + String.valueOf(value)); |
166 | } |
|
167 | 1424 | } |
168 | ||
169 | /** |
|
170 | * Set the variables map. |
|
171 | * |
|
172 | * @param vars The new Map of variables. |
|
173 | */ |
|
174 | protected void setVars(final Map vars) { |
|
175 | 12 | this.vars = vars; |
176 | 12 | } |
177 | ||
178 | /** |
|
179 | * Get the Map of all local variables in this Context. |
|
180 | * |
|
181 | * @return Returns the vars. |
|
182 | */ |
|
183 | public Map getVars() { |
|
184 | 796 | return vars; |
185 | } |
|
186 | ||
187 | /** |
|
188 | * Set the log used by this <code>Context</code> instance. |
|
189 | * |
|
190 | * @param log The new log. |
|
191 | */ |
|
192 | protected void setLog(final Log log) { |
|
193 | 0 | this.log = log; |
194 | 0 | } |
195 | ||
196 | /** |
|
197 | * Get the log used by this <code>Context</code> instance. |
|
198 | * |
|
199 | * @return Log The log being used. |
|
200 | */ |
|
201 | protected Log getLog() { |
|
202 | 0 | return log; |
203 | } |
|
204 | ||
205 | } |
|
206 |