1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2.env;
18
19 import java.io.Serializable;
20 import java.util.AbstractMap;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import org.apache.commons.scxml2.Context;
25
26
27
28
29
30 public final class EffectiveContextMap extends AbstractMap<String, Object> implements Serializable {
31
32 private static final long serialVersionUID = 1L;
33
34
35 private final Context leaf;
36
37
38 public EffectiveContextMap(final Context ctx) {
39 super();
40 this.leaf = ctx;
41 }
42
43
44
45
46 @Override
47 public Set<Entry<String, Object>> entrySet() {
48 Set<Entry<String, Object>> entrySet = new HashSet<Entry<String, Object>>();
49 Context current = leaf;
50 while (current != null) {
51 entrySet.addAll(current.getVars().entrySet());
52 current = current.getParent();
53 }
54 return entrySet;
55 }
56
57
58
59
60 @Override
61 public Object put(final String key, final Object value) {
62 Object old = leaf.get(key);
63 if (leaf.has(key)) {
64 leaf.set(key, value);
65 } else {
66 leaf.setLocal(key, value);
67 }
68 return old;
69 }
70
71
72
73
74 @Override
75 public Object get(final Object key) {
76 if (key != null) {
77 Context current = leaf;
78 while (current != null) {
79 if (current.getVars().containsKey(key.toString())) {
80 return current.getVars().get(key);
81 }
82 current = current.getParent();
83 }
84 }
85 return null;
86 }
87 }