Interface Encoder<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.
Encoders are interfaces used in
For example:
Codec which purpose is to encode any value of T
with a transcoder.
For example:
record Name(String imTheBoss) { }
Encoder<Name> encoder = new Encoder<>() {
@Override
public <D> Result<D> encode(Transcoder<D> coder, @Nullable Name value) {
if (value == null) return new Result.Error<>("null");
return new Result.Ok<>(coder.createString(value.imTheBoss()));
}
};
Result<BinaryTag> result = encoder.encode(Transcoder.NBT, new Name("me")); // Result.OK(StringBinaryTag("me"))
Result<BinaryTag> errorResult = encoder.encode(Transcoder.NBT, null); // Result.Error("null")
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Encoder<T> empty()Creates an empty encoder that only encodes null<D> Result<D> encode(Transcoder<D> coder, T value) Encodes a value ofTusing the specificTranscoder
TheResultwill be ofResult.OkorResult.Errorand its typedD
-
Method Details
-
empty
Creates an empty encoder that only encodes null- Type Parameters:
T- the encoder type- Returns:
- the empty encoder
-
encode
Encodes a value ofTusing the specificTranscoder
TheResultwill be ofResult.OkorResult.Errorand its typedD- Type Parameters:
D- The resultant type- Parameters:
coder- the transcoder to usevalue- the value to encode- Returns:
- the
Resultof the encoding with its type determined by the transcoder
-