Skip to content

Commit

Permalink
feat: Improvements ChunkUtils (#289)
Browse files Browse the repository at this point in the history
Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 authored Oct 24, 2024
1 parent 4fb471c commit 8d10540
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
33 changes: 19 additions & 14 deletions common/src/main/java/com/hedera/block/common/utils/ChunkUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> the type of the collection
* @return a list of lists of the specified size
* */
public static <T> List<List<T>> chunkify(
@NonNull final Collection<T> collection, final int chunkSize) {
Objects.requireNonNull(collection);
@NonNull final Collection<T> 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<T> localCollection = List.copyOf(collection);
final List<T> 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<List<T>> 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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ public boolean isTimeoutExpired() {
private ResponseSender getResponseSender(
@NonNull final SubscribeStreamResponse subscribeStreamResponse) {

final OneOf<SubscribeStreamResponse.ResponseOneOfType> oneOfTypeOneOf =
final OneOf<SubscribeStreamResponse.ResponseOneOfType> 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());
};
}

Expand Down

0 comments on commit 8d10540

Please sign in to comment.