Skip to content

Commit

Permalink
Fix checkstyle issues in com.fauna.codec
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Nov 12, 2024
1 parent fdfc88c commit 9a73c93
Show file tree
Hide file tree
Showing 51 changed files with 1,300 additions and 461 deletions.
32 changes: 31 additions & 1 deletion src/main/java/com/fauna/codec/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,44 @@

import com.fauna.exception.CodecException;


/**
* Interface for codecs, which handle the serialization and deserialization of specific types.
* <p>
* Each codec is associated with a particular class and supports a set of Fauna data types.
*
* @param <T> The type of object that this codec can encode and decode.
*/
public interface Codec<T> {

/**
* Decodes an object from the provided {@link UTF8FaunaParser}.
*
* @param parser The parser to use for reading and decoding the data.
* @return The decoded object of type {@code T}.
* @throws CodecException If an error occurs during decoding.
*/
T decode(UTF8FaunaParser parser) throws CodecException;

/**
* Encodes the specified object using the provided {@link UTF8FaunaGenerator}.
*
* @param gen The generator to use for writing and encoding the data.
* @param obj The object of type {@code T} to encode.
* @throws CodecException If an error occurs during encoding.
*/
void encode(UTF8FaunaGenerator gen, T obj) throws CodecException;

/**
* Gets the class associated with this codec.
*
* @return The {@link Class} type that this codec handles.
*/
Class<?> getCodecClass();

/**
* Gets the set of supported Fauna data types for this codec.
*
* @return An array of {@link FaunaType} values representing the supported types.
*/
FaunaType[] getSupportedTypes();
}
19 changes: 19 additions & 0 deletions src/main/java/com/fauna/codec/CodecProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,27 @@

import java.lang.reflect.Type;

/**
* Interface for providing codecs, responsible for obtaining codecs for specific classes and types.
*/
public interface CodecProvider {

/**
* Retrieves a codec for the specified class type.
*
* @param clazz The class type for which to obtain a codec.
* @param <T> The type of the class.
* @return A codec capable of serializing and deserializing instances of the specified class.
*/
<T> Codec<T> get(Class<T> clazz);

/**
* Retrieves a codec for the specified class type, with additional type arguments for generic classes.
*
* @param clazz The class type for which to obtain a codec.
* @param typeArgs The generic type arguments, if applicable.
* @param <T> The type of the class.
* @return A codec capable of serializing and deserializing instances of the specified class with the provided type arguments.
*/
<T> Codec<T> get(Class<T> clazz, Type[] typeArgs);
}
25 changes: 25 additions & 0 deletions src/main/java/com/fauna/codec/CodecRegistry.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package com.fauna.codec;

/**
* Interface defining a registry for codecs, which manage the serialization and deserialization of objects.
* <p>
* Provides methods for storing, retrieving, and checking for codecs by their unique keys.
*/
public interface CodecRegistry {

/**
* Retrieves the codec associated with the specified key.
*
* @param key The unique key representing the codec.
* @param <T> The type of the object handled by the codec.
* @return The codec associated with the specified key, or {@code null} if not found.
*/
<T> Codec<T> get(CodecRegistryKey key);

/**
* Registers a codec with the specified key in the registry.
*
* @param key The unique key representing the codec.
* @param codec The codec to register.
* @param <T> The type of the object handled by the codec.
*/
<T> void put(CodecRegistryKey key, Codec<T> codec);

/**
* Checks if a codec is registered under the specified key.
*
* @param key The unique key representing the codec.
* @return {@code true} if a codec exists for the specified key; {@code false} otherwise.
*/
boolean contains(CodecRegistryKey key);
}
47 changes: 42 additions & 5 deletions src/main/java/com/fauna/codec/CodecRegistryKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,57 @@
import java.util.Arrays;
import java.util.Objects;

/**
* Represents a unique key in the codec registry.
*/
public class CodecRegistryKey {
private final Class<?> base;
private final Type[] typeArgs;

public <T> CodecRegistryKey(Class<T> clazz, Type[] typeArgs) {
base = clazz;
/**
* Constructs a new {@code CodecRegistryKey} for the specified class and type arguments.
*
* @param clazz The base class of the codec.
* @param typeArgs The type arguments for generic types, if applicable.
* @param <T> The type of the base class.
*/
public <T> CodecRegistryKey(final Class<T> clazz, final Type[] typeArgs) {
this.base = clazz;
this.typeArgs = typeArgs;
}

public static <T> CodecRegistryKey from(Class<T> clazz) {
/**
* Creates a {@code CodecRegistryKey} for the specified class, without any type arguments.
*
* @param clazz The base class of the codec.
* @param <T> The type of the base class.
* @return A new {@code CodecRegistryKey} instance.
*/
public static <T> CodecRegistryKey from(final Class<T> clazz) {
return new CodecRegistryKey(clazz, null);
}

public static <T> CodecRegistryKey from(Class<T> clazz, Type[] typeArgs) {
/**
* Creates a {@code CodecRegistryKey} for the specified class and type arguments.
*
* @param clazz The base class of the codec.
* @param typeArgs The type arguments for generic types.
* @param <T> The type of the base class.
* @return A new {@code CodecRegistryKey} instance.
*/
public static <T> CodecRegistryKey from(final Class<T> clazz, final Type[] typeArgs) {
return new CodecRegistryKey(clazz, typeArgs);
}

/**
* Compares this key with another object for equality, based on the base class and type arguments.
*
* @param other The object to compare with this key.
* @return {@code true} if the other object is a {@code CodecRegistryKey} with the same base class and type arguments;
* {@code false} otherwise.
*/
@Override
public boolean equals(Object other) {
public boolean equals(final Object other) {
if (other == this) {
return true;
} else if (other instanceof CodecRegistryKey) {
Expand All @@ -34,6 +66,11 @@ public boolean equals(Object other) {
}
}

/**
* Returns a hash code for this key, based on the base class and type arguments.
*
* @return The hash code for this {@code CodecRegistryKey}.
*/
@Override
public final int hashCode() {
return Objects.hash(base, Arrays.hashCode(typeArgs));
Expand Down
83 changes: 54 additions & 29 deletions src/main/java/com/fauna/codec/DefaultCodecProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,37 @@
import java.util.Map;
import java.util.Optional;

public class DefaultCodecProvider implements CodecProvider {
/**
* Provides codecs for serialization and deserialization of various data types in Fauna.
* <p>
* This provider supports codecs for primitive types, collections, optional values, documents, enums, and more.
* </p>
*/
public final class DefaultCodecProvider implements CodecProvider {

private final CodecRegistry registry;

/**
* Singleton instance of the {@code DefaultCodecProvider} for global access.
*/
public static final CodecProvider SINGLETON =
new DefaultCodecProvider(DefaultCodecRegistry.SINGLETON);

public DefaultCodecProvider(CodecRegistry registry) {
registry.put(CodecRegistryKey.from(Object.class),
new DynamicCodec(this));
/**
* Initializes a new instance of {@code DefaultCodecProvider} with a specified registry.
*
* @param registry The codec registry to store generated codecs.
*/
public DefaultCodecProvider(final CodecRegistry registry) {
registry.put(CodecRegistryKey.from(Object.class), new DynamicCodec(this));

registry.put(CodecRegistryKey.from(Query.class), new QueryCodec(this));
registry.put(CodecRegistryKey.from(QueryObj.class),
new QueryObjCodec(this));
registry.put(CodecRegistryKey.from(QueryArr.class),
new QueryArrCodec(this));
registry.put(CodecRegistryKey.from(QueryVal.class),
new QueryValCodec(this));
registry.put(CodecRegistryKey.from(QueryLiteral.class),
new QueryLiteralCodec());

registry.put(CodecRegistryKey.from(EventSourceResponse.class),
new EventSourceResponseCodec());
registry.put(CodecRegistryKey.from(QueryObj.class), new QueryObjCodec(this));
registry.put(CodecRegistryKey.from(QueryArr.class), new QueryArrCodec(this));
registry.put(CodecRegistryKey.from(QueryVal.class), new QueryValCodec(this));
registry.put(CodecRegistryKey.from(QueryLiteral.class), new QueryLiteralCodec());

registry.put(CodecRegistryKey.from(EventSourceResponse.class), new EventSourceResponseCodec());

var bdc = new BaseDocumentCodec(this);
registry.put(CodecRegistryKey.from(BaseDocument.class), bdc);
Expand All @@ -65,12 +72,27 @@ public DefaultCodecProvider(CodecRegistry registry) {
this.registry = registry;
}

public <T> Codec<T> get(Class<T> clazz) {
/**
* Retrieves the codec for the specified class type.
*
* @param clazz The class for which a codec is requested.
* @param <T> The data type to be encoded or decoded.
* @return The {@link Codec} associated with the class.
*/
public <T> Codec<T> get(final Class<T> clazz) {
return get(clazz, null);
}

/**
* Retrieves the codec for the specified class type and type arguments.
*
* @param clazz The class for which a codec is requested.
* @param typeArgs The type arguments for generic classes.
* @param <T> The data type to be encoded or decoded.
* @return The {@link Codec} associated with the class and type arguments.
*/
@Override
public <T> Codec<T> get(Class<T> clazz, Type[] typeArgs) {
public <T> Codec<T> get(final Class<T> clazz, final Type[] typeArgs) {
CodecRegistryKey key = CodecRegistryKey.from(clazz, typeArgs);

if (!registry.contains(key)) {
Expand All @@ -81,30 +103,34 @@ public <T> Codec<T> get(Class<T> clazz, Type[] typeArgs) {
return registry.get(key);
}

/**
* Generates a codec for the specified class type and type arguments if not already available.
*
* @param clazz The class for which a codec needs to be generated.
* @param typeArgs The type arguments for generic classes.
* @param <T> The data type to be encoded or decoded.
* @param <E> The element type for collection codecs.
* @return The generated {@link Codec} for the class and type arguments.
*/
@SuppressWarnings({"unchecked"})
private <T, E> Codec<T> generate(Class<T> clazz, Type[] typeArgs) {
private <T, E> Codec<T> generate(final Class<T> clazz, final Type[] typeArgs) {
if (Map.class.isAssignableFrom(clazz)) {
var ta = typeArgs == null || typeArgs.length <= 1 ? Object.class :
typeArgs[1];
var ta = typeArgs == null || typeArgs.length <= 1 ? Object.class : typeArgs[1];
Codec<?> valueCodec = this.get((Class<?>) ta, null);

return (Codec<T>) new MapCodec<E, Map<String, E>>(
(Codec<E>) valueCodec);
return (Codec<T>) new MapCodec<E, Map<String, E>>((Codec<E>) valueCodec);
}

var ta = typeArgs == null || typeArgs.length == 0 ? Object.class :
typeArgs[0];
var ta = typeArgs == null || typeArgs.length == 0 ? Object.class : typeArgs[0];

if (List.class.isAssignableFrom(clazz)) {
Codec<?> elemCodec = this.get((Class<?>) ta, null);

return (Codec<T>) new ListCodec<E, List<E>>((Codec<E>) elemCodec);
}

if (clazz == Optional.class) {
Codec<?> valueCodec = this.get((Class<?>) ta, null);
return (Codec<T>) new OptionalCodec<E, Optional<E>>(
(Codec<E>) valueCodec);
return (Codec<T>) new OptionalCodec<E, Optional<E>>((Codec<E>) valueCodec);
}

if (clazz == Page.class) {
Expand All @@ -114,8 +140,7 @@ private <T, E> Codec<T> generate(Class<T> clazz, Type[] typeArgs) {

if (clazz == NullableDocument.class) {
Codec<?> valueCodec = this.get((Class<?>) ta, null);
return (Codec<T>) new NullableDocumentCodec<E, NullableDocument<E>>(
(Codec<E>) valueCodec);
return (Codec<T>) new NullableDocumentCodec<E, NullableDocument<E>>((Codec<E>) valueCodec);
}

if (clazz.isEnum()) {
Expand Down
Loading

0 comments on commit 9a73c93

Please sign in to comment.