class RankedClient implements Comparable<RankedClient> {

    static Map<TaskId, SortedSet<UUID>> tasksToCaughtUpClients(final SortedMap<TaskId, SortedSet<RankedClient>> statefulTasksToRankedClients) {
        final Map<TaskId, SortedSet<UUID>> taskToCaughtUpClients = new HashMap<>();
        for (final SortedMap.Entry<TaskId, SortedSet<RankedClient>> taskToRankedClients : statefulTasksToRankedClients.entrySet()) {
            final SortedSet<RankedClient> rankedClients = taskToRankedClients.getValue();
            for (final RankedClient rankedClient : rankedClients) {
                if (rankedClient.rank() == Task.LATEST_OFFSET || rankedClient.rank() == 0) {
                    final TaskId taskId = taskToRankedClients.getKey();
                    taskToCaughtUpClients.computeIfAbsent(taskId, ignored -> new TreeSet<>()).add(rankedClient.clientId());
                } else {
                    break;
                }
            }
        }
        return taskToCaughtUpClients;
    }

      RankedClient(final UUID clientId, final long rank);

}