Package com.dua3.utility.io
Class Codecs
java.lang.Object
com.dua3.utility.io.Codecs
A registry for
Codec
instances.
Codecs are registered by calling registerCodec(Class, Encoder, Decoder)
.
Registered codecs for a class can be obtained by using get(Class)
.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
C extends Collection<T>>
Codec<C> collectionCodec
(String name, Codec<T> codec, IntFunction<? extends C> construct) GetCodec
for aCollection
.static <T> Codec
<T> createCodec
(String name, Encoder<? super T> enc, Decoder<? extends T> dec) Retrieves the codec registered for a specific class.mapEntryCodec
(Codec<K> codecK, Codec<? extends V> codecV) <T extends @Nullable Object>
voidregisterCodec
(Class<T> cls, Encoder<? super T> enc, Decoder<? extends T> dec) Register a codec for a specific class.
-
Constructor Details
-
Codecs
public Codecs()Constructor. Default codecs for the Java primitive types are automatically registered.
-
-
Method Details
-
createCodec
public static <T> Codec<T> createCodec(String name, Encoder<? super T> enc, Decoder<? extends T> dec) - Type Parameters:
T
- the object type- Parameters:
name
- the codec nameenc
- the encoderdec
- the decoder- Returns:
- the new codec
-
collectionCodec
public static <T,C extends Collection<T>> Codec<C> collectionCodec(String name, Codec<T> codec, IntFunction<? extends C> construct) GetCodec
for aCollection
.- Type Parameters:
T
- the type of collection elementsC
- the collection type- Parameters:
name
- the codec namecodec
- the element codecconstruct
- collection factory method- Returns:
- collection codec
-
mapEntryCodec
public static <K,V> Codec<Map.Entry<K,V>> mapEntryCodec(Codec<K> codecK, Codec<? extends V> codecV) - Type Parameters:
K
- the type of map keysV
- the type of map values- Parameters:
codecK
- the codec for keyscodecV
- the codec for values- Returns:
- the map entry codec
-
mapCodec
public static <K,V, Codec<M> mapCodecM extends Map<K, V>> (Codec<K> codecK, Codec<V> codecV, Supplier<? extends M> construct) - Type Parameters:
K
- the key typeV
- the value typeM
- the map type- Parameters:
codecK
- the key codeccodecV
- the value codecconstruct
- the map construction method- Returns:
- map codec
-
registerCodec
public <T extends @Nullable Object> void registerCodec(Class<T> cls, Encoder<? super T> enc, Decoder<? extends T> dec) Register a codec for a specific class.- Type Parameters:
T
- the class type- Parameters:
cls
- the class to register the codec forenc
- the encoder for the classdec
- the decoder for the class- Throws:
IllegalArgumentException
- if a codec is already registered for the class
-
get
Retrieves the codec registered for a specific class.- Type Parameters:
T
- the class type- Parameters:
cls
- the class to retrieve the codec for- Returns:
- an Optional containing the codec, or an empty Optional if no codec is registered for the class
-