Class DefaultObjectMapper
java.lang.Object
eu.cloudnetservice.driver.network.rpc.defaults.object.DefaultObjectMapper
- All Implemented Interfaces:
ObjectMapper
@Singleton
@Provides(ObjectMapper.class)
public class DefaultObjectMapper
extends Object
implements ObjectMapper
The default object mapper implementation.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ObjectMapperprivate static final Map<Type,ObjectSerializer<?>> private final Map<Type,ObjectSerializer<?>> private final com.github.benmanes.caffeine.cache.LoadingCache<Type,Collection<eu.cloudnetservice.common.collection.Pair<Type, Type>>> -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new default object mapper instance with all default object serializers already registered.DefaultObjectMapper(boolean registerDefaultSerializers) Constructs a new default object mapper instance and optionally registers the default serializers to it. -
Method Summary
Modifier and TypeMethodDescription<T> TreadObject(@NonNull DataBuf dataBuf, @NonNull Type type) Reads an object from the given buffer by the given type using the previously registered serializers.<T> @NonNull ObjectMapperregisterBinding(@NonNull Type type, @NonNull ObjectSerializer<T> serializer, boolean superTypes) Registers the given object serializer to this mapper, including all super types of the given type if requested.protected <T> @Nullable ObjectSerializer<T>serializerForType(@NonNull eu.cloudnetservice.common.collection.Pair<Type, Type> typePair) Finds the best matching serializer for the given type.unregisterBinding(@NonNull Type type, boolean superTypes) Unregisters all object serializers which were registered for the given type, include all super types of the given type if requested.unregisterBindings(@NonNull ClassLoader classLoader) Unregisters all object serializers whose classes were loaded by the given class loader.<T> DataBuf.MutablewriteObject(DataBuf.Mutable dataBuf, T object) Writes the given object into the given data buffer using the previously registered object serializers.
-
Field Details
-
DEFAULT_MAPPER
-
DEFAULT_SERIALIZERS
-
registeredSerializers
-
typeCache
private final com.github.benmanes.caffeine.cache.LoadingCache<Type,Collection<eu.cloudnetservice.common.collection.Pair<Type, typeCacheType>>>
-
-
Constructor Details
-
DefaultObjectMapper
public DefaultObjectMapper()Constructs a new default object mapper instance with all default object serializers already registered. This call is equivalent tonew DefaultObjectMapper(true). -
DefaultObjectMapper
public DefaultObjectMapper(boolean registerDefaultSerializers) Constructs a new default object mapper instance and optionally registers the default serializers to it.- Parameters:
registerDefaultSerializers- true if the default serializers should get registered, false otherwise.
-
-
Method Details
-
unregisterBinding
@NonNull public @NonNull ObjectMapper unregisterBinding(@NonNull @NonNull Type type, boolean superTypes) Unregisters all object serializers which were registered for the given type, include all super types of the given type if requested.- Specified by:
unregisterBindingin interfaceObjectMapper- Parameters:
type- the base type of the mappers to unregister.superTypes- if all super types of the base type should get included when unregistering.- Returns:
- the same instance as used to call the method, for chaining.
-
unregisterBindings
Unregisters all object serializers whose classes were loaded by the given class loader.- Specified by:
unregisterBindingsin interfaceObjectMapper- Parameters:
classLoader- the class loader of the object serializers to unregister.- Returns:
- the same instance as used to call the method, for chaining.
-
registerBinding
@NonNull public <T> @NonNull ObjectMapper registerBinding(@NonNull @NonNull Type type, @NonNull @NonNull ObjectSerializer<T> serializer, boolean superTypes) Registers the given object serializer to this mapper, including all super types of the given type if requested. This call will silently be ignored if previously a mapper was registered for one of the requested types.- Specified by:
registerBindingin interfaceObjectMapper- Type Parameters:
T- the generic type of the object serializer to register.- Parameters:
type- the type which can be (de-) serialized using the given serializer.serializer- the serializer to use for the type.superTypes- if all super types of the given type should get associated with the given serializer as well.- Returns:
- the same instance as used to call the method, for chaining.
-
writeObject
@NonNull public <T> DataBuf.Mutable writeObject(@NonNull DataBuf.Mutable dataBuf, @Nullable T object) Writes the given object into the given data buffer using the previously registered object serializers.- Specified by:
writeObjectin interfaceObjectMapper- Type Parameters:
T- the generic type of the object to serialize.- Parameters:
dataBuf- the buffer to serialize the data to.object- the object to serialize into the buffer, can be null.- Returns:
- the same buffer used to call the method, for further writing.
-
readObject
Reads an object from the given buffer by the given type using the previously registered serializers. The returned object might be null if the object written to the buffer was null as well.- Specified by:
readObjectin interfaceObjectMapper- Type Parameters:
T- the generic type of the object.- Parameters:
dataBuf- the buffer from which the object should get deserialized.type- the type of the object to deserialize.- Returns:
- the deserialized object from the buffer, can be null.
-
serializerForType
@Nullable protected <T> @Nullable ObjectSerializer<T> serializerForType(@NonNull @NonNull eu.cloudnetservice.common.collection.Pair<Type, Type> typePair) Finds the best matching serializer for the given type. The method first tries to get the serializer by the exact type of the supplied type token, then by the raw type.- Type Parameters:
T- the generic type of the object serializer to get.- Parameters:
typePair- the mapping pair of the actual type (first) and the raw type (second) of the serializer to get.- Returns:
- the best matching object serializer for the given type.
- Throws:
NullPointerException- if the given type token is null.
-