package com.hypixel.hytale.codec.store; import com.hypixel.hytale.codec.Codec; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; import javax.annotation.Nullable; public class CodecStore { public static final CodecStore STATIC = new CodecStore(); private final CodecStore parent; private final Map, Codec> codecs = new ConcurrentHashMap<>(); private final Map, Supplier>> codecSuppliers = new ConcurrentHashMap<>(); public CodecStore() { this.parent = STATIC; } public CodecStore(CodecStore parent) { this.parent = parent; } @Nullable public Codec getCodec(CodecKey key) { Codec codec = (Codec)this.codecs.get(key); if (codec != null) { return codec; } else { Supplier> supplier = this.codecSuppliers.get(key); if (supplier != null) { codec = (Codec)supplier.get(); } if (codec != null) { return codec; } else { return this.parent != null ? this.parent.getCodec(key) : null; } } } public void putCodec(CodecKey key, Codec codec) { this.codecs.put(key, codec); } public Codec removeCodec(CodecKey key) { return this.codecs.remove(key); } public void putCodecSupplier(CodecKey key, Supplier> supplier) { this.codecSuppliers.put(key, supplier); } public Supplier> removeCodecSupplier(CodecKey key) { return this.codecSuppliers.remove(key); } }