|
21 | 21 | import java.util.function.Function; |
22 | 22 |
|
23 | 23 | public enum ProtocolAdapterConnectionState { |
24 | | - Closed(context -> 0), |
25 | | - Closing(context -> 0), |
26 | | - Connected(context -> 0), |
27 | | - Connecting(context -> 0), |
28 | | - Disconnected(context -> 0), |
29 | | - Disconnecting(context -> 0), |
30 | | - Error(context -> 0), |
31 | | - ErrorClosing(context -> 0), |
| 24 | + Closed(ProtocolAdapterConnectionState::transitionFromClosed), |
| 25 | + Closing(ProtocolAdapterConnectionState::transitionFromClosing), |
| 26 | + Connected(ProtocolAdapterConnectionState::transitionFromConnected), |
| 27 | + Connecting(ProtocolAdapterConnectionState::transitionFromConnecting), |
| 28 | + Disconnected(ProtocolAdapterConnectionState::transitionFromDisconnected), |
| 29 | + Disconnecting(ProtocolAdapterConnectionState::transitionFromDisconnecting), |
| 30 | + Error(ProtocolAdapterConnectionState::transitionFromError), |
| 31 | + ErrorClosing(ProtocolAdapterConnectionState::transitionFromErrorClosing), |
32 | 32 | ; |
33 | 33 |
|
34 | | - private final @NotNull Function<Object, Integer> transitionFunction; |
| 34 | + private final @NotNull Function<ProtocolAdapterConnectionState, ProtocolAdapterConnectionTransitionResponse> |
| 35 | + transitionFunction; |
35 | 36 |
|
36 | | - ProtocolAdapterConnectionState(@NotNull final Function<Object, Integer> transitionFunction) { |
| 37 | + ProtocolAdapterConnectionState(@NotNull final Function<ProtocolAdapterConnectionState, ProtocolAdapterConnectionTransitionResponse> transitionFunction) { |
37 | 38 | this.transitionFunction = transitionFunction; |
38 | 39 | } |
39 | 40 |
|
40 | | - public @NotNull Integer transition( |
41 | | - final @NotNull ProtocolAdapterConnectionState targetState, |
42 | | - final @NotNull Object context) { |
43 | | - return transitionFunction.apply(context); |
| 41 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromClosed( |
| 42 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 43 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.Closed; |
| 44 | + return switch (toState) { |
| 45 | + case Closed -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 46 | + case Disconnected -> ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 47 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromClosing( |
| 52 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 53 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.Closing; |
| 54 | + return switch (toState) { |
| 55 | + case Closing -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 56 | + case Closed -> ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 57 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromConnected( |
| 62 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 63 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.Connected; |
| 64 | + return switch (toState) { |
| 65 | + case Connected -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 66 | + case Disconnecting, Closing, ErrorClosing -> |
| 67 | + ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 68 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromConnecting( |
| 73 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 74 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.Connecting; |
| 75 | + return switch (toState) { |
| 76 | + case Connecting -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 77 | + case Connected, Error -> ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 78 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromDisconnected( |
| 83 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 84 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.Disconnected; |
| 85 | + return switch (toState) { |
| 86 | + case Disconnected -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 87 | + case Connecting -> ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 88 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromDisconnecting( |
| 93 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 94 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.Disconnecting; |
| 95 | + return switch (toState) { |
| 96 | + case Disconnecting -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 97 | + case Connecting -> ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 98 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromError( |
| 103 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 104 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.Error; |
| 105 | + return switch (toState) { |
| 106 | + case Error -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 107 | + case Disconnected -> ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 108 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + public static @NotNull ProtocolAdapterConnectionTransitionResponse transitionFromErrorClosing( |
| 113 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 114 | + final ProtocolAdapterConnectionState fromState = ProtocolAdapterConnectionState.ErrorClosing; |
| 115 | + return switch (toState) { |
| 116 | + case ErrorClosing -> ProtocolAdapterConnectionTransitionResponse.notChanged(fromState); |
| 117 | + case Error -> ProtocolAdapterConnectionTransitionResponse.success(fromState, toState); |
| 118 | + default -> ProtocolAdapterConnectionTransitionResponse.failure(fromState, toState); |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + public @NotNull ProtocolAdapterConnectionTransitionResponse transition( |
| 123 | + final @NotNull ProtocolAdapterConnectionState toState) { |
| 124 | + return transitionFunction.apply(toState); |
44 | 125 | } |
45 | 126 |
|
46 | 127 | public boolean isClosed() { |
|
0 commit comments