Source:
private OffsetsMaps fetchTopicPartitionsMetadata(List<TopicPartition> topicPartitions) { Map<SystemStreamPartition, String> oldestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> newestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> upcomingOffsets = new HashMap<>(); Map<TopicPartition, Long> oldestOffsetsWithLong; Map<TopicPartition, Long> upcomingOffsetsWithLong; oldestOffsetsWithLong = threadSafeKafkaConsumer.execute(consumer -> consumer.beginningOffsets(topicPartitions)); LOG.debug("Kafka-fetched beginningOffsets: {}", oldestOffsetsWithLong); upcomingOffsetsWithLong = threadSafeKafkaConsumer.execute(consumer -> consumer.endOffsets(topicPartitions)); LOG.debug("Kafka-fetched endOffsets: {}", upcomingOffsetsWithLong); oldestOffsetsWithLong.forEach((topicPartition, offset) -> oldestOffsets.put(toSystemStreamPartition(topicPartition), String.valueOf(offset))); upcomingOffsetsWithLong.forEach((topicPartition, offset) -> { upcomingOffsets.put(toSystemStreamPartition(topicPartition), String.valueOf(offset)); if (offset <= 0) { LOG.warn( "Empty Kafka topic partition with upcoming offset {}. Skipping newest offset and setting oldest offset to 0 to consume from beginning", topicPartition, offset); oldestOffsets.put(toSystemStreamPartition(topicPartition), "0"); } else { newestOffsets.put(toSystemStreamPartition(topicPartition), String.valueOf(offset - 1)); } }); return new OffsetsMaps(oldestOffsets, newestOffsets, upcomingOffsets); }

Target:
private OffsetsMaps fetchTopicPartitionsMetadata(List<TopicPartition> topicPartitions) { Map<SystemStreamPartition, String> oldestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> newestOffsets = new HashMap<>(); Map<SystemStreamPartition, String> upcomingOffsets = new HashMap<>(); final Map<TopicPartition, Long> oldestOffsetsWithLong = new HashMap<>(); final Map<TopicPartition, Long> upcomingOffsetsWithLong = new HashMap<>(); threadSafeKafkaConsumer.execute(consumer -> { Map<TopicPartition, Long> beginningOffsets = consumer.beginningOffsets(topicPartitions); LOG.debug("Beginning offsets for topic-partitions: is {}", topicPartitions, beginningOffsets); oldestOffsetsWithLong.putAll(beginningOffsets); Map<TopicPartition, Long> endOffsets = consumer.endOffsets(topicPartitions); LOG.debug("End offsets for topic-partitions: is {}", topicPartitions, endOffsets); upcomingOffsetsWithLong.putAll(endOffsets); return Optional.empty(); }); oldestOffsetsWithLong.forEach((topicPartition, offset) -> oldestOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), String.valueOf(offset))); upcomingOffsetsWithLong.forEach((topicPartition, offset) -> { upcomingOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), String.valueOf(offset)); if (offset <= 0) { LOG.warn( "Empty Kafka topic partition with upcoming offset {}. Skipping newest offset and setting oldest offset to 0 to consume from beginning", topicPartition, offset); oldestOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), "0"); } else { newestOffsets.put(KafkaUtil.toSystemStreamPartition(systemName, topicPartition), String.valueOf(offset - 1)); } }); return new OffsetsMaps(oldestOffsets, newestOffsets, upcomingOffsets); }