class JobFunctions {

    public static Optional<Long> getTimeInState(Task task, TaskState checkedState, Clock clock) {
        return findTaskStatus(task, checkedState).map(checkedStatus -> {
            TaskState currentState = task.getStatus().getState();
            if (currentState == checkedState) {
                return clock.wallTime() - task.getStatus().getTimestamp();
            }
            if (TaskState.isAfter(checkedState, currentState)) {
                return 0L;
            }
            return findStatusAfter(task, checkedState).map(after -> after.getTimestamp() - checkedStatus.getTimestamp()).orElse(0L);
        });
    }

    private  JobFunctions();

}