Class ListCodec<C>

java.lang.Object
net.luis.utils.io.codec.struct.ListCodec<C>
Type Parameters:
C - The type of elements in the list
All Implemented Interfaces:
Codec<List<C>>, Decoder<List<C>>, Encoder<List<C>>

public class ListCodec<C> extends Object implements Codec<List<C>>
A codec for encoding and decoding lists of elements.
This codec uses another codec to encode and decode the elements of the list.

The list codec can be sized to only accept lists of a certain size range.
The minimum size and maximum size are inclusive.
If the list size is out of range, the codec will return an error during encoding or decoding.

  • Field Details

    • codec

      private final Codec<C> codec
      The codec used to encode and decode the elements of the list.
    • minSize

      private final int minSize
      The minimum size of the list (inclusive).
    • maxSize

      private final int maxSize
      The maximum size of the list (inclusive).
  • Constructor Details

    • ListCodec

      @Internal public ListCodec(@NotNull @NotNull Codec<C> codec)
      Constructs a new list codec using the given codec for the elements.
      The list codec will accept lists of any size.
      Do not use this constructor directly, use any of the list factory methods in Codec instead.
      Parameters:
      codec - The codec for the elements
      Throws:
      NullPointerException - If the codec is null
    • ListCodec

      @Internal public ListCodec(@NotNull @NotNull Codec<C> codec, int minSize, int maxSize)
      Constructs a new list codec using the given codec for the elements and the size range of the list.
      Do not use this constructor directly, use any of the list factory methods in Codec instead.
      Parameters:
      codec - The codec for the elements
      minSize - The minimum size of the list (inclusive)
      maxSize - The maximum size of the list (inclusive)
      Throws:
      NullPointerException - If the codec is null
      IllegalArgumentException - If the minimum size is less than zero or greater than the maximum size
  • Method Details

    • encodeStart

      @NotNull public <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R current, @Nullable @Nullable List<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
      current - The current value
      value - The value to encode
      Returns:
      The result
    • decodeStart

      @NotNull public <R> @NotNull Result<List<C>> decodeStart(@NotNull @NotNull TypeProvider<R> provider, @Nullable R value)
      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
      value - The value to decode
      Returns:
      The result
    • 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