001/**
002 *
003 * Copyright 2018-2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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.jivesoftware.smack.fsm;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022
023import org.jivesoftware.smack.SmackException;
024import org.jivesoftware.smack.c2s.internal.WalkStateGraphContext;
025import org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex;
026
027public abstract class StateMachineException extends SmackException {
028
029    private static final long serialVersionUID = 1L;
030
031    protected StateMachineException(String message) {
032        super(message);
033    }
034
035    protected StateMachineException() {
036        super();
037    }
038
039    public static class SmackMandatoryStateFailedException extends StateMachineException {
040
041        private static final long serialVersionUID = 1L;
042
043        public SmackMandatoryStateFailedException(State state, StateTransitionResult failureReason) {
044        }
045    }
046
047    public static final class SmackStateGraphDeadEndException extends StateMachineException {
048
049        private final List<State> walkedStateGraphPath;
050
051        private final Map<State, StateTransitionResult> failedStates;
052
053        private final StateDescriptor deadEndState;
054
055        private static final long serialVersionUID = 1L;
056
057        private SmackStateGraphDeadEndException(String message, WalkStateGraphContext walkStateGraphContext, GraphVertex<State> stateVertex) {
058            super(message);
059            this.walkedStateGraphPath = Collections.unmodifiableList(walkStateGraphContext.getWalk());
060            this.failedStates = Collections.unmodifiableMap(walkStateGraphContext.getFailedStates());
061
062            deadEndState = stateVertex.getElement().getStateDescriptor();
063        }
064
065        public List<State> getWalkedStateGraph() {
066            return walkedStateGraphPath;
067        }
068
069        public Map<State, StateTransitionResult> getFailedStates() {
070            return failedStates;
071        }
072
073        public StateDescriptor getDeadEndState() {
074            return deadEndState;
075        }
076
077        public static SmackStateGraphDeadEndException from(WalkStateGraphContext walkStateGraphContext, GraphVertex<State> stateVertex) {
078            String message = stateVertex + " has no successor vertexes";
079
080            return new SmackStateGraphDeadEndException(message, walkStateGraphContext, stateVertex);
081        }
082    }
083}