2021-05-11 21:59:05 +02:00
|
|
|
package it.cavallium.dbengine.client;
|
|
|
|
|
2021-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.Buffer;
|
|
|
|
import io.net5.buffer.api.Send;
|
2021-08-22 21:23:22 +02:00
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationException;
|
2021-05-11 21:59:05 +02:00
|
|
|
import it.cavallium.dbengine.database.serialization.Serializer;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2021-09-22 18:33:28 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-05-11 21:59:05 +02:00
|
|
|
|
2021-09-02 17:15:40 +02:00
|
|
|
public class MappedSerializer<A, B> implements Serializer<B> {
|
2021-05-11 21:59:05 +02:00
|
|
|
|
2021-09-02 17:15:40 +02:00
|
|
|
private final Serializer<A> serializer;
|
2021-05-11 21:59:05 +02:00
|
|
|
private final Mapper<A, B> keyMapper;
|
|
|
|
|
2021-09-02 17:15:40 +02:00
|
|
|
public MappedSerializer(Serializer<A> serializer,
|
2021-05-11 21:59:05 +02:00
|
|
|
Mapper<A, B> keyMapper) {
|
|
|
|
this.serializer = serializer;
|
|
|
|
this.keyMapper = keyMapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-10-19 00:22:05 +02:00
|
|
|
public @NotNull B deserialize(@NotNull Buffer serialized) throws SerializationException {
|
|
|
|
return keyMapper.map(serializer.deserialize(serialized));
|
2021-05-11 21:59:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-10-19 00:22:05 +02:00
|
|
|
public void serialize(@NotNull B deserialized, Buffer output) throws SerializationException {
|
|
|
|
serializer.serialize(keyMapper.unmap(deserialized), output);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSerializedSizeHint() {
|
|
|
|
return serializer.getSerializedSizeHint();
|
2021-05-11 21:59:05 +02:00
|
|
|
}
|
|
|
|
}
|