java.lang.Object
net.luis.utils.io.codec.struct.MapCodec<K,V>
- Type Parameters:
K
- The type of keys in the mapV
- The type of values in the map
A codec for encoding and decoding maps of key-value pairs.
This codec uses two other codecs to encode and decode the keys and values of the map.
The key codec must be a
This codec uses two other codecs to encode and decode the keys and values of the map.
The key codec must be a
KeyableCodec
to encode and decode the keys of the map.
The map codec can be sized to only accept maps of a certain size range.
The minimum size and maximum size are inclusive.
If the map size is out of range, the codec will return an error during encoding or decoding.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final KeyableCodec
<K> The codec used to encode and decode the keys of the map.private final int
The maximum size of the map (inclusive).private final int
The minimum size of the map (inclusive).The codec used to encode and decode the values of the map.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
ConstructorsConstructorDescriptionMapCodec
(@NotNull KeyableCodec<K> keyCodec, @NotNull Codec<V> valueCodec) Constructs a new map codec using the given codecs for the keys and values.
The map codec will accept maps of any size.
Do not use this constructor directly, use any of the map factory methods inCodec
instead.MapCodec
(@NotNull KeyableCodec<K> keyCodec, @NotNull Codec<V> valueCodec, int minSize, int maxSize) Constructs a new map codec using the given codecs for the keys and values and the size range of the map.
Do not use this constructor directly, use any of the map factory methods inCodec
instead. -
Method Summary
Modifier and TypeMethodDescriptiondecodeEntry
(@NotNull TypeProvider<R> provider, Map.Entry<String, R> entry) Decodes the given map entry using the key and value codecs.
The result contains the decoded entry or an error message.decodeStart
(@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.encodeEntry
(@NotNull TypeProvider<R> provider, Map.Entry<K, V> entry) Encodes the given map entry using the key and value codecs.
The result contains the encoded entry or an error message.<R> @NotNull Result
<R> encodeStart
(@NotNull TypeProvider<R> provider, R current, @Nullable Map<K, V> value) Encodes the value of the specified type and returns the result.
The result contains the encoded value or an error message.boolean
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
-
keyCodec
The codec used to encode and decode the keys of the map. -
valueCodec
The codec used to encode and decode the values of the map. -
minSize
private final int minSizeThe minimum size of the map (inclusive). -
maxSize
private final int maxSizeThe maximum size of the map (inclusive).
-
-
Constructor Details
-
MapCodec
@Internal public MapCodec(@NotNull @NotNull KeyableCodec<K> keyCodec, @NotNull @NotNull Codec<V> valueCodec) Constructs a new map codec using the given codecs for the keys and values.
The map codec will accept maps of any size.
Do not use this constructor directly, use any of the map factory methods inCodec
instead.- Parameters:
keyCodec
- The key codecvalueCodec
- The value codec- Throws:
NullPointerException
- If the key or value codec is null
-
MapCodec
@Internal public MapCodec(@NotNull @NotNull KeyableCodec<K> keyCodec, @NotNull @NotNull Codec<V> valueCodec, int minSize, int maxSize) Constructs a new map codec using the given codecs for the keys and values and the size range of the map.
Do not use this constructor directly, use any of the map factory methods inCodec
instead.- Parameters:
keyCodec
- The key codecvalueCodec
- The value codecminSize
- The minimum size of the map (inclusive)maxSize
- The maximum size of the map (inclusive)- Throws:
NullPointerException
- If the key or value codec is nullIllegalArgumentException
- If the minimum size is less than zero or greater than the maximum size
-
-
Method Details
-
encodeStart
@NotNull public <R> @NotNull Result<R> encodeStart(@NotNull @NotNull TypeProvider<R> provider, @NotNull R current, @Nullable @Nullable Map<K, V> 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<K>
- Type Parameters:
R
- The type to encode to- Parameters:
provider
- The type providercurrent
- The current valuevalue
- The value to encode- Returns:
- The result
-
encodeEntry
@NotNull private <R> @NotNull Result<Map.Entry<String,R>> encodeEntry(@NotNull @NotNull TypeProvider<R> provider, @NotNull Map.Entry<K, V> entry) Encodes the given map entry using the key and value codecs.
The result contains the encoded entry or an error message.- Type Parameters:
R
- The type of the encoded value- Parameters:
provider
- The type providerentry
- The map entry- Returns:
- The result of the encoding process
- Throws:
NullPointerException
- If the type provider or map entry is null
-
decodeStart
@NotNull public <R> @NotNull Result<Map<K,V>> 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<K>
- Type Parameters:
R
- The type to decode from- Parameters:
provider
- The type providervalue
- The value to decode- Returns:
- The result
-
decodeEntry
@NotNull private <R> @NotNull Result<Map.Entry<K,V>> decodeEntry(@NotNull @NotNull TypeProvider<R> provider, @NotNull Map.Entry<String, R> entry) Decodes the given map entry using the key and value codecs.
The result contains the decoded entry or an error message.- Type Parameters:
R
- The type of the decoded value- Parameters:
provider
- The type providerentry
- The map entry- Returns:
- The result of the decoding process
- Throws:
NullPointerException
- If the type provider or map entry is null
-
equals
-
hashCode
public int hashCode() -
toString
-