Skip to content

Commit

Permalink
[Java] Move CRC classes to Agrona.
Browse files Browse the repository at this point in the history
  • Loading branch information
vyazelenko committed Nov 26, 2024
1 parent 80c9f3c commit edd5733
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
*/
package io.aeron.archive.checksum;

import java.util.Objects;
import org.agrona.Strings;

/**
* Factory and common methods for working with {@link Checksum} instances.
*/
public final class Checksums
{
private static final Checksum CRC_32 = Crc32.INSTANCE;
private static final Checksum CRC_32C = Crc32c.INSTANCE;

/**
* Returns an instance of {@link Checksum} that computes CRC-32 checksums.
*
* @return CRC-32 implementation.
*/
public static Checksum crc32()
{
return Crc32.INSTANCE;
return CRC_32;
}

/**
Expand All @@ -39,44 +42,51 @@ public static Checksum crc32()
*/
public static Checksum crc32c()
{
return Crc32c.INSTANCE;
return CRC_32C;
}

/**
* Factory method to create an instance of the {@link Checksum} interface.
*
* @param className fully qualified class name of the {@link Checksum} implementation.
* @param className fully qualified class name or an alias of the {@link Checksum} implementation.
* @return a {@link Checksum} instance.
* @throws NullPointerException if {@code className == null}.
* @throws IllegalArgumentException if {@code className == null} or empty.
* @throws IllegalStateException if an attempt was made to acquire CRC-32C while running on JDK 8.
* @throws IllegalArgumentException if an error occurs while creating a {@link Checksum} instance.
* @throws ClassCastException if created instance does not implement the {@link Checksum} interface.
* @see #crc32c()
*/
public static Checksum newInstance(final String className)
{
Objects.requireNonNull(className, "className is required!");

if (Crc32.class.getName().equals(className))
{
return crc32();
}
else if (Crc32c.class.getName().equals(className))
if (Strings.isEmpty(className))
{
return crc32c();
throw new IllegalArgumentException("className is empty");
}
else

return switch (className)
{
try
{
final Class<?> klass = Class.forName(className);
final Object instance = klass.getDeclaredConstructor().newInstance();
return (Checksum)instance;
}
catch (final ReflectiveOperationException ex)
case "CRC-32":
case "io.aeron.archive.checksum.Crc32":
case "org.agrona.checksum.Crc32":
yield crc32();
case "CRC-32C":
case "io.aeron.archive.checksum.Crc32c":
case "org.agrona.checksum.Crc32c":
yield crc32c();
default:
{
throw new IllegalArgumentException("failed to create Checksum instance for class: " + className, ex);
try
{
final Class<?> klass = Class.forName(className);
final Object instance = klass.getDeclaredConstructor().newInstance();
yield (Checksum)instance;
}
catch (final ReflectiveOperationException ex)
{
throw new IllegalArgumentException(
"failed to create Checksum instance for class: " + className, ex);
}
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
/**
* Implementation of the {@link Checksum} interface that computes CRC-32 checksum.
*/
// TODO: Replace with Agrona classes when released
final class Crc32 implements Checksum
{
private static final MethodHandle UPDATE_BYTE_BUFFER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* <em>Note: Available only on JDK 9+.</em>
* </p>
*/
// TODO: Replace with Agrona classes when released
final class Crc32c implements Checksum
{
public static final Crc32c INSTANCE = new Crc32c();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,51 @@
package io.aeron.archive.checksum;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.condition.JRE.JAVA_9;

class ChecksumsTest
{
@Test
void crc32()
{
assertSame(Crc32.INSTANCE, Checksums.crc32());
final Checksum instance = Checksums.crc32();
assertNotNull(instance);
assertSame(instance, Checksums.crc32());
}

@EnabledForJreRange(min = JAVA_9)
@Test
void crc32c()
{
assertSame(Crc32c.INSTANCE, Checksums.crc32c());
final Checksum instance = Checksums.crc32c();
assertNotNull(instance);
assertSame(instance, Checksums.crc32c());
}

@Test
void newInstanceReturnsSameInstanceOfCrc32()
@ParameterizedTest
@ValueSource(strings = { "CRC-32", "io.aeron.archive.checksum.Crc32", "org.agrona.checksum.Crc32" })
void newInstanceReturnsSameInstanceOfCrc32(final String alias)
{
assertSame(Crc32.INSTANCE, Checksums.newInstance(Crc32.class.getName()));
assertSame(Checksums.crc32(), Checksums.newInstance(alias));
}

@EnabledForJreRange(min = JAVA_9)
@Test
void newInstanceReturnsSameInstanceOfCrc32c()
@ParameterizedTest
@ValueSource(strings = { "CRC-32C", "io.aeron.archive.checksum.Crc32c", "org.agrona.checksum.Crc32c" })
void newInstanceReturnsSameInstanceOfCrc32c(final String alias)
{
assertSame(Crc32c.INSTANCE, Checksums.newInstance(Crc32c.class.getName()));
assertSame(Checksums.crc32c(), Checksums.newInstance(alias));
}

@Test
void newInstanceThrowsNullPointerExceptionIfClassNameIsNull()
@ParameterizedTest
@NullAndEmptySource
void newInstanceThrowsNullPointerExceptionIfClassNameIsNull(final String alias)
{
assertThrows(NullPointerException.class, () -> Checksums.newInstance(null));
assertThrows(IllegalArgumentException.class, () -> Checksums.newInstance(alias));
}

@Test
Expand Down Expand Up @@ -96,4 +102,4 @@ public int compute(final long address, final int offset, final int length)
return 0;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand All @@ -30,7 +28,6 @@
import static org.agrona.BufferUtil.address;
import static org.junit.jupiter.api.Assertions.assertEquals;

@EnabledForJreRange(min = JRE.JAVA_9)
class Crc32cTest
{
private Constructor<?> constructor;
Expand Down

0 comments on commit edd5733

Please sign in to comment.