From 4a96d9cad446ea405b51dfeebb01a1b6d7f6fb2b Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 27 Sep 2022 19:21:16 -0400 Subject: Add some interesting new things Adds a number of things based off of some of the notes I've made over time, plus a few papers I've read. More details to come later, whenever I decide to actually get serious about documentation and examples and the like --- .../src/main/java/bjc/utils/services/Bordello.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 base/src/main/java/bjc/utils/services/Bordello.java (limited to 'base/src/main/java/bjc/utils/services/Bordello.java') diff --git a/base/src/main/java/bjc/utils/services/Bordello.java b/base/src/main/java/bjc/utils/services/Bordello.java new file mode 100644 index 0000000..104c4ab --- /dev/null +++ b/base/src/main/java/bjc/utils/services/Bordello.java @@ -0,0 +1,53 @@ +package bjc.utils.services; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * A place to retrieve services from + * + * @author bjcul + * + */ +public class Bordello { + private static final Map, Object> services = new ConcurrentHashMap<>(); + + /** + * Retrieve the implementation of a given service. + * + * @param The type of the service. + * + * @param interfaceClass The class of the service. + * + * @return The default implementation of the service. + */ + public static T get(Class interfaceClass) { + synchronized (interfaceClass) { + Object service = services.get(interfaceClass); + if (service == null) { + try { + Class implementor = interfaceClass.getAnnotation(Implementor.class).value(); + service = implementor.getDeclaredConstructor().newInstance(); + services.put(interfaceClass, implementor); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + return interfaceClass.cast(service); + } + } + + /** + * Set an implementation for a given service to be something other than the default. + * + * @param The type of the service + * + * @param interfaceClass The class of the service + * @param implementor The alternate implementation for the service. + */ + public static void set(Class interfaceClass, T implementor) { + synchronized (interfaceClass) { + services.put(interfaceClass, implementor); + } + } +} -- cgit v1.2.3