java.lang.Object
net.luis.utils.io.codec.struct.OptionalCodec<C>
- Type Parameters:
C
- The type of the optional value
A codec for encoding and decoding optional values.
This codec uses another codec to encode and decode the optional value.
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 Summary
FieldsModifier and TypeFieldDescriptionThe codec used to encode and decode the optional value.The supplier used to provide the default value if the optional value is empty.
Can be null if no default value is provided.Fields inherited from interface net.luis.utils.io.codec.Codec
BOOLEAN, BYTE, BYTE_ARRAY, CHARSET, DOUBLE, DOUBLE_STREAM, FILE, FLOAT, INT_STREAM, INTEGER, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, LONG, LONG_STREAM, PATH, SHORT, STRING, URI, URL, UUID, ZONED_DATE_TIME
-
Constructor Summary
ConstructorsConstructorDescriptionOptionalCodec
(@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 inCodec
instead. -
Method Summary
Modifier and TypeMethodDescriptiondecodeStart
(@NotNull TypeProvider<R> provider, R value) Decodes the value of the specified type and returns the result.
The result contains the decoded value or an error message.<R> @NotNull Result
<R> encodeStart
(@NotNull TypeProvider<R> provider, R current, @Nullable Optional<C> value) Encodes the value of the specified type and returns the result.
The result contains the encoded value or an error message.boolean
Gets the default value for this codec.
If no default value is provided, an empty optional is returned.int
hashCode()
Creates a new codec that will return the given default value in an error case during decoding.orElseFlat
(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.Creates a new codec that will return the value provided by the given supplier in an error case during decoding.orElseGetFlat
(@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.toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.luis.utils.io.codec.Codec
configure, getter, keyable, list, list, list, map, mapFlat, named, noneEmptyList, optional, stream, validate, withAlternative, xmap
Methods inherited from interface net.luis.utils.io.codec.decoder.Decoder
decode, mapDecoder
Methods inherited from interface net.luis.utils.io.codec.encoder.Encoder
encode, encode, mapEncoder
-
Field Details
-
codec
The codec used to encode and decode the optional value. -
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
Constructs a new optional codec using the given codec for the optional value.
Do not use this constructor directly, use the optional factory methods inCodec
instead.- Parameters:
codec
- The codec for the optional value- Throws:
NullPointerException
- If the codec is null
-
-
Method Details
-
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 toTypeProvider.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 interfaceEncoder<C>
- Type Parameters:
R
- The type to encode to- Parameters:
provider
- The type providercurrent
- The current valuevalue
- 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 interfaceDecoder<C>
- Type Parameters:
R
- The type to decode from- Parameters:
provider
- The type providervalue
- The value to decode- Returns:
- The result
-
orElse
Description copied from interface:Codec
Creates a new codec that will return the given default value in an error case during decoding. -
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. -
orElseFlat
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
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
-
hashCode
public int hashCode() -
toString
-