From 190309e5f3066edfbd4bcb8ec9999843c3581482 Mon Sep 17 00:00:00 2001 From: asofold Date: Mon, 24 Apr 2017 23:21:53 +0200 Subject: [PATCH] Add an auxiliary class, to delegate IRegisterWithOrder setup to. --- .../order/RegistrationOrderStore.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 NCPCore/src/main/java/fr/neatmonster/nocheatplus/components/registry/order/RegistrationOrderStore.java diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/components/registry/order/RegistrationOrderStore.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/components/registry/order/RegistrationOrderStore.java new file mode 100644 index 00000000..2831ae9b --- /dev/null +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/components/registry/order/RegistrationOrderStore.java @@ -0,0 +1,74 @@ +package fr.neatmonster.nocheatplus.components.registry.order; + +import java.util.HashMap; +import java.util.Map; + +/** + * This is an auxiliary class that allows more convenient implementations of + * IRegisterWithOrder, it's not really meant to be registered anywhere. Methods + * for adding default and mappings are provided for chaining. + * + * @author asofold + * + */ +public class RegistrationOrderStore implements IRegisterWithOrder { + // (No generics this time.) + + private final Map, RegistrationOrder> orderMap = new HashMap, RegistrationOrder>(); + private RegistrationOrder defaultOrder = null; + + public RegistrationOrderStore() { + } + + /** + * Set the default order to apply when no mapping is present for a given + * key. + * + * @param order + * Allows setting to null. The default order applies, if no + * mapping is present. It does not apply if a mapping to null is + * present. + * @return + */ + public RegistrationOrderStore defaultOrder(RegistrationOrder order) { + this.defaultOrder = order; + return this; + } + + /** + * Set a mapping from type to register for and order. + * + * @param registerForType + * @param order + * Can be set to null, to prevent the return of the defaultOrder. + * @return + */ + public RegistrationOrderStore order(Class registerForType, RegistrationOrder order) { + this.orderMap.put(registerForType, order); + return this; + } + + /** + * Convenience: set up defaultOrder with the constructor. + * + * @param defaultOrder + */ + public RegistrationOrderStore(RegistrationOrder order) { + this.defaultOrder = order; + } + + @Override + public RegistrationOrder getRegistrationOrder(final Class registerForType) { + // Prefer set null entries over the default type. + return orderMap.containsKey(registerForType) ? orderMap.get(registerForType) : defaultOrder; + } + + /** + * Remove all mappings and set the defaultOrder to null. + */ + public void clear() { + orderMap.clear(); + defaultOrder = null; + } + +}