CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/collections/ValuesSetSerializer.java

47 lines
1.5 KiB
Java
Raw Normal View History

2021-05-08 03:09:00 +02:00
package it.cavallium.dbengine.database.collections;
2021-09-17 16:56:28 +02:00
import io.net5.buffer.api.Buffer;
import io.net5.buffer.api.BufferAllocator;
import io.net5.buffer.api.Send;
2021-08-22 21:23:22 +02:00
import it.cavallium.dbengine.database.serialization.SerializationException;
2021-05-08 03:09:00 +02:00
import it.cavallium.dbengine.database.serialization.Serializer;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
2021-07-13 22:58:08 +02:00
import java.util.ArrayList;
2021-09-22 18:33:28 +02:00
import java.util.Objects;
2021-05-08 03:09:00 +02:00
import org.jetbrains.annotations.NotNull;
2021-09-22 18:33:28 +02:00
import org.jetbrains.annotations.Nullable;
2021-05-08 03:09:00 +02:00
2021-09-01 00:01:56 +02:00
class ValuesSetSerializer<X> implements Serializer<ObjectArraySet<X>> {
2021-05-08 03:09:00 +02:00
2021-09-01 00:01:56 +02:00
private final Serializer<X> entrySerializer;
2021-05-08 03:09:00 +02:00
2021-10-19 00:22:05 +02:00
ValuesSetSerializer(Serializer<X> entrySerializer) {
2021-05-08 03:09:00 +02:00
this.entrySerializer = entrySerializer;
}
@Override
2021-10-19 00:22:05 +02:00
public @NotNull ObjectArraySet<X> deserialize(@NotNull Buffer serialized) throws SerializationException {
Objects.requireNonNull(serialized);
int entriesLength = serialized.readInt();
ArrayList<X> deserializedElements = new ArrayList<>(entriesLength);
for (int i = 0; i < entriesLength; i++) {
var deserializationResult = entrySerializer.deserialize(serialized);
deserializedElements.add(deserializationResult);
2021-05-08 03:09:00 +02:00
}
2021-10-19 00:22:05 +02:00
return new ObjectArraySet<>(deserializedElements);
2021-05-08 03:09:00 +02:00
}
@Override
2021-10-19 00:22:05 +02:00
public void serialize(@NotNull ObjectArraySet<X> deserialized, Buffer output) throws SerializationException {
output.writeInt(deserialized.size());
for (X entry : deserialized) {
entrySerializer.serialize(entry, output);
2021-05-08 03:09:00 +02:00
}
}
2021-10-19 00:22:05 +02:00
@Override
public int getSerializedSizeHint() {
return -1;
}
2021-05-08 03:09:00 +02:00
}