View Javadoc
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.Set;
21  
22  import org.apache.commons.scxml2.model.EnterableState;
23  import org.apache.commons.scxml2.model.Final;
24  
25  /**
26   * The immutable encapsulation of the current state of a state machine.
27   *
28   */
29  public class Status implements Serializable {
30  
31      /**
32       * Serial version UID.
33       */
34      private static final long serialVersionUID = 1L;
35  
36      private final StateConfiguration configuration;
37  
38  
39      public Status(StateConfiguration configuration) {
40          this.configuration = configuration;
41      }
42  
43      /**
44       * @return Whether the state machine terminated AND we reached a top level Final state.
45       */
46      public boolean isFinal() {
47          return getFinalState() != null;
48      }
49  
50      /**
51       * @return Returns the single top level final state in which the state machine terminated, or null otherwise
52       */
53      public Final getFinalState() {
54          if (configuration.getStates().size() == 1) {
55              EnterableState es = configuration.getStates().iterator().next();
56              if (es instanceof Final && es.getParent() == null) {
57                  return (Final)es;
58              }
59          }
60          return null;
61      }
62  
63      /**
64       * Get the atomic states configuration (leaf only).
65       *
66       * @return Returns the atomic states configuration - simple (leaf) states only.
67       */
68      public Set<EnterableState> getStates() {
69          return configuration.getStates();
70      }
71  
72      /**
73       * Get the active states configuration.
74       *
75       * @return active states configuration including simple states and their
76       *         complex ancestors up to the root.
77       */
78      public Set<EnterableState> getActiveStates() {
79          return configuration.getActiveStates();
80      }
81  
82      public boolean isInState(final String state) {
83          for (EnterableState es : configuration.getActiveStates()) {
84              if (state.equals(es.getId())) {
85                  return true;
86              }
87          }
88          return false;
89      }
90  }
91