package it.cavallium.dbengine.client; import io.netty.buffer.ByteBuf; import it.cavallium.dbengine.database.serialization.SerializationException; import it.cavallium.dbengine.database.serialization.Serializer; import org.jetbrains.annotations.NotNull; public class MappedSerializer implements Serializer { private final Serializer serializer; private final Mapper keyMapper; public MappedSerializer(Serializer serializer, Mapper keyMapper) { this.serializer = serializer; this.keyMapper = keyMapper; } @Override public @NotNull B deserialize(@NotNull ByteBuf serialized) throws SerializationException { try { return keyMapper.map(serializer.deserialize(serialized.retain())); } finally { serialized.release(); } } @Override public @NotNull ByteBuf serialize(@NotNull B deserialized) throws SerializationException { return serializer.serialize(keyMapper.unmap(deserialized)); } }