Class OptionalCodec<C>

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

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

The optional codec can be set to provide a default value if the optional value is empty.
The default value is provided by a supplier.

If the optional value is empty during encoding the current value is returned.
This means that the value will not be appended to a data structure.

If the optional value is empty during decoding the default value is returned.

  • Field Details

    • codec

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

      @Nullable private @Nullable Supplier<Optional<C>> defaultProvider
      The supplier used to provide the default value if the optional value is empty.
      Can be null if no default value is provided.
  • Constructor Details

    • OptionalCodec

      @Internal public OptionalCodec(@NotNull @NotNull Codec<C> codec)
      Constructs a new optional codec using the given codec for the optional value.
      Do not use this constructor directly, use the optional factory methods in Codec instead.
      Parameters:
      codec - The codec for the optional value
      Throws:
      NullPointerException - If the codec is null
  • Method Details

    • getDefault

      @NotNull private @NotNull Optional<C> getDefault()
      Gets the default value for this codec.
      If no default value is provided, an empty optional is returned.
      Returns:
      The default value
    • encodeStart

      @NotNull public <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R current, @Nullable @Nullable Optional<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<Optional<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
    • orElse

      @NotNull public @NotNull Codec<Optional<C>> orElse(@Nullable @Nullable Optional<C> defaultValue)
      Description copied from interface: Codec
      Creates a new codec that will return the given default value in an error case during decoding.
      Specified by:
      orElse in interface Codec<C>
      Parameters:
      defaultValue - The default value
      Returns:
      A new codec
      See Also:
    • orElseGet

      @NotNull public @NotNull Codec<Optional<C>> orElseGet(@NotNull @NotNull Supplier<Optional<C>> supplier)
      Description copied from interface: Codec
      Creates a new codec that will return the value provided by the given supplier in an error case during decoding.
      Specified by:
      orElseGet in interface Codec<C>
      Parameters:
      supplier - The default value supplier
      Returns:
      A new codec
    • orElseFlat

      @NotNull public @NotNull Codec<C> orElseFlat(@Nullable C defaultValue)
      Returns a new codec that is not optional.
      The new codec will use the given default value if the optional value is empty.
      Parameters:
      defaultValue - The default value
      Returns:
      The new codec
    • orElseGetFlat

      @NotNull public @NotNull Codec<C> orElseGetFlat(@NotNull @NotNull Supplier<C> supplier)
      Returns a new codec that is not optional.
      The new codec will use the default value provided by the given supplier if the optional value is empty.
      Parameters:
      supplier - The supplier for the default value
      Returns:
      The new codec
      Throws:
      NullPointerException - If the supplier 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