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 */
017package org.apache.commons.scxml2;
018
019import java.io.Serializable;
020import java.util.Set;
021
022import org.apache.commons.scxml2.model.EnterableState;
023import org.apache.commons.scxml2.model.Final;
024
025/**
026 * The immutable encapsulation of the current state of a state machine.
027 *
028 */
029public class Status implements Serializable {
030
031    /**
032     * Serial version UID.
033     */
034    private static final long serialVersionUID = 1L;
035
036    private final StateConfiguration configuration;
037
038
039    public Status(StateConfiguration configuration) {
040        this.configuration = configuration;
041    }
042
043    /**
044     * @return Whether the state machine terminated AND we reached a top level Final state.
045     */
046    public boolean isFinal() {
047        return getFinalState() != null;
048    }
049
050    /**
051     * @return Returns the single top level final state in which the state machine terminated, or null otherwise
052     */
053    public Final getFinalState() {
054        if (configuration.getStates().size() == 1) {
055            EnterableState es = configuration.getStates().iterator().next();
056            if (es instanceof Final && es.getParent() == null) {
057                return (Final)es;
058            }
059        }
060        return null;
061    }
062
063    /**
064     * Get the atomic states configuration (leaf only).
065     *
066     * @return Returns the atomic states configuration - simple (leaf) states only.
067     */
068    public Set<EnterableState> getStates() {
069        return configuration.getStates();
070    }
071
072    /**
073     * Get the active states configuration.
074     *
075     * @return active states configuration including simple states and their
076     *         complex ancestors up to the root.
077     */
078    public Set<EnterableState> getActiveStates() {
079        return configuration.getActiveStates();
080    }
081
082    public boolean isInState(final String state) {
083        for (EnterableState es : configuration.getActiveStates()) {
084            if (state.equals(es.getId())) {
085                return true;
086            }
087        }
088        return false;
089    }
090}
091