Class MapCodec<K,V>

java.lang.Object
net.luis.utils.io.codec.struct.MapCodec<K,V>
Type Parameters:
K - The type of keys in the map
V - The type of values in the map
All Implemented Interfaces:
Codec<Map<K,V>>, Decoder<Map<K,V>>, Encoder<Map<K,V>>

public class MapCodec<K,V> extends Object implements Codec<Map<K,V>>
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 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 Details

    • keyCodec

      private final KeyableCodec<K> keyCodec
      The codec used to encode and decode the keys of the map.
    • valueCodec

      private final Codec<V> valueCodec
      The codec used to encode and decode the values of the map.
    • minSize

      private final int minSize
      The minimum size of the map (inclusive).
    • maxSize

      private final int maxSize
      The 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 in Codec instead.
      Parameters:
      keyCodec - The key codec
      valueCodec - 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 in Codec instead.
      Parameters:
      keyCodec - The key codec
      valueCodec - The value codec
      minSize - The minimum size of the map (inclusive)
      maxSize - The maximum size of the map (inclusive)
      Throws:
      NullPointerException - If the key or value codec is null
      IllegalArgumentException - 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 to TypeProvider.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 interface Encoder<K>
      Type Parameters:
      R - The type to encode to
      Parameters:
      provider - The type provider
      current - The current value
      value - 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 provider
      entry - 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 interface Decoder<K>
      Type Parameters:
      R - The type to decode from
      Parameters:
      provider - The type provider
      value - 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 provider
      entry - The map entry
      Returns:
      The result of the decoding process
      Throws:
      NullPointerException - If the type provider or map entry is null
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object