CavalliumDBEngine/src/main/java/it/cavallium/dbengine/client/MappedSerializerFixedLength.java

37 lines
1.2 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.client;
2021-08-29 23:18:03 +02:00
import io.netty.buffer.api.Buffer;
2021-08-31 09:14:46 +02:00
import io.netty.buffer.api.Send;
2021-08-22 21:23:22 +02:00
import it.cavallium.dbengine.database.serialization.SerializationException;
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
import org.jetbrains.annotations.NotNull;
2021-08-31 09:14:46 +02:00
public class MappedSerializerFixedLength<A, B> implements SerializerFixedBinaryLength<B, Send<Buffer>> {
2021-08-31 09:14:46 +02:00
private final SerializerFixedBinaryLength<A, Send<Buffer>> fixedLengthSerializer;
private final Mapper<A, B> keyMapper;
2021-08-31 09:14:46 +02:00
public MappedSerializerFixedLength(SerializerFixedBinaryLength<A, Send<Buffer>> fixedLengthSerializer,
Mapper<A, B> keyMapper) {
this.fixedLengthSerializer = fixedLengthSerializer;
this.keyMapper = keyMapper;
}
@Override
2021-08-31 09:14:46 +02:00
public @NotNull B deserialize(@NotNull Send<Buffer> serialized) throws SerializationException {
try (serialized) {
return keyMapper.map(fixedLengthSerializer.deserialize(serialized));
}
}
@Override
2021-08-31 09:14:46 +02:00
public @NotNull Send<Buffer> serialize(@NotNull B deserialized) throws SerializationException {
return fixedLengthSerializer.serialize(keyMapper.unmap(deserialized));
}
@Override
public int getSerializedBinaryLength() {
return fixedLengthSerializer.getSerializedBinaryLength();
}
}