001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.scxml;
018    
019    import java.io.Serializable;
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.HashSet;
023    import java.util.Iterator;
024    import java.util.Set;
025    
026    import org.apache.commons.scxml.model.State;
027    
028    /**
029     * The encapsulation of the current state of a state machine.
030     *
031     */
032    public class Status implements Serializable {
033    
034        /**
035         * Serial version UID.
036         */
037        private static final long serialVersionUID = 1L;
038    
039        /**
040         * The states that are currently active.
041         */
042        private Set states;
043    
044        /**
045         * The events that are currently queued.
046         */
047        private Collection events;
048    
049        /**
050         * Have we reached a final configuration for this state machine.
051         *
052         * True - if all the states are final and there are not events
053         * pending from the last step. False - otherwise.
054         *
055         * @return Whether a final configuration has been reached.
056         */
057        public boolean isFinal() {
058            boolean rslt = true;
059            for (Iterator i = states.iterator(); i.hasNext();) {
060                State s = (State) i.next();
061                if (!s.isFinal()) {
062                    rslt = false;
063                    break;
064                }
065                //the status is final only iff these are top-level states
066                if (s.getParent() != null) {
067                    rslt = false;
068                    break;
069                }
070            }
071            if (!events.isEmpty()) {
072                rslt = false;
073            }
074            return rslt;
075        }
076    
077        /**
078         * Constructor.
079         */
080        public Status() {
081            states = new HashSet();
082            events = new ArrayList();
083        }
084    
085        /**
086         * Get the states configuration (leaf only).
087         *
088         * @return Returns the states configuration - simple (leaf) states only.
089         */
090        public Set getStates() {
091            return states;
092        }
093    
094        /**
095         * Get the events that are currently queued.
096         *
097         * @return The events that are currently queued.
098         */
099        public Collection getEvents() {
100            return events;
101        }
102    
103        /**
104         * Get the complete states configuration.
105         *
106         * @return complete states configuration including simple states and their
107         *         complex ancestors up to the root.
108         */
109        public Set getAllStates() {
110            return SCXMLHelper.getAncestorClosure(states, null);
111        }
112    
113    }
114