this.history = history;
}
@Override
protected DecisionTask getNextHistoryTask(String nextPageToken) {
DecisionTask result = new DecisionTask();
Iterator<HistoryEvent> iterator = history.iterator();
if (!iterator.hasNext()) {
throw new IllegalStateException("empty history");
}
HistoryEvent startEvent = iterator.next();
WorkflowExecutionStartedEventAttributes startedAttributes = startEvent.getWorkflowExecutionStartedEventAttributes();
if (startedAttributes == null) {
throw new IllegalStateException("first event is not WorkflowExecutionStarted: " + startEvent);
}
List<HistoryEvent> events = new ArrayList<HistoryEvent>();
events.add(startEvent);
EventType eventType = null;
int lastStartedIndex = 0;
int index = 1;
long previousStartedEventId = 0;
long startedEventId = 0;
while (iterator.hasNext()) {
HistoryEvent event = iterator.next();
eventType = EventType.fromValue(event.getEventType());
events.add(event);
if (eventType == EventType.DecisionTaskStarted) {
previousStartedEventId = startedEventId;
startedEventId = event.getEventId();
lastStartedIndex = index;
}
index++;
}
if (events.size() > lastStartedIndex + 1) {
events = events.subList(0, lastStartedIndex + 1);
}
result.setEvents(events);
result.setPreviousStartedEventId(previousStartedEventId);
result.setStartedEventId(startedEventId);
result.setWorkflowExecution(workflowExecution);
WorkflowType workflowType = startedAttributes.getWorkflowType();
result.setWorkflowType(workflowType);
return result;
}