ConnectionStateEvent.java

  1. /**
  2.  *
  3.  * Copyright 2018-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * 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.jivesoftware.smack.fsm;

  18. import org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex;

  19. public class ConnectionStateEvent {

  20.     private final StateDescriptor currentStateDescriptor;
  21.     private final StateDescriptor successorStateDescriptor;

  22.     private final long timestamp;

  23.     public ConnectionStateEvent(StateDescriptor currentStateDescriptor) {
  24.         this(currentStateDescriptor, null);
  25.     }

  26.     public ConnectionStateEvent(StateDescriptor currentStateDescriptor, StateDescriptor successorStateDescriptor) {
  27.         this.currentStateDescriptor = currentStateDescriptor;
  28.         this.successorStateDescriptor = successorStateDescriptor;
  29.         this.timestamp = System.currentTimeMillis();
  30.     }

  31.     public StateDescriptor getStateDescriptor() {
  32.         return currentStateDescriptor;
  33.     }

  34.     @Override
  35.     public String toString() {
  36.         if (successorStateDescriptor == null) {
  37.             return getClass().getSimpleName() + ": " + currentStateDescriptor.getStateName();
  38.         } else {
  39.             return currentStateDescriptor.getStateName() + ' ' + getClass().getSimpleName() + ' '
  40.                             + successorStateDescriptor.getStateName();
  41.         }
  42.     }

  43.     public long getTimestamp() {
  44.         return timestamp;
  45.     }

  46.     public static class StateRevertBackwardsWalk extends ConnectionStateEvent {
  47.         public StateRevertBackwardsWalk(State state) {
  48.             super(state.getStateDescriptor());
  49.         }
  50.     }

  51.     public static class FinalStateReached extends ConnectionStateEvent {
  52.         public FinalStateReached(State state) {
  53.             super(state.getStateDescriptor());
  54.         }
  55.     }

  56.     public static class TransitionNotPossible extends ConnectionStateEvent {
  57.         private final StateTransitionResult.TransitionImpossible transitionImpossibleReason;

  58.         public TransitionNotPossible(State currentState, State successorState, StateTransitionResult.TransitionImpossible reason) {
  59.             super(currentState.getStateDescriptor(), successorState.getStateDescriptor());
  60.             this.transitionImpossibleReason = reason;
  61.         }

  62.         @Override
  63.         public String toString() {
  64.             return super.toString() + ": " + transitionImpossibleReason;
  65.         }
  66.     }

  67.     public static class AboutToTransitionInto extends ConnectionStateEvent {
  68.         public AboutToTransitionInto(State currentState, State successorState) {
  69.             super(currentState.getStateDescriptor(), successorState.getStateDescriptor());
  70.         }
  71.     }

  72.     public static class TransitionFailed extends ConnectionStateEvent {
  73.         private final StateTransitionResult.Failure transitionFailedReason;

  74.         public TransitionFailed(State currentState, State failedSuccessorState, StateTransitionResult.Failure transitionFailedReason) {
  75.             super(currentState.getStateDescriptor(), failedSuccessorState.getStateDescriptor());
  76.             this.transitionFailedReason = transitionFailedReason;
  77.         }

  78.         @Override
  79.         public String toString() {
  80.             return super.toString() + ": " + transitionFailedReason;
  81.         }
  82.     }

  83.     public static class TransitionIgnoredDueCycle extends ConnectionStateEvent {
  84.         public TransitionIgnoredDueCycle(GraphVertex<State> currentStateVertex, GraphVertex<State> successorStateVertexCausingCycle) {
  85.             super(currentStateVertex.getElement().getStateDescriptor(), successorStateVertexCausingCycle.getElement().getStateDescriptor());
  86.         }
  87.     }

  88.     public static class SuccessfullyTransitionedInto extends ConnectionStateEvent {
  89.         private final StateTransitionResult.Success transitionSuccessResult;

  90.         public SuccessfullyTransitionedInto(State state, StateTransitionResult.Success transitionSuccessResult) {
  91.             super(state.getStateDescriptor());
  92.             this.transitionSuccessResult = transitionSuccessResult;
  93.         }

  94.         @Override
  95.         public String toString() {
  96.             return super.toString() + ": " + transitionSuccessResult;
  97.         }
  98.     }

  99.     public abstract static class DetailedTransitionIntoInformation extends ConnectionStateEvent {
  100.         protected DetailedTransitionIntoInformation(State state) {
  101.             super(state.getStateDescriptor());
  102.         }
  103.     }
  104. }