|
| 1 | +/* |
| 2 | + * Copyright 2019-present HiveMQ GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.hivemq.protocols.fsm; |
| 18 | + |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | + |
| 21 | +import java.util.function.BiFunction; |
| 22 | + |
| 23 | +public enum ProtocolAdapterState { |
| 24 | + Starting(ProtocolAdapterState::transitionFromStarting), |
| 25 | + Started(ProtocolAdapterState::transitionFromStarted), |
| 26 | + Stopping(ProtocolAdapterState::transitionFromStopping), |
| 27 | + Stopped(ProtocolAdapterState::transitionFromStopped), |
| 28 | + Error(ProtocolAdapterState::transitionFromError), |
| 29 | + ; |
| 30 | + |
| 31 | + private final @NotNull BiFunction<ProtocolAdapterState, ProtocolAdapterWrapper, ProtocolAdapterTransitionResult> |
| 32 | + transitionFunction; |
| 33 | + |
| 34 | + ProtocolAdapterState(@NotNull final BiFunction<ProtocolAdapterState, ProtocolAdapterWrapper, ProtocolAdapterTransitionResult> transitionFunction) { |
| 35 | + this.transitionFunction = transitionFunction; |
| 36 | + } |
| 37 | + |
| 38 | + public static ProtocolAdapterTransitionResult transitionFromStarted( |
| 39 | + final @NotNull ProtocolAdapterState targetState, |
| 40 | + final @NotNull ProtocolAdapterWrapper wrapper) { |
| 41 | + switch (targetState) { |
| 42 | + case Starting: |
| 43 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting); |
| 44 | + case Started: |
| 45 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started); |
| 46 | + case Stopping: |
| 47 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping); |
| 48 | + case Stopped: |
| 49 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped); |
| 50 | + default: |
| 51 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static ProtocolAdapterTransitionResult transitionFromStarting( |
| 56 | + final @NotNull ProtocolAdapterState targetState, |
| 57 | + final @NotNull ProtocolAdapterWrapper wrapper) { |
| 58 | + switch (targetState) { |
| 59 | + case Starting: |
| 60 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting); |
| 61 | + case Started: |
| 62 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started); |
| 63 | + case Stopping: |
| 64 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping); |
| 65 | + case Stopped: |
| 66 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped); |
| 67 | + default: |
| 68 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public static ProtocolAdapterTransitionResult transitionFromStopped( |
| 73 | + final @NotNull ProtocolAdapterState targetState, |
| 74 | + final @NotNull ProtocolAdapterWrapper wrapper) { |
| 75 | + switch (targetState) { |
| 76 | + case Starting: |
| 77 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting); |
| 78 | + case Started: |
| 79 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started); |
| 80 | + case Stopping: |
| 81 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping); |
| 82 | + case Stopped: |
| 83 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped); |
| 84 | + default: |
| 85 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static ProtocolAdapterTransitionResult transitionFromStopping( |
| 90 | + final @NotNull ProtocolAdapterState targetState, |
| 91 | + final @NotNull ProtocolAdapterWrapper wrapper) { |
| 92 | + switch (targetState) { |
| 93 | + case Starting: |
| 94 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting); |
| 95 | + case Started: |
| 96 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started); |
| 97 | + case Stopping: |
| 98 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping); |
| 99 | + case Stopped: |
| 100 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped); |
| 101 | + default: |
| 102 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static ProtocolAdapterTransitionResult transitionFromError( |
| 107 | + final @NotNull ProtocolAdapterState targetState, |
| 108 | + final @NotNull ProtocolAdapterWrapper wrapper) { |
| 109 | + switch (targetState) { |
| 110 | + case Starting: |
| 111 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting); |
| 112 | + case Started: |
| 113 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started); |
| 114 | + case Stopping: |
| 115 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping); |
| 116 | + case Stopped: |
| 117 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped); |
| 118 | + default: |
| 119 | + return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public @NotNull ProtocolAdapterTransitionResult transition( |
| 124 | + final @NotNull ProtocolAdapterState targetState, |
| 125 | + final @NotNull ProtocolAdapterWrapper wrapper) { |
| 126 | + return transitionFunction.apply(targetState, wrapper); |
| 127 | + } |
| 128 | +} |
0 commit comments