- Type Parameters:
C
- The type of the named value
This codec uses another codec to encode and decode the value.
The named codec is used to encode and decode values with a specific name.
The name is used to identify the value in a data structure.
Optionally, aliases can be configured to identify the value if the name is not present.
The current value in encodeStart(TypeProvider, Object, Object)
must be a data structure that can hold the named value.
If the current value is not a data structure, the codec will return an error.
The value in decodeStart(TypeProvider, Object)
must be a data structure that contains the named value.
If the value is not a data structure or the named value is not present, the codec will return an error.
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe aliases of the value.The codec used to encode and decode the named value.private final String
The name of the value.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
ConstructorsConstructorDescriptionNamedCodec
(@NotNull Codec<C> codec, @NotNull String name, String @NotNull ... aliases) Constructs a new named codec using the given codec and name for the value. -
Method Summary
Modifier and TypeMethodDescriptiondecodeStart
(@NotNull TypeProvider<R> provider, R map) Decodes the value of the specified type and returns the result.
The result contains the decoded value or an error message.decodeStartWithAlias
(@NotNull TypeProvider<R> provider, R map, @NotNull String error) Decodes the value using the given provider and map.
This method will try to decode the value by the first alias which is present in the map.
If no alias is present, an error will be returned.
The result will contain the decoded value or an error message.<R> @NotNull Result
<R> encodeStart
(@NotNull TypeProvider<R> provider, R map, C value) Encodes the value of the specified type and returns the result.
The result contains the encoded value or an error message.boolean
getDecodeName
(@NotNull TypeProvider<R> provider, R map) Gets the first alias which is present in the given map or an error.
The result contains the name or an error message.int
hashCode()
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, orElse, orElseGet, 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 named value. -
name
The name of the value. -
aliases
The aliases of the value.
-
-
Constructor Details
-
NamedCodec
NamedCodec(@NotNull @NotNull Codec<C> codec, @NotNull @NotNull String name, String @NotNull ... aliases) Constructs a new named codec using the given codec and name for the value.- Parameters:
codec
- The codec used toname
- The name of the valuealiases
- The aliases of the value- Throws:
NullPointerException
- If the codec or name is null
-
-
Method Details
-
encodeStart
@NotNull public <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R map, @Nullable 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 providermap
- The current valuevalue
- The value to encode- Returns:
- The result
-
decodeStart
@NotNull public <R> @NotNull Result<C> decodeStart(@NotNull @NotNull TypeProvider<R> provider, @Nullable R map) 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 providermap
- The value to decode- Returns:
- The result
-
decodeStartWithAlias
@NotNull private <R> @NotNull Result<C> decodeStartWithAlias(@NotNull @NotNull TypeProvider<R> provider, @NotNull R map, @NotNull @NotNull String error) Decodes the value using the given provider and map.
This method will try to decode the value by the first alias which is present in the map.
If no alias is present, an error will be returned.
The result will contain the decoded value or an error message.- Type Parameters:
R
- The type to decode from- Parameters:
provider
- The type providermap
- The map to decode the value fromerror
- The actual error message which occurred during decoding- Returns:
- The result
- Throws:
NullPointerException
- If the provider, map or error is null
-
getDecodeName
@NotNull private <R> @NotNull Result<String> getDecodeName(@NotNull @NotNull TypeProvider<R> provider, @NotNull R map) Gets the first alias which is present in the given map or an error.
The result contains the name or an error message.- Type Parameters:
R
- The type to decode from- Parameters:
provider
- The type providermap
- The map to decode the value from- Returns:
- The result
- Throws:
NullPointerException
- If the provider or map is null
-
equals
-
hashCode
public int hashCode() -
toString
-