Class NamedCodec<C>

java.lang.Object
net.luis.utils.io.codec.NamedCodec<C>
Type Parameters:
C - The type of the named value
All Implemented Interfaces:
Codec<C>, Decoder<C>, Encoder<C>

public class NamedCodec<C> extends Object implements Codec<C>
A codec for encoding and decoding named values.
This codec uses another codec to encode and decode the value.

The named codec is used to encode and decode values with a specific name.
The name is used to identify the value in a data structure.
Optionally, aliases can be configured to identify the value if the name is not present.

The current value in encodeStart(TypeProvider, Object, Object) must be a data structure that can hold the named value.
If the current value is not a data structure, the codec will return an error.

The value in decodeStart(TypeProvider, Object) must be a data structure that contains the named value.
If the value is not a data structure or the named value is not present, the codec will return an error.

  • Field Details

    • codec

      private final Codec<C> codec
      The codec used to encode and decode the named value.
    • name

      private final String name
      The name of the value.
    • aliases

      private final Set<String> aliases
      The aliases of the value.
  • Constructor Details

    • NamedCodec

      NamedCodec(@NotNull @NotNull Codec<C> codec, @NotNull @NotNull String name, String @NotNull ... aliases)
      Constructs a new named codec using the given codec and name for the value.
      Parameters:
      codec - The codec used to
      name - The name of the value
      aliases - The aliases of the value
      Throws:
      NullPointerException - If the codec or name is null
  • Method Details

    • encodeStart

      @NotNull public <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R map, @Nullable C value)
      Description copied from interface: Encoder
      Encodes the value of the specified type and returns the result.
      The result contains the encoded value or an error message.

      The current value is the value that is currently encoded.
      In the most cases this value should be equal to TypeProvider.empty().
      In the case of encoding a value that is part of a bigger structure, the current value should be the structure.

      Specified by:
      encodeStart in interface Encoder<C>
      Type Parameters:
      R - The type to encode to
      Parameters:
      provider - The type provider
      map - The current value
      value - The value to encode
      Returns:
      The result
    • decodeStart

      @NotNull public <R> @NotNull Result<C> decodeStart(@NotNull @NotNull TypeProvider<R> provider, @Nullable R map)
      Description copied from interface: Decoder
      Decodes the value of the specified type and returns the result.
      The result contains the decoded value or an error message.
      Specified by:
      decodeStart in interface Decoder<C>
      Type Parameters:
      R - The type to decode from
      Parameters:
      provider - The type provider
      map - The value to decode
      Returns:
      The result
    • decodeStartWithAlias

      @NotNull private <R> @NotNull Result<C> decodeStartWithAlias(@NotNull @NotNull TypeProvider<R> provider, @NotNull R map, @NotNull @NotNull String error)
      Decodes the value using the given provider and map.
      This method will try to decode the value by the first alias which is present in the map.
      If no alias is present, an error will be returned.
      The result will contain the decoded value or an error message.
      Type Parameters:
      R - The type to decode from
      Parameters:
      provider - The type provider
      map - The map to decode the value from
      error - The actual error message which occurred during decoding
      Returns:
      The result
      Throws:
      NullPointerException - If the provider, map or error is null
    • getDecodeName

      @NotNull private <R> @NotNull Result<String> getDecodeName(@NotNull @NotNull TypeProvider<R> provider, @NotNull R map)
      Gets the first alias which is present in the given map or an error.
      The result contains the name or an error message.
      Type Parameters:
      R - The type to decode from
      Parameters:
      provider - The type provider
      map - The map to decode the value from
      Returns:
      The result
      Throws:
      NullPointerException - If the provider or map is null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object