Skip to content

Commit 7cd3a5d

Browse files
committed
+ only consider groupId and orderIndex in Chunk.equals
1 parent 898eed7 commit 7cd3a5d

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,27 @@ in the same group representing the original data unit.
114114
*/
115115
@Builder
116116
public record Chunk(@NonNull UUID groupId, int orderIndex, int groupSize, byte @NonNull [] bytes)
117-
implements Serializable {
117+
implements Serializable {
118118

119119
@Serial
120120
private static final long serialVersionUID = -1879320933982945956L;
121+
122+
@Override
123+
public boolean equals(Object o) {
124+
if (this == o) {
125+
return true;
126+
}
127+
if (o == null || getClass() != o.getClass()) {
128+
return false;
129+
}
130+
Chunk chunk = (Chunk) o;
131+
return orderIndex == chunk.orderIndex && Objects.equals(groupId, chunk.groupId);
132+
}
133+
134+
@Override
135+
public int hashCode() {
136+
return Objects.hash(groupId, orderIndex);
137+
}
121138
}
122139
```
123140

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<modelVersion>4.0.0</modelVersion>
2929
<groupId>io.github.q3769</groupId>
3030
<artifactId>chunk4j</artifactId>
31-
<version>20250322.0.0</version>
31+
<version>20250322.0.2025</version>
3232
<packaging>jar</packaging>
3333
<name>chunk4j</name>
3434
<description>A Java API to chop up larger data blobs into smaller "chunks" of a pre-defined size, and stitch the

src/main/java/chunk4j/Chunk.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.Serial;
2828
import java.io.Serializable;
29+
import java.util.Objects;
2930
import java.util.UUID;
3031
import lombok.Builder;
3132
import lombok.NonNull;
@@ -36,17 +37,34 @@
3637
* than the one where the data was chopped, the group of chunks can be collectively stitched back
3738
* together to restore the original data unit.
3839
*
39-
* @author Qingtian Wang
4040
* @param groupId The unique identifier for the entire group of chunks representing the original
4141
* data unit.
4242
* @param orderIndex The order index of this chunk within the chunk group.
4343
* @param groupSize The total number of chunks in the group.
4444
* @param bytes The byte array representing the data of this chunk.
45+
* @author Qingtian Wang
4546
*/
4647
@Builder
4748
public record Chunk(@NonNull UUID groupId, int orderIndex, int groupSize, byte @NonNull [] bytes)
4849
implements Serializable {
4950

5051
@Serial
5152
private static final long serialVersionUID = -1879320933982945956L;
53+
54+
@Override
55+
public boolean equals(Object o) {
56+
if (this == o) {
57+
return true;
58+
}
59+
if (o == null || getClass() != o.getClass()) {
60+
return false;
61+
}
62+
Chunk chunk = (Chunk) o;
63+
return orderIndex == chunk.orderIndex && Objects.equals(groupId, chunk.groupId);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(groupId, orderIndex);
69+
}
5270
}

src/test/java/chunk4j/ChunkStitcherTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class maxGroups {
4848
@Test
4949
void lessThanNeeded() throws InterruptedException, ExecutionException {
5050
int originalDataItems = 1000;
51-
int insufficientMaxGroups = 2;
51+
int insufficientMaxGroups = 1;
5252
ChunkStitcher tot =
5353
new ChunkStitcher.Builder().maxStitchingGroups(insufficientMaxGroups).build();
5454
List<Chunk> chunksOfAllItems = new ArrayList<>();
@@ -59,7 +59,7 @@ void lessThanNeeded() throws InterruptedException, ExecutionException {
5959

6060
List<Future<Optional<byte[]>>> allStitchedFutures = new ArrayList<>();
6161
try (ExecutorService executorService =
62-
MoreExecutors.newBoundedVirtualThreadPerTaskExecutor(insufficientMaxGroups * 100)) {
62+
MoreExecutors.newBoundedVirtualThreadPerTaskExecutor(insufficientMaxGroups * 10)) {
6363
for (Chunk chunk : chunksOfAllItems) {
6464
allStitchedFutures.add(executorService.submit(() -> tot.stitch(chunk)));
6565
}

0 commit comments

Comments
 (0)