Skip to content

Commit

Permalink
+ javadoc
Browse files Browse the repository at this point in the history
+ elf4j version bump
  • Loading branch information
q3769 committed Jun 10, 2024
1 parent 8859c8c commit fce17ab
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 66 deletions.
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<dependency>
<groupId>io.github.elf4j</groupId>
<artifactId>elf4j</artifactId>
<version>4.0.0</version>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
Expand Down Expand Up @@ -212,8 +212,11 @@
<configuration>
<java>
<palantirJavaFormat>
<version>2.38.0</version>
<version>2.47.0</version>
<style>PALANTIR</style>
<formatJavadoc>true</formatJavadoc>
</palantirJavaFormat>
<removeUnusedImports/>
<formatAnnotations/>
</java>
</configuration>
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/chunk4j/Chopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@
import java.util.List;

/**
* The Chopper interface defines the contract for chopping a data blob into chunks. Implementations of this interface
* should provide a method to chop a byte array into a list of Chunk objects. Each Chunk object represents a portion of
* the original data blob. The size of each portion is determined by a pre-configured maximum size (the Chunk's
* capacity). If the size of the original data blob is smaller or equal to the Chunk's capacity, the returned list will
* contain only one Chunk.
*
* @author Qingtian Wang
*/
public interface Chopper {

/**
* @param bytes
* the original data blob to be chopped into chunks
* @return the group of chunks which the original data blob is chopped into. Each chunk carries a portion of the
* original bytes; and the size of that portion has a pre-configured maximum (a.k.a. the {@code Chunk}'s
* capacity). Thus, if the size of the original bytes is smaller or equal to the chunk's capacity, then the
* returned chunk group will have only one chunk element.
* Chops a byte array into a list of Chunk objects. Each Chunk object represents a portion of the original data
* blob. The size of each portion is determined by a pre-configured maximum size (the Chunk's capacity). If the size
* of the original data blob is smaller or equal to the Chunk's capacity, the returned list will contain only one
* Chunk.
*
* @param bytes the original data blob to be chopped into chunks
* @return the group of chunks which the original data blob is chopped into.
*/
List<Chunk> chop(byte[] bytes);
}
22 changes: 8 additions & 14 deletions src/main/java/chunk4j/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@
import lombok.Value;

/**
* A larger blob of data can be chopped up into smaller "chunks" to form a "group". When needed, often on a different
* network node than the one where the data was chopped, the group of chunks can be collectively stitched back together
* to restore the original data.
*
* @author Qingtian Wang
* The Chunk class represents a chunk of data that is part of a larger data blob. The data blob can be chopped up into
* smaller chunks to form a group. When needed, often on a different network node than the one where the data was
* chopped, the group of chunks can be collectively stitched back together to restore the original data. The Chunk class
* is thread-safe and serializable. @author Qingtian Wang
*/
@Value
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Builder
public class Chunk implements Serializable {

private static final long serialVersionUID = -1879320933982945956L;
/**
* The group ID of the original data blob. All chunks in the same group share the same group ID.
*/

/** The group ID of the original data blob. All chunks in the same group share the same group ID. */
@EqualsAndHashCode.Include
UUID groupId;

Expand All @@ -58,14 +56,10 @@ public class Chunk implements Serializable {
@EqualsAndHashCode.Include
int index;

/**
* Total number of chunks the original data blob is chopped to form the group.
*/
/** Total number of chunks the original data blob is chopped to form the group. */
int groupSize;

/**
* Data bytes chopped for this current chunk to hold.
*/
/** Data bytes chopped for this current chunk to hold. */
@ToString.Exclude
byte[] bytes;
}
36 changes: 30 additions & 6 deletions src/main/java/chunk4j/ChunkChopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,25 @@
import java.util.List;
import java.util.UUID;
import javax.annotation.concurrent.ThreadSafe;
import lombok.NonNull;

/**
* The ChunkChopper class is responsible for chopping data into chunks. It is thread-safe and provides a static factory
* method for creating new instances. Each instance is configured with a maximum chunk byte size.
*
* @author Qingtian Wang
*/
@ThreadSafe
public final class ChunkChopper implements Chopper {

private final int chunkCapacity;

/**
* Private constructor for the ChunkChopper class. It is used by the static factory method to create a new instance
* of ChunkChopper.
*
* @param chunkByteCapacity The maximum size of the byte array in a chunk.
*/
private ChunkChopper(int chunkByteCapacity) {
if (chunkByteCapacity <= 0) {
throw new IllegalArgumentException(
Expand All @@ -47,16 +57,24 @@ private ChunkChopper(int chunkByteCapacity) {
}

/**
* @param maxChunkByteSize
* how big you want the chunks of your data chopped up to be
* @return new Chopper instance
* Static factory method for creating a new ChunkChopper.
*
* @param maxChunkByteSize The maximum size of the byte array in a chunk.
* @return A new ChunkChopper.
*/
public static ChunkChopper ofByteSize(int maxChunkByteSize) {
public static @NonNull ChunkChopper ofByteSize(int maxChunkByteSize) {
return new ChunkChopper(maxChunkByteSize);
}

/**
* Chops a byte array into chunks. Each chunk is represented by a Chunk object and contains a portion of the
* original byte array. The chunks are added to a list, which is returned by the method.
*
* @param bytes The byte array to chop into chunks.
* @return A list of chunks.
*/
@Override
public List<Chunk> chop(byte[] bytes) {
public @NonNull List<Chunk> chop(byte[] bytes) {
final List<Chunk> chunks = new ArrayList<>();
final UUID groupId = UUID.randomUUID();
final int groupSize = numberOfChunks(bytes);
Expand All @@ -75,7 +93,13 @@ public List<Chunk> chop(byte[] bytes) {
return chunks;
}

private int numberOfChunks(byte[] bytes) {
/**
* Calculates the number of chunks that a byte array will be chopped into.
*
* @param bytes The byte array to chop into chunks.
* @return The number of chunks.
*/
private int numberOfChunks(byte @NonNull [] bytes) {
int chunkCount = bytes.length / this.chunkCapacity;
return bytes.length % this.chunkCapacity == 0 ? chunkCount : chunkCount + 1;
}
Expand Down
Loading

0 comments on commit fce17ab

Please sign in to comment.