class FlowRetryUtil {

    public static FlowLog getLastSuccessfulStateLog(String failedState, List<FlowLog> flowLogs) {
        List<FlowLog> reversedOrderedFlowLog = flowLogs.stream()
                .sorted(Comparator.comparing(FlowLog::getCreated).reversed())
                .collect(Collectors.toList());
        for (int i = 0; i < reversedOrderedFlowLog.size(); i++) {
            if (failedState.equals(reversedOrderedFlowLog.get(i).getCurrentState())) {
                if (reversedOrderedFlowLog.size() < (i + 1)) {
                    return reversedOrderedFlowLog.get(i);
                } else {
                    return reversedOrderedFlowLog.get(i + 1);
                }
            }
        }
        return reversedOrderedFlowLog.get(0);
    }

    private  FlowRetryUtil();

}