• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #169

pending completion
#169

push

github-actions

web-flow
Remove use of deprecated API (#1758)

4 of 4 new or added lines in 1 file covered. (100.0%)

17345 of 21558 relevant lines covered (80.46%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.37
/temporal-sdk/src/main/java/io/temporal/internal/statemachines/StateMachine.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.internal.statemachines;
22

23
import io.temporal.api.enums.v1.CommandType;
24
import io.temporal.api.enums.v1.EventType;
25
import io.temporal.internal.common.ProtocolType;
26
import java.util.ArrayList;
27
import java.util.List;
28
import java.util.Objects;
29
import java.util.Set;
30
import javax.annotation.Nullable;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

34
/**
35
 * State machine instance of a single server side entity like activity, workflow task or the whole
36
 * workflow.
37
 *
38
 * @see StateMachineDefinition
39
 */
40
final class StateMachine<State, ExplicitEvent, Data> {
41
  private static final Logger log = LoggerFactory.getLogger(StateMachine.class);
1✔
42

43
  private final StateMachineDefinition<State, ExplicitEvent, Data> definition;
44

45
  private final List<Transition<State, TransitionEvent<ExplicitEvent>>> transitionHistory =
1✔
46
      new ArrayList<>();
47

48
  @Nullable private final String entityName;
49

50
  private State state;
51

52
  /**
53
   * Create a new instance of the StateMachine.
54
   *
55
   * @param definition State machine definition.
56
   * @param entityName name or id of the entity this state machine represents. For debug purposes
57
   *     only. Can be null.
58
   */
59
  public static <State, ExplicitEvent, Data> StateMachine<State, ExplicitEvent, Data> newInstance(
60
      StateMachineDefinition<State, ExplicitEvent, Data> definition, @Nullable String entityName) {
61
    return new StateMachine<>(definition, entityName);
1✔
62
  }
63

64
  private StateMachine(
65
      StateMachineDefinition<State, ExplicitEvent, Data> definition, @Nullable String entityName) {
1✔
66
    this.definition = Objects.requireNonNull(definition);
1✔
67
    this.entityName = entityName;
1✔
68
    this.state = definition.getInitialState();
1✔
69
  }
1✔
70

71
  /** All possible history event types that are known to this state machine instance. */
72
  public Set<EventType> getValidEventTypes() {
73
    return definition.getValidEventTypes();
1✔
74
  }
75

76
  /** Current state of the state machine. */
77
  public State getState() {
78
    return state;
1✔
79
  }
80

81
  /** Is this state final? */
82
  public boolean isFinalState() {
83
    return definition.isFinalState(state);
1✔
84
  }
85

86
  /**
87
   * Applies an explicit event for handling.
88
   *
89
   * @param explicitEvent event to handle.
90
   * @param data data which is passed as an argument to resulting action.
91
   */
92
  public void handleExplicitEvent(ExplicitEvent explicitEvent, Data data) {
93
    executeTransition(new TransitionEvent<>(explicitEvent), data);
1✔
94
  }
1✔
95

96
  /**
97
   * Applies an event history event for handling.
98
   *
99
   * @param eventType type of the event to handle.
100
   * @param data data which is passed as an argument to resulting action.
101
   */
102
  public void handleHistoryEvent(EventType eventType, Data data) {
103
    executeTransition(new TransitionEvent<>(eventType), data);
1✔
104
  }
1✔
105

106
  /**
107
   * Applies a message for handling.
108
   *
109
   * @param messageType type of the message to handle.
110
   * @param data data which is passed as an argument to resulting action.
111
   */
112
  public void handleMessage(ProtocolType messageType, Data data) {
113
    executeTransition(new TransitionEvent<>(messageType), data);
1✔
114
  }
1✔
115

116
  /**
117
   * Applies a command for handling.
118
   *
119
   * @param commandType type of the command to handle.
120
   * @param data data which is passed as an argument to resulting action.
121
   */
122
  public void handleCommand(CommandType commandType, Data data) {
123
    executeTransition(new TransitionEvent<>(commandType), data);
1✔
124
  }
1✔
125

126
  public String getHistory() {
127
    return transitionHistory.toString();
×
128
  }
129

130
  List<Transition<State, TransitionEvent<ExplicitEvent>>> getTransitionHistory() {
131
    return transitionHistory;
1✔
132
  }
133

134
  @Override
135
  public String toString() {
136
    return "StateMachine{"
×
137
        + "definition="
138
        + definition
139
        + ", state="
140
        + state
141
        + ", transitionHistory="
142
        + transitionHistory
143
        + '}';
144
  }
145

146
  private void executeTransition(TransitionEvent<ExplicitEvent> transitionEvent, Data data) {
147
    Transition<State, TransitionEvent<ExplicitEvent>> transition =
1✔
148
        new Transition<>(state, transitionEvent);
149
    TransitionAction<State, Data> destination = definition.getTransitionAction(transition);
1✔
150
    if (destination == null) {
1✔
151
      throw new IllegalArgumentException(
×
152
          stateMachineNameString()
×
153
              + ": invalid "
154
              + transition
155
              + ", transition history is "
156
              + transitionHistory);
157
    }
158
    try {
159
      state = destination.apply(data);
1✔
160
      logTransition(transition);
1✔
161
    } catch (RuntimeException e) {
1✔
162
      throw new RuntimeException(
1✔
163
          stateMachineNameString()
1✔
164
              + ": failure executing "
165
              + transition
166
              + ", transition history is "
167
              + transitionHistory,
168
          e);
169
    }
1✔
170
    transitionHistory.add(transition);
1✔
171
  }
1✔
172

173
  private void logTransition(Transition<State, TransitionEvent<ExplicitEvent>> transition) {
174
    if (log.isTraceEnabled()) {
1✔
175
      log.trace(
×
176
          "State Machine {}: {} --:{}:--> {}",
177
          stateMachineNameString(),
×
178
          transition.from,
179
          transition.event,
180
          state);
181
    }
182
  }
1✔
183

184
  private String stateMachineNameString() {
185
    return definition.getName()
1✔
186
        + (entityName != null && !entityName.isEmpty() ? "[" + entityName + "]" : "");
1✔
187
  }
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc