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.scxml2;
18
19 import java.io.Serializable;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import org.apache.commons.scxml2.model.EnterableState;
25
26 /**
27 * The current active states of a state machine
28 */
29 public class StateConfiguration implements Serializable {
30 /**
31 * Serial version UID.
32 */
33 private static final long serialVersionUID = 1L;
34
35 /**
36 * The states that are currently active.
37 */
38 private final Set<EnterableState> activeStates = new HashSet<EnterableState>();
39 private final Set<EnterableState> activeStatesSet = Collections.unmodifiableSet(activeStates);
40
41 /**
42 * The atomic states that are currently active.
43 */
44 private final Set<EnterableState> atomicStates = new HashSet<EnterableState>();
45 private final Set<EnterableState> atomicStatesSet = Collections.unmodifiableSet(atomicStates);
46
47 /**
48 * Get the active states
49 *
50 * @return active states including simple states and their
51 * complex ancestors up to the root.
52 */
53 public Set<EnterableState> getActiveStates() {
54 return activeStatesSet;
55 }
56
57 /**
58 * Get the current atomic states (leaf only).
59 *
60 * @return Returns the atomic states - simple (leaf) states only.
61 */
62 public Set<EnterableState> getStates() {
63 return atomicStatesSet;
64 }
65
66 /**
67 * Enter an active state
68 * If the state is atomic also record it add it to the current states
69 * @param state state to enter
70 */
71 public void enterState(final EnterableState state) {
72 if (!activeStates.add(state)) {
73 throw new IllegalStateException("State "+state.getId()+" already added.");
74 }
75 if (state.isAtomicState()) {
76 if (!atomicStates.add(state)) {
77 throw new IllegalStateException("Atomic state "+state.getId()+" already added.");
78 }
79 }
80 }
81
82 /**
83 * Exit an active state
84 * If the state is atomic also remove it from current states
85 * @param state state to exit
86 */
87 public void exitState(final EnterableState state) {
88 if (!activeStates.remove(state)) {
89 throw new IllegalStateException("State "+state.getId()+" not active.");
90 }
91 atomicStates.remove(state);
92 }
93
94 /**
95 * Clear the state configuration
96 */
97 public void clear() {
98 activeStates.clear();
99 atomicStates.clear();
100 }
101 }