CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/serialization/CodecsStandard.java
Andrea Cavalli 7a712722d7 Code cleanup
2021-12-16 16:34:20 +01:00

30 lines
734 B
Java

package it.cavallium.dbengine.database.serialization;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
public class CodecsStandard<A> implements Codecs<A> {
private final Int2ObjectMap<Codec<A>> codecs;
public CodecsStandard() {
codecs = new Int2ObjectOpenHashMap<>();
}
@Override
public void registerCodec(int id, Codec<A> serializer) {
if (codecs.put(id, serializer) != null) {
throw new IllegalArgumentException("Codec " + id + " already registered!");
}
}
@Override
public Codec<A> getCodec(int id) {
var codec = codecs.get(id);
if (codec == null) {
throw new UnsupportedOperationException("Unsupported codec " + id);
}
return codec;
}
}