Interface Decoder<T extends @UnknownNullability Object>

Type Parameters:
T - the value type
All Known Subinterfaces:
Codec<T>, DataComponent<T>, StructCodec<R>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Decoder<T extends @UnknownNullability Object>
Decoders are interfaces used in Codec which purpose is to decode any value of T with a transcoder.
For example:
record Name(String imTheBoss) { }
Decoder<Name> decoder = new Decoder<>() {
    @Override
    public <D> Result<Name> decode(Transcoder<D> coder, D value) {
        return coder.getString(value).mapResult(Name::new);
    }
};
Result<Name> result = decoder.decode(Transcoder.NBT, StringBinaryTag.stringBinaryTag("me")); // Result.OK(Name("me"))
Result<Name> errorResult = decoder.decode(Transcoder.NBT, EndBinaryTag.endBinaryTag()); // Result.Error(...)
  • Method Details

    • unit

      static <T> Decoder<T> unit(T value)
      Returns a unit decoder of T
      Type Parameters:
      T - the type of value
      Parameters:
      value - the value to always return
      Returns:
      the unit decoder
    • decode

      <D> Result<T> decode(Transcoder<D> coder, D value)
      Decodes a value of D using the specific Transcoder
      The Result will be of Result.Ok or Result.Error and its typed T
      Type Parameters:
      D - The value type
      Parameters:
      coder - the transcoder to use
      value - the value to decode
      Returns:
      the Result of the encoding with its type determined by the transcoder