Class EitherCodec<F,S>

java.lang.Object
net.luis.utils.io.codec.struct.EitherCodec<F,S>
Type Parameters:
F - The type of the first value
S - The type of the second value
All Implemented Interfaces:
Codec<Either<F,S>>, Decoder<Either<F,S>>, Encoder<Either<F,S>>

public class EitherCodec<F,S> extends Object implements Codec<Either<F,S>>
A codec for encoding and decoding values of type Either.
This codec uses two other codecs to encode and decode the values of the first and second type.

The first codec is used to encode and decode the values of the first type.
The second codec is used to encode and decode the values of the second type.

The first codec is decoded first and if it fails, the second codec is used to decode the value.
Note: If the first codec is a string codec, it will always succeed, so the second codec will never be used.

  • Field Details

    • firstCodec

      private final Codec<F> firstCodec
      The codec used to encode and decode the values of the first type.
    • secondCodec

      private final Codec<S> secondCodec
      The codec used to encode and decode the values of the second type.
  • Constructor Details

    • EitherCodec

      @Internal public EitherCodec(@NotNull @NotNull Codec<F> firstCodec, @NotNull @NotNull Codec<S> secondCodec)
      Constructs a new either codec using the given codecs for the first and second type.
      Do not use this constructor directly, use any of the either factory methods in Codec instead.
      Parameters:
      firstCodec - The first codec
      secondCodec - The second codec
      Throws:
      NullPointerException - If the first or second codec is null
  • Method Details

    • encodeStart

      @NotNull public <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R current, @Nullable @Nullable Either<F,S> 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<F>
      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<Either<F,S>> 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<F>
      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