CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/serialization/CodecsArray.java

29 lines
640 B
Java
Raw Normal View History

package it.cavallium.dbengine.database.serialization;
public class CodecsArray<A> implements Codecs<A> {
private final Codec<A>[] codecs;
public CodecsArray() {
//noinspection unchecked
codecs = new Codec[256];
}
@Override
public synchronized void registerCodec(int id, Codec<A> serializer) {
if (codecs[id] != null) {
throw new IllegalArgumentException("Codec " + id + " already registered!");
}
codecs[id] = serializer;
}
@Override
public Codec<A> getCodec(int id) {
var codec = codecs[id];
if (codec == null) {
throw new UnsupportedOperationException("Unsupported codec " + id);
}
return codec;
}
}