From 8d105402c49c8a79b3e09da09e34bb2226248932 Mon Sep 17 00:00:00 2001 From: Alfredo Gutierrez Date: Wed, 23 Oct 2024 18:16:48 -0600 Subject: [PATCH] feat: Improvements ChunkUtils (#289) Signed-off-by: Alfredo Gutierrez --- .../hedera/block/common/utils/ChunkUtils.java | 33 +++++++++++-------- .../ConsumerStreamResponseObserver.java | 6 ++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/common/src/main/java/com/hedera/block/common/utils/ChunkUtils.java b/common/src/main/java/com/hedera/block/common/utils/ChunkUtils.java index 3c2481ef6..671343040 100644 --- a/common/src/main/java/com/hedera/block/common/utils/ChunkUtils.java +++ b/common/src/main/java/com/hedera/block/common/utils/ChunkUtils.java @@ -17,38 +17,43 @@ package com.hedera.block.common.utils; import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; -import java.util.stream.IntStream; /** Utility class for chunking collections. */ public final class ChunkUtils { - /** Chunks a collection into a list of lists of the specified size. - * @param collection the collection to chunk, if the collection is empty, an empty list is returned. + /** + * Chunk a collection into a list of lists. + * The resulting list will have a specified size. + * + * @param dataToSplit the collection to chunk, if the collection is empty, an empty list is returned. * @param chunkSize the size of each chunk * @param the type of the collection * @return a list of lists of the specified size * */ public static List> chunkify( - @NonNull final Collection collection, final int chunkSize) { - Objects.requireNonNull(collection); + @NonNull final Collection dataToSplit, final int chunkSize) { + Objects.requireNonNull(dataToSplit); if (chunkSize <= 0) { throw new IllegalArgumentException("Chunk size must be greater than 0"); } - if (collection.isEmpty()) { + if (dataToSplit.isEmpty()) { return Collections.emptyList(); // or throw, depends on how we want to handle } - final List localCollection = List.copyOf(collection); + final List localCollection = List.copyOf(dataToSplit); final int localCollectionSize = localCollection.size(); - return IntStream.iterate(0, i -> i < localCollectionSize, i -> i + chunkSize) - .mapToObj( - i -> - localCollection.subList( - i, Math.min(i + chunkSize, localCollectionSize))) - .collect(Collectors.toList()); + + List> result = new ArrayList<>(); + + for (int i = 0; i < localCollectionSize; i += chunkSize) { + int end = Math.min(i + chunkSize, localCollectionSize); + result.add(localCollection.subList(i, end)); + } + + return result; } private ChunkUtils() {} diff --git a/server/src/main/java/com/hedera/block/server/consumer/ConsumerStreamResponseObserver.java b/server/src/main/java/com/hedera/block/server/consumer/ConsumerStreamResponseObserver.java index 20bf465cf..4e36dff26 100644 --- a/server/src/main/java/com/hedera/block/server/consumer/ConsumerStreamResponseObserver.java +++ b/server/src/main/java/com/hedera/block/server/consumer/ConsumerStreamResponseObserver.java @@ -184,13 +184,13 @@ public boolean isTimeoutExpired() { private ResponseSender getResponseSender( @NonNull final SubscribeStreamResponse subscribeStreamResponse) { - final OneOf oneOfTypeOneOf = + final OneOf responseType = subscribeStreamResponse.response(); - return switch (oneOfTypeOneOf.kind()) { + return switch (responseType.kind()) { case STATUS -> statusResponseSender; case BLOCK_ITEMS -> blockItemsResponseSender; default -> throw new IllegalArgumentException( - "Unknown response type: " + oneOfTypeOneOf.kind()); + "Unknown response type: " + responseType.kind()); }; }