Class UnitCodec<C>

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

public class UnitCodec<C> extends Object implements Codec<C>
A codec for encoding and decoding unit values.
This codec does not encode or decode any value, it is used to represent a unit value.

The unit codec can be used to represent a value that is not encoded or decoded.
This is useful when a codec is required to encode or decode a value, but the value is not needed.

The unit codec will always return the same value when encoding and decoding.
During encoding, the current value is returned.
During decoding, the unit value is returned which is created by a supplier.

  • Field Details

    • supplier

      private final Supplier<C> supplier
      The supplier used to create the unit value.
  • Constructor Details

    • UnitCodec

      @Internal public UnitCodec(@NotNull @NotNull Supplier<C> supplier)
      Constructs a new unit codec using the given supplier for the unit value.
      Do not use this constructor directly, use any of the unit factory methods in Codec instead.
      Parameters:
      supplier - The supplier used to create the unit value
      Throws:
      NullPointerException - If the supplier is null
  • Method Details

    • encodeStart

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

      @NotNull public <R> @NotNull Result<C> decodeStart(@Nullable @Nullable 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