Better use of generics to enforce types for serialization

This commit is contained in:
nathan 2020-06-24 10:25:34 +02:00
parent 06aeb876eb
commit 0f466ec40e
2 changed files with 8 additions and 8 deletions

View File

@ -38,7 +38,7 @@ interface SerializationManager {
* Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this * Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this
* method. The order must be the same at deserialization as it was for serialization. * method. The order must be the same at deserialization as it was for serialization.
*/ */
SerializationManager register(Class<?> clazz); <T> SerializationManager register(Class<T> clazz);
/** /**
* Registers the class using the specified ID. If the ID is already in use by the same type, the old entry is overwritten. If the ID * Registers the class using the specified ID. If the ID is already in use by the same type, the old entry is overwritten. If the ID
@ -51,7 +51,7 @@ interface SerializationManager {
* @param id Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-8 are used by default for primitive types and String, but * @param id Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-8 are used by default for primitive types and String, but
* these IDs can be repurposed. * these IDs can be repurposed.
*/ */
SerializationManager register(Class<?> clazz, int id); <T> SerializationManager register(Class<T> clazz, int id);
/** /**
* Registers the class using the lowest, next available integer ID and the specified serializer. If the class is already registered, * Registers the class using the lowest, next available integer ID and the specified serializer. If the class is already registered,
@ -62,7 +62,7 @@ interface SerializationManager {
* Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this * Because the ID assigned is affected by the IDs registered before it, the order classes are registered is important when using this
* method. The order must be the same at deserialization as it was for serialization. * method. The order must be the same at deserialization as it was for serialization.
*/ */
SerializationManager register(Class<?> clazz, Serializer<?> serializer); <T> SerializationManager register(Class<T> clazz, Serializer<T> serializer);
/** /**
* Registers the class using the specified ID and serializer. If the ID is already in use by the same type, the old entry is * Registers the class using the specified ID and serializer. If the ID is already in use by the same type, the old entry is
@ -75,7 +75,7 @@ interface SerializationManager {
* @param id Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-8 are used by default for primitive types and String, but * @param id Must be >= 0. Smaller IDs are serialized more efficiently. IDs 0-8 are used by default for primitive types and String, but
* these IDs can be repurposed. * these IDs can be repurposed.
*/ */
SerializationManager register(Class<?> clazz, Serializer<?> serializer, int id); <T> SerializationManager register(Class<T> clazz, Serializer<T> serializer, int id);
/** /**
* Waits until a kryo is available to write, using CAS operations to prevent having to synchronize. * Waits until a kryo is available to write, using CAS operations to prevent having to synchronize.

View File

@ -36,28 +36,28 @@ class DefaultStorageSerializationManager implements SerializationManager {
@Override @Override
public public
SerializationManager register(final Class<?> clazz) { <T> SerializationManager register(final Class<T> clazz) {
kryo.register(clazz); kryo.register(clazz);
return this; return this;
} }
@Override @Override
public public
SerializationManager register(final Class<?> clazz, final int id) { <T> SerializationManager register(final Class<T> clazz, final int id) {
kryo.register(clazz, id); kryo.register(clazz, id);
return this; return this;
} }
@Override @Override
public public
SerializationManager register(final Class<?> clazz, final Serializer<?> serializer) { <T> SerializationManager register(final Class<T> clazz, final Serializer<T> serializer) {
kryo.register(clazz, serializer); kryo.register(clazz, serializer);
return this; return this;
} }
@Override @Override
public public
SerializationManager register(final Class<?> type, final Serializer<?> serializer, final int id) { <T> SerializationManager register(final Class<T> type, final Serializer<T> serializer, final int id) {
kryo.register(type, serializer, id); kryo.register(type, serializer, id);
return this; return this;
} }