1
2
3
4
5
6
7 package org.state4j.sm;
8
9 import java.util.Map;
10 import java.util.TreeMap;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.state4j.exceptions.PreconditionViolatedException;
15
16
17 /***
18 * @author andrea
19 *
20 */
21 public class SmContextBaseImpl<T extends SmTarget> implements SmContext<T> {
22 Log logger = LogFactory.getLog(SmContextBaseImpl.class.getName());
23 private SmState currentState;
24 private SmEvent currentEvent;
25 private T target;
26 private Map<String, String> attributes = new TreeMap<String, String>();
27
28
29
30
31
32
33 public void entryInState(SmState state) {
34 logger.debug("entryInState(" + (state == null ? "null" : state.getName()) + ")");
35 this.setCurrentState(state);
36 }
37
38
39
40
41
42
43 public void exitFromState(SmState state) {
44 logger.debug("exitFromState(" + (state == null ? "null" : state.getName()) + ")");
45 this.setCurrentState(null);
46 }
47
48 public void setCurrentState(SmState state) {
49 this.currentState = state;
50 }
51
52
53
54
55
56
57 public SmEvent getCurrentEvent() {
58 if (!this.hasCurrentEvent()) {
59 throw new RuntimeException("Sorry!! cannot get currentEvent. It's null");
60 }
61 logger.debug("getCurrentEvent(): " + this.currentEvent.getName());
62 return this.currentEvent;
63 }
64
65
66
67
68
69
70 public void setCurrentEvent(SmEvent event) {
71 logger.debug("setCurrentEvent(" + (event == null ? "null" : event.getName()) + ")");
72 this.currentEvent = event;
73 }
74
75
76
77
78
79
80 public boolean hasCurrentEvent() {
81 return this.currentEvent != null;
82 }
83
84
85
86
87
88
89 public SmState getCurrentState() {
90 if (!this.hasCurrentState()) {
91 throw new RuntimeException("Sorry!! cannot get currentState. It's null");
92 }
93 return this.currentState;
94 }
95
96
97
98
99
100
101 public boolean hasCurrentState() {
102 return this.currentState != null;
103 }
104
105 public void setTarget(T target) {
106 this.target = target;
107 }
108
109 public T getTarget() {
110 if (!this.hasTarget()) {
111 throw new PreconditionViolatedException("Sorry!! cannot get Target");
112 }
113 return target;
114 }
115
116 public boolean hasTarget() {
117 return this.target != null;
118 }
119
120 public void setAttribute(String key, String value) {
121 if (value == null || value.equals("")) {
122 attributes.remove(key);
123 } else {
124 this.attributes.put(key, value);
125 }
126 }
127
128 public String getAttribute(String key) {
129 if (!hasAttribute(key)) {
130 throw new PreconditionViolatedException("Can't return attribute [" + key + "]");
131 }
132 return this.attributes.get(key);
133 }
134
135 public boolean hasAttribute(String key) {
136 return this.attributes.containsKey(key);
137 }
138
139 public void setAttributes(Map<String, String> attrs) {
140 this.attributes = new TreeMap<String, String>();
141 addAttributes(attrs);
142 }
143
144 public Object clone() throws CloneNotSupportedException {
145 SmContextBaseImpl<T> t = (SmContextBaseImpl<T>) super.clone();
146 t.setAttributes(new TreeMap<String, String>(this.attributes));
147 return t;
148 }
149
150 public void addAttributes(Map<String, String> attrs) {
151 for (String key : attrs.keySet()) {
152 setAttribute(key, attrs.get(key));
153 }
154 }
155
156 }