- Type Parameters:
C
- The type of the value that is encoded and decoded by this codec
- All Known Subinterfaces:
KeyableCodec<C>
- All Known Implementing Classes:
EitherCodec
,ListCodec
,MapCodec
,NamedCodec
,OptionalCodec
,RangeCodec
,UnitCodec
It is used to encode and decode values of a specific type.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionA codec that encodes and decodes boolean values.static final RangeCodec
<Byte> A range codec that encodes and decodes byte values.static final Codec
<byte[]> A codec that encodes and decodes byte arrays.
The underlying byte array is converted to and from a list of bytes.A codec that encodes and decodescharsets
.
The underlying charset is converted to and from a string.static final RangeCodec
<Double> A range codec that encodes and decodes double values.static final Codec
<DoubleStream> A codec that encodes and decodesdouble streams
.
The underlying double stream is converted to and from aStream
of doubles.static final RangeCodec
<Float> A range codec that encodes and decodes float values.A codec that encodes and decodesint streams
.
The underlying int stream is converted to and from aStream
of integers.static final RangeCodec
<Integer> A range codec that encodes and decodes integer values.A codec that encodes and decodeslocal date
values.
The underlying local date is converted to and from a string.static final Codec
<LocalDateTime> A codec that encodes and decodeslocal date time
values.
The underlying local date time is converted to and from a string.A codec that encodes and decodeslocal time
values.
The underlying local time is converted to and from a string.static final RangeCodec
<Long> A range codec that encodes and decodes long values.static final Codec
<LongStream> A codec that encodes and decodeslong streams
.
The underlying long stream is converted to and from aStream
of longs.static final RangeCodec
<Short> A range codec that encodes and decodes short values.static final KeyableCodec
<String> A codec that encodes and decodes strings.static final KeyableCodec
<UUID> A keyable codec that encodes and decodesUUIDs
.
The underlying UUID is converted to and from a string.static final Codec
<ZonedDateTime> A codec that encodes and decodeszoned date time
values.
The underlying zoned date time is converted to and from a string. -
Method Summary
Modifier and TypeMethodDescriptionWraps the current codec into a new codec with the given codec name.default <O> @NotNull ConfiguredCodec
<C, O> Creates a new configured codec.
This method combines thenamed(String, String...)
andgetter(Function)
methods.static <E extends Enum<E>>
@NotNull KeyableCodec<E> dynamicEnum
(@NotNull Class<E> clazz) Creates a new keyable codec for an enum class.
The enum is encoded using the name of the enum value.
The decoder supports both the name and the ordinal value of the enum.Creates a new either codec for the given codecs.
The value which is encoded and decoded by this codec can be either of typeF
orS
.
The codec will try to use the first codec to encode and decode the value,
if that fails, it will try to use the second codec.static <E extends Enum<E>>
@NotNull KeyableCodec<E> Creates a new keyable codec for the given enum class.
The enum value is encoded and decoded using its name.static <E extends Enum<E>>
@NotNull KeyableCodec<E> enumOrdinal
(@NotNull Class<E> clazz) Creates a new keyable codec for the given enum class.
The enum value is encoded and decoded using its ordinal value.static <E extends Enum<E>>
@NotNull KeyableCodec<E> friendlyEnumName
(@NotNull Function<E, String> friendlyEncoder, @NotNull Function<String, E> friendlyDecoder) Creates a new keyable codec for an enum class.
The enum value is encoded and decoded using a friendly name.default <O> @NotNull ConfiguredCodec
<C, O> Creates a new configured codec.
The configured codec is used in the codec builder to create codecs for complex data structures.
The configured codec encodes and decodes components of the data structure using the given getter function.
It is expected that this is only called on named codecs.default @NotNull KeyableCodec
<C> Wraps the current codec into a new keyable codec using the given key encoder and key decoder.
The key encoder and key decoder are defined as functions that convert keys of the type C to and from strings.
If the key decoder is unable to decode a key, it should return null.static <C> @NotNull KeyableCodec
<C> keyable
(@NotNull Codec<C> codec, @NotNull Function<C, String> keyEncoder, @NotNull Function<String, @Nullable C> keyDecoder) Creates a new keyable codec for the given codec using the given key encoder and key decoder.
The key encoder and key decoder are defined as functions that convert keys of the typeC
to and from strings.
If the key decoder is unable to decode a key, it should return null.static <C> @NotNull KeyableCodec
<C> Creates a new keyable codec for the given codec using the given key encoder and key decoder.
The key encoder is the java built-intoString()
method.
The key decoder is defined as function that converts keys from strings to values of the typeC
.
If the key decoder is unable to decode a key, it should return null.static <C> @NotNull KeyableCodec
<C> keyable
(@NotNull Codec<C> codec, @NotNull KeyableEncoder<C> encoder, @NotNull KeyableDecoder<C> decoder) Creates a new keyable codec for the given codec using the given key encoder and key decoder.list()
Creates a new list codec uses the current codec as element codec for the list codec.
The created list codec has no size restrictions.list
(int maxSize) Creates a new list codec uses the current codec as element codec for the list codec with the given maximum size.
The list must have at most the maximum size after encoding and decoding.list
(int minSize, int maxSize) Creates a new list codec uses the current codec as element codec for the list codec with the given minimum and maximum size.
The list must have at least the minimum size and at most the maximum size after encoding and decoding.Creates a new list codec for the given codec.
The created list codec has no size restrictions.Creates a new list codec for the given codec and maximum size.
The list must have at most the maximum size after encoding and decoding.Creates a new list codec for the given codec and minimum and maximum size.
The list must have at least the minimum size and at most the maximum size after encoding and decoding.Creates a new map codec withSTRING
as key codec and the given value codec.
The created map codec has no size restrictions.map
(@NotNull KeyableCodec<K> keyCodec, @NotNull Codec<V> valueCodec) Creates a new map codec with the given key codec and value codec.
The created map codec has no size restrictions.map
(@NotNull KeyableCodec<K> keyCodec, @NotNull Codec<V> valueCodec, int maxSize) Creates a new map codec with the given key codec and value codec and maximum size.
The map must have at most the maximum size after encoding and decoding.map
(@NotNull KeyableCodec<K> keyCodec, @NotNull Codec<V> valueCodec, int minSize, int maxSize) Creates a new map codec with the given key codec and value codec and minimum and maximum size.
The map must have at least the minimum size and at most the maximum size after encoding and decoding.default <O> @NotNull Codec
<O> map
(@NotNull ResultingFunction<O, C> to, @NotNull ResultMappingFunction<C, O> from) Creates a new mapped codec of typeO
from the current codec.
This mapping functions allows the handling of errors that occur during mapping.default <O> @NotNull Codec
<O> mapFlat
(@NotNull Function<O, C> to, @NotNull ResultMappingFunction<C, O> from) Creates a new mapped codec of typeO
from the current codec.Creates a new named codec with the current codec using the given name and aliases.Creates a new list codec uses the current codec as element codec for the list codec for non-empty lists.
The list must not be empty after encoding and decoding.noneEmptyList
(@NotNull Codec<C> codec) Creates a new list codec for the given codec for non-empty lists.
The list must not be empty after encoding and decoding.noneEmptyMap
(@NotNull KeyableCodec<K> keyCodec, @NotNull Codec<V> valueCodec) Creates a new map codec with the given key codec and value codec for non-empty maps.
The map must not be empty after encoding and decoding.static @NotNull KeyableCodec
<String> Creates a new keyable string codec for non-empty strings.
The string must not be empty after encoding and decoding.static <C> @NotNull Codec
<C> Creates a new codec from the given encoder and decoder with the given name.optional()
Wraps the current codec into a new optional codec.Creates a new optional codec for the given codec.Creates a new codec that will return the given default value in an error case during decoding.Creates a new codec that will return the value provided by the given supplier in an error case during decoding.stream()
Creates a new stream codec uses the current codec as element codec for the stream codec.Creates a new stream codec for the given codec.static @NotNull KeyableCodec
<String> string
(int length) Creates a new keyable string codec for the given maximum length.static @NotNull KeyableCodec
<String> string
(int minLength, int maxLength) Creates a new keyable string codec for the given minimum and maximum length.static <E> @NotNull Codec
<E> stringResolver
(@NotNull Function<E, String> stringEncoder, @NotNull Function<String, @Nullable E> stringDecoder) Creates a new codec that encodes and decodes values of the typeC
to and from strings.
The string encoder and decoder are defined as functions that convert values of the typeC
to and from strings.
If the decoder is unable to decode a string, it should return null.static <C> @NotNull Codec
<C> unit
(C value) Creates a new unit codec for the given value.static <C> @NotNull Codec
<C> Creates a new unit codec for the given supplier.Creates a new codec that will validate the result of the decoding process using the given validator function.
The validator function is applied to the decoded value and must return a result that contains the validated value or an error message.withAlternative
(@NotNull Codec<? extends C> alternative) Creates a new codec that uses the current codec as the main codec and the given codec as alternative codec.
If the main codec fails to encode or decode a value, the alternative codec is used.static <C> @NotNull Codec
<C> withAlternative
(@NotNull Codec<C> main, @NotNull Codec<? extends C> alternative) Creates a new codec that uses the given codec as the main codec and the given codec as alternative codec.
If the main codec fails to encode or decode a value, the alternative codec is used.default <O> @NotNull Codec
<O> Creates a new mapped codec of typeO
from the current codec.Methods inherited from interface net.luis.utils.io.codec.decoder.Decoder
decode, decodeStart, mapDecoder
Methods inherited from interface net.luis.utils.io.codec.encoder.Encoder
encode, encode, encodeStart, mapEncoder
-
Field Details
-
BOOLEAN
A codec that encodes and decodes boolean values. -
BYTE
A range codec that encodes and decodes byte values. -
SHORT
A range codec that encodes and decodes short values. -
INTEGER
A range codec that encodes and decodes integer values. -
LONG
A range codec that encodes and decodes long values. -
FLOAT
A range codec that encodes and decodes float values. -
DOUBLE
A range codec that encodes and decodes double values. -
STRING
A codec that encodes and decodes strings. -
BYTE_ARRAY
A codec that encodes and decodes byte arrays.
The underlying byte array is converted to and from a list of bytes. -
INT_STREAM
A codec that encodes and decodesint streams
.
The underlying int stream is converted to and from aStream
of integers. -
LONG_STREAM
A codec that encodes and decodeslong streams
.
The underlying long stream is converted to and from aStream
of longs. -
DOUBLE_STREAM
A codec that encodes and decodesdouble streams
.
The underlying double stream is converted to and from aStream
of doubles. -
UUID
A keyable codec that encodes and decodesUUIDs
.
The underlying UUID is converted to and from a string. -
LOCAL_TIME
A codec that encodes and decodeslocal time
values.
The underlying local time is converted to and from a string. -
LOCAL_DATE
A codec that encodes and decodeslocal date
values.
The underlying local date is converted to and from a string. -
LOCAL_DATE_TIME
A codec that encodes and decodeslocal date time
values.
The underlying local date time is converted to and from a string. -
ZONED_DATE_TIME
A codec that encodes and decodeszoned date time
values.
The underlying zoned date time is converted to and from a string. -
CHARSET
A codec that encodes and decodescharsets
.
The underlying charset is converted to and from a string. -
FILE
-
PATH
-
URI
-
URL
-
-
Method Details
-
of
@NotNull static <C> @NotNull Codec<C> of(@NotNull @NotNull Encoder<C> encoder, @NotNull @NotNull Decoder<C> decoder, @NotNull @NotNull String name) Creates a new codec from the given encoder and decoder with the given name.- Type Parameters:
C
- The type of the value that is encoded and decoded by the codec- Parameters:
encoder
- The encoder that is used to encode values of the typeC
decoder
- The decoder that is used to decode values of the typeC
name
- The name of the codec- Returns:
- A new codec
- Throws:
NullPointerException
- If the encoder, decoder or name is null
-
keyable
@NotNull static <C> @NotNull KeyableCodec<C> keyable(@NotNull @NotNull Codec<C> codec, @NotNull @NotNull Function<String, @Nullable C> keyDecoder) Creates a new keyable codec for the given codec using the given key encoder and key decoder.
The key encoder is the java built-intoString()
method.
The key decoder is defined as function that converts keys from strings to values of the typeC
.
If the key decoder is unable to decode a key, it should return null.- Type Parameters:
C
- The type of the value that is encoded and decoded by the codec- Parameters:
codec
- The base codec that is used to encode and decode values of the typeC
keyDecoder
- The key decoder- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the codec or key decoder is null- See Also:
-
keyable
@NotNull static <C> @NotNull KeyableCodec<C> keyable(@NotNull @NotNull Codec<C> codec, @NotNull @NotNull Function<C, String> keyEncoder, @NotNull @NotNull Function<String, @Nullable C> keyDecoder) Creates a new keyable codec for the given codec using the given key encoder and key decoder.
The key encoder and key decoder are defined as functions that convert keys of the typeC
to and from strings.
If the key decoder is unable to decode a key, it should return null.- Type Parameters:
C
- The type of the value that is encoded and decoded by the codec- Parameters:
codec
- The base codec that is used to encode and decode values of the typeC
keyEncoder
- The key encoderkeyDecoder
- The key decoder- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the codec, key encoder or key decoder is null- See Also:
-
keyable
@NotNull static <C> @NotNull KeyableCodec<C> keyable(@NotNull @NotNull Codec<C> codec, @NotNull @NotNull KeyableEncoder<C> encoder, @NotNull @NotNull KeyableDecoder<C> decoder) Creates a new keyable codec for the given codec using the given key encoder and key decoder.- Type Parameters:
C
- The type of the value that is encoded and decoded by the codec- Parameters:
codec
- The base codec that is used to encode and decode values of the typeC
encoder
- The key encoder that is used to encode keys of the typeC
decoder
- The key decoder that is used to decode keys of the typeC
- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the codec, key encoder or key decoder is null- See Also:
-
enumOrdinal
@NotNull static <E extends Enum<E>> @NotNull KeyableCodec<E> enumOrdinal(@NotNull @NotNull Class<E> clazz) Creates a new keyable codec for the given enum class.
The enum value is encoded and decoded using its ordinal value.- Type Parameters:
E
- The type of the enum- Parameters:
clazz
- The enum class- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the enum class is null- See Also:
-
enumName
@NotNull static <E extends Enum<E>> @NotNull KeyableCodec<E> enumName(@NotNull @NotNull Class<E> clazz) Creates a new keyable codec for the given enum class.
The enum value is encoded and decoded using its name.- Type Parameters:
E
- The type of the enum- Parameters:
clazz
- The enum class- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the enum class is null- See Also:
-
friendlyEnumName
@NotNull static <E extends Enum<E>> @NotNull KeyableCodec<E> friendlyEnumName(@NotNull @NotNull Function<E, String> friendlyEncoder, @NotNull @NotNull Function<String, E> friendlyDecoder) Creates a new keyable codec for an enum class.
The enum value is encoded and decoded using a friendly name.- Type Parameters:
E
- The type of the enum- Parameters:
friendlyEncoder
- The function that converts an enum value to a friendly namefriendlyDecoder
- The function that converts a friendly name to an enum value- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the friendly name encoder or decoder is null- See Also:
-
dynamicEnum
@NotNull static <E extends Enum<E>> @NotNull KeyableCodec<E> dynamicEnum(@NotNull @NotNull Class<E> clazz) Creates a new keyable codec for an enum class.
The enum is encoded using the name of the enum value.
The decoder supports both the name and the ordinal value of the enum.- Type Parameters:
E
- The type of the enum- Parameters:
clazz
- The enum class- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the enum class is null- See Also:
-
string
Creates a new keyable string codec for the given maximum length.- Parameters:
length
- The maximum length of the string (inclusive)- Returns:
- A new keyable string codec
- Throws:
IllegalArgumentException
- If the length is less than 0- See Also:
-
string
Creates a new keyable string codec for the given minimum and maximum length.- Parameters:
minLength
- The minimum length of the string (inclusive)maxLength
- The maximum length of the string (inclusive)- Returns:
- A new keyable string codec
- Throws:
IllegalArgumentException
- If the minimum length is less than zero or greater than the maximum length- See Also:
-
noneEmptyString
Creates a new keyable string codec for non-empty strings.
The string must not be empty after encoding and decoding.- Returns:
- A new keyable string codec
- See Also:
-
unit
Creates a new unit codec for the given value.- Type Parameters:
C
- The type of the value- Parameters:
value
- The value- Returns:
- A new unit codec
- See Also:
-
unit
Creates a new unit codec for the given supplier.- Type Parameters:
C
- The type of the value- Parameters:
supplier
- The supplier that provides the value- Returns:
- A new unit codec
- Throws:
NullPointerException
- If the supplier is null- See Also:
-
optional
Creates a new optional codec for the given codec.- Type Parameters:
C
- The type of the value that is encoded and decoded by the codec- Parameters:
codec
- The base codec- Returns:
- A new optional codec
- Throws:
NullPointerException
- If the codec is null- See Also:
-
list
Creates a new list codec for the given codec.
The created list codec has no size restrictions.- Type Parameters:
C
- The type of the list elements that are encoded and decoded by the codec- Parameters:
codec
- The base codec- Returns:
- A new list codec
- Throws:
NullPointerException
- If the codec is null- See Also:
-
list
Creates a new list codec for the given codec and maximum size.
The list must have at most the maximum size after encoding and decoding.- Type Parameters:
C
- The type of the list elements that are encoded and decoded by the codec- Parameters:
codec
- The base codecmaxSize
- The maximum size of the list (inclusive)- Returns:
- A new list codec
- Throws:
NullPointerException
- If the codec is nullIllegalArgumentException
- If the maximum size is less than 0- See Also:
-
list
@NotNull static <C> @NotNull Codec<List<C>> list(@NotNull @NotNull Codec<C> codec, int minSize, int maxSize) Creates a new list codec for the given codec and minimum and maximum size.
The list must have at least the minimum size and at most the maximum size after encoding and decoding.- Type Parameters:
C
- The type of the list elements that are encoded and decoded by the codec- Parameters:
codec
- The base codecminSize
- The minimum size of the list (inclusive)maxSize
- The maximum size of the list (inclusive)- Returns:
- A new list codec
- Throws:
NullPointerException
- If the codec is nullIllegalArgumentException
- If the minimum size is less than zero or greater than the maximum size- See Also:
-
noneEmptyList
Creates a new list codec for the given codec for non-empty lists.
The list must not be empty after encoding and decoding.- Type Parameters:
C
- The type of the list elements that are encoded and decoded by the codec- Parameters:
codec
- The base codec- Returns:
- A new list codec
- Throws:
NullPointerException
- If the codec is null- See Also:
-
stream
Creates a new stream codec for the given codec.- Type Parameters:
C
- The type of the stream elements that are encoded and decoded by the codec- Parameters:
codec
- The base codec- Returns:
- A new stream codec
- Throws:
NullPointerException
- If the codec is null
-
map
Creates a new map codec withSTRING
as key codec and the given value codec.
The created map codec has no size restrictions.- Type Parameters:
C
- The type of the map values that are encoded and decoded by the codec- Parameters:
valueCodec
- The value codec- Returns:
- A new map codec
- Throws:
NullPointerException
- If the value codec is null- See Also:
-
map
@NotNull static <K,V> @NotNull Codec<Map<K,V>> map(@NotNull @NotNull KeyableCodec<K> keyCodec, @NotNull @NotNull Codec<V> valueCodec) Creates a new map codec with the given key codec and value codec.
The created map codec has no size restrictions.- Type Parameters:
K
- The type of the map keys that are encoded and decoded by the codecV
- The type of the map values that are encoded and decoded by the codec- Parameters:
keyCodec
- The key codecvalueCodec
- The value codec- Returns:
- A new map codec
- Throws:
NullPointerException
- If the key codec or value codec is null- See Also:
-
map
@NotNull static <K,V> @NotNull Codec<Map<K,V>> map(@NotNull @NotNull KeyableCodec<K> keyCodec, @NotNull @NotNull Codec<V> valueCodec, int maxSize) Creates a new map codec with the given key codec and value codec and maximum size.
The map must have at most the maximum size after encoding and decoding.- Type Parameters:
K
- The type of the map keys that are encoded and decoded by the codecV
- The type of the map values that are encoded and decoded by the codec- Parameters:
keyCodec
- The key codecvalueCodec
- The value codecmaxSize
- The maximum size of the map (inclusive)- Returns:
- A new map codec
- Throws:
NullPointerException
- If the key codec or value codec is nullIllegalArgumentException
- If the maximum size is less than 0- See Also:
-
map
@NotNull static <K,V> @NotNull Codec<Map<K,V>> map(@NotNull @NotNull KeyableCodec<K> keyCodec, @NotNull @NotNull Codec<V> valueCodec, int minSize, int maxSize) Creates a new map codec with the given key codec and value codec and minimum and maximum size.
The map must have at least the minimum size and at most the maximum size after encoding and decoding.- Type Parameters:
K
- The type of the map keys that are encoded and decoded by the codecV
- The type of the map values that are encoded and decoded by the codec- Parameters:
keyCodec
- The key codecvalueCodec
- The value codecminSize
- The minimum size of the map (inclusive)maxSize
- The maximum size of the map (inclusive)- Returns:
- A new map codec
- Throws:
NullPointerException
- If the key codec or value codec is nullIllegalArgumentException
- If the minimum size is less than zero or greater than the maximum size- See Also:
-
noneEmptyMap
@NotNull static <K,V> @NotNull Codec<Map<K,V>> noneEmptyMap(@NotNull @NotNull KeyableCodec<K> keyCodec, @NotNull @NotNull Codec<V> valueCodec) Creates a new map codec with the given key codec and value codec for non-empty maps.
The map must not be empty after encoding and decoding.- Type Parameters:
K
- The type of the map keys that are encoded and decoded by the codecV
- The type of the map values that are encoded and decoded by the codec- Parameters:
keyCodec
- The key codecvalueCodec
- The value codec- Returns:
- A new map codec
- Throws:
NullPointerException
- If the key codec or value codec is null- See Also:
-
either
@NotNull static <F,S> @NotNull Codec<Either<F,S>> either(@NotNull @NotNull Codec<F> firstCodec, @NotNull @NotNull Codec<S> secondCodec) Creates a new either codec for the given codecs.
The value which is encoded and decoded by this codec can be either of typeF
orS
.
The codec will try to use the first codec to encode and decode the value,
if that fails, it will try to use the second codec.
Note: If the first codec is a string codec, it will always succeed,
so the second codec will never be used.
- Type Parameters:
F
- The type of the first valueS
- The type of the second value- Parameters:
firstCodec
- The first codecsecondCodec
- The second codec- Returns:
- A new either codec
- Throws:
NullPointerException
- If the first codec or second codec is null- See Also:
-
withAlternative
@NotNull static <C> @NotNull Codec<C> withAlternative(@NotNull @NotNull Codec<C> main, @NotNull @NotNull Codec<? extends C> alternative) Creates a new codec that uses the given codec as the main codec and the given codec as alternative codec.
If the main codec fails to encode or decode a value, the alternative codec is used.- Type Parameters:
C
- The type of the value that is encoded and decoded by the codec- Parameters:
main
- The main codecalternative
- The alternative codec- Returns:
- A new codec
- Throws:
NullPointerException
- If the main codec or alternative codec is null
-
stringResolver
@NotNull static <E> @NotNull Codec<E> stringResolver(@NotNull @NotNull Function<E, String> stringEncoder, @NotNull @NotNull Function<String, @Nullable E> stringDecoder) Creates a new codec that encodes and decodes values of the typeC
to and from strings.
The string encoder and decoder are defined as functions that convert values of the typeC
to and from strings.
If the decoder is unable to decode a string, it should return null.- Type Parameters:
E
- The type of the value that is encoded and decoded by the codec- Parameters:
stringEncoder
- The encoder functionstringDecoder
- The decoder function- Returns:
- A new codec
- Throws:
NullPointerException
- If the string encoder or decoder is null
-
codec
Wraps the current codec into a new codec with the given codec name.- Parameters:
name
- The name of the codec- Returns:
- A new codec
- Throws:
NullPointerException
- If the codec name is null- See Also:
-
keyable
@NotNull default @NotNull KeyableCodec<C> keyable(@NotNull @NotNull Function<C, String> keyEncoder, @NotNull @NotNull Function<String, @Nullable C> keyDecoder) Wraps the current codec into a new keyable codec using the given key encoder and key decoder.
The key encoder and key decoder are defined as functions that convert keys of the type C to and from strings.
If the key decoder is unable to decode a key, it should return null.- Parameters:
keyEncoder
- The key encoderkeyDecoder
- The key decoder- Returns:
- A new keyable codec
- Throws:
NullPointerException
- If the key encoder or key decoder is null- See Also:
-
optional
Wraps the current codec into a new optional codec.- Returns:
- A new optional codec for the current codec
- See Also:
-
list
Creates a new list codec uses the current codec as element codec for the list codec.
The created list codec has no size restrictions.- Returns:
- A new list codec for the current codec
- See Also:
-
list
Creates a new list codec uses the current codec as element codec for the list codec with the given maximum size.
The list must have at most the maximum size after encoding and decoding.- Parameters:
maxSize
- The maximum size of the list (inclusive)- Returns:
- A new list codec for the current codec
- Throws:
IllegalArgumentException
- If the maximum size is less than 0- See Also:
-
list
Creates a new list codec uses the current codec as element codec for the list codec with the given minimum and maximum size.
The list must have at least the minimum size and at most the maximum size after encoding and decoding.- Parameters:
minSize
- The minimum size of the list (inclusive)maxSize
- The maximum size of the list (inclusive)- Returns:
- A new list codec for the current codec
- Throws:
IllegalArgumentException
- If the minimum size is less than zero or greater than the maximum size- See Also:
-
noneEmptyList
Creates a new list codec uses the current codec as element codec for the list codec for non-empty lists.
The list must not be empty after encoding and decoding.- Returns:
- A new list codec for the current codec
- See Also:
-
stream
Creates a new stream codec uses the current codec as element codec for the stream codec.- Returns:
- A new stream codec for the current codec
- See Also:
-
withAlternative
@NotNull default @NotNull Codec<C> withAlternative(@NotNull @NotNull Codec<? extends C> alternative) Creates a new codec that uses the current codec as the main codec and the given codec as alternative codec.
If the main codec fails to encode or decode a value, the alternative codec is used.- Parameters:
alternative
- The alternative codec- Returns:
- A new codec
- Throws:
NullPointerException
- If the alternative codec is null- See Also:
-
xmap
@NotNull default <O> @NotNull Codec<O> xmap(@NotNull @NotNull Function<O, C> to, @NotNull @NotNull Function<C, O> from) Creates a new mapped codec of typeO
from the current codec.
The mapped codec maps the raw input and output values using the given functions.
The functions are applied before encoding and after decoding the base codec, on the raw values.
Any errors that occur during mapping must be self-contained and should not affect the base codec.
- Type Parameters:
O
- The type of the mapped value- Parameters:
to
- The encoding mapping functionfrom
- The decoding mapping function- Returns:
- A new mapped codec
- Throws:
NullPointerException
- If the encoding mapping function or decoding mapping function is null- See Also:
-
mapFlat
@NotNull default <O> @NotNull Codec<O> mapFlat(@NotNull @NotNull Function<O, C> to, @NotNull @NotNull ResultMappingFunction<C, O> from) Creates a new mapped codec of typeO
from the current codec.
The mapped codec maps the raw input value using the given function.
The function is applied before encoding the base codec.
This mapping functions allows the handling of errors that occur during decode-mapping.
Therefor the mapping function is applied to the result of the base codec.
The mapping function must return a new result that contains the mapped value or an error message.
- Type Parameters:
O
- The type of the mapped value- Parameters:
to
- The encoding mapping functionfrom
- The decoding mapping function- Returns:
- A new mapped codec
- Throws:
NullPointerException
- If the encoding mapping function or decoding mapping function is null- See Also:
-
map
@NotNull default <O> @NotNull Codec<O> map(@NotNull @NotNull ResultingFunction<O, C> to, @NotNull @NotNull ResultMappingFunction<C, O> from) Creates a new mapped codec of typeO
from the current codec.
This mapping functions allows the handling of errors that occur during mapping.
The encode-mapping function is applied before encoding the base codec.
The function can return a new result that contains the mapped value or an error message.
The decode-mapping function is applied to the result of the base codec.
The function can return a new result that contains the mapped value or an error message.
- Type Parameters:
O
- The type of the mapped value- Parameters:
to
- The encoding mapping functionfrom
- The decoding mapping function- Returns:
- A new mapped codec
- Throws:
NullPointerException
- If the encoding mapping function or decoding mapping function is null- See Also:
-
validate
Creates a new codec that will validate the result of the decoding process using the given validator function.
The validator function is applied to the decoded value and must return a result that contains the validated value or an error message.- Parameters:
validator
- The validator function- Returns:
- A new codec
- Throws:
NullPointerException
- If the validator function is null
-
orElse
Creates a new codec that will return the given default value in an error case during decoding.- Parameters:
defaultValue
- The default value- Returns:
- A new codec
- See Also:
-
orElseGet
Creates a new codec that will return the value provided by the given supplier in an error case during decoding.- Parameters:
supplier
- The default value supplier- Returns:
- A new codec
- Throws:
NullPointerException
- If the default value supplier is null
-
named
@NotNull default @NotNull Codec<C> named(@NotNull @NotNull String name, String @NotNull ... aliases) Creates a new named codec with the current codec using the given name and aliases.- Parameters:
name
- The name of the codecaliases
- The aliases of the codec- Returns:
- A new named codec
- Throws:
NullPointerException
- If the codec name is null- See Also:
-
getter
Creates a new configured codec.
The configured codec is used in the codec builder to create codecs for complex data structures.
The configured codec encodes and decodes components of the data structure using the given getter function.
It is expected that this is only called on named codecs.- Type Parameters:
O
- The type of the object which contains the component- Parameters:
getter
- The getter function- Returns:
- A new configured codec
- Throws:
NullPointerException
- If the getter function is null- See Also:
-
configure
@NotNull default <O> @NotNull ConfiguredCodec<C,O> configure(@NotNull @NotNull String name, @NotNull @NotNull Function<O, C> getter) Creates a new configured codec.
This method combines thenamed(String, String...)
andgetter(Function)
methods.
The configured codec is used in the codec builder to create codecs for complex data structures.
The configured codec encodes and decodes components of the data structure using the given getter function.
- Type Parameters:
O
- The type of the object which contains the component- Parameters:
name
- The name of the codecgetter
- The getter function- Returns:
- A new configured codec
- Throws:
NullPointerException
- If the codec name or getter function is null- See Also:
-