diff --git a/src/com/blarg/gdx/Services.java b/src/com/blarg/gdx/Services.java new file mode 100644 index 0000000..49344d4 --- /dev/null +++ b/src/com/blarg/gdx/Services.java @@ -0,0 +1,62 @@ +package com.blarg.gdx; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.utils.ObjectMap; + +/** + * Just a simple service locator. Completely up to the user to use this 'correctly.' + * + * echo $BAD_PRACTICE_COMPLAINTS > /dev/null + */ +public class Services { + public interface Service { + void onRegister(); + void onUnregister(); + } + + static final ObjectMap, Object> services = new ObjectMap, Object>(); + + public static void register(Object service) { + if (service == null) + throw new IllegalArgumentException("service can not be null."); + + Class type = service.getClass(); + if (services.containsKey(type)) + throw new UnsupportedOperationException("Another service of this type has already been registered."); + + if (type.isAssignableFrom(Service.class)) + ((Service)service).onRegister(); + + services.put(type, service); + Gdx.app.log("Services", String.format("Registered object of type %s.", type.getSimpleName())); + } + + public static void unregister(Class type) { + if (type == null) + throw new IllegalArgumentException("type can not be null."); + + Object service = services.get(type); + if (service == null) + return; + + services.remove(type); + Gdx.app.log("Services", String.format("Unregistered object of type %s.", type.getSimpleName())); + + if (type.isAssignableFrom(Service.class)) + ((Service)service).onUnregister(); + } + + public static T get(Class type) { + return type.cast(services.get(type)); + } + + public static void unregisterAll() { + Gdx.app.log("Services", "Unregistering all services."); + for (ObjectMap.Entry, Object> i : services.entries()) { + if (i.key.isAssignableFrom(Service.class)) + ((Service)i.value).onUnregister(); + } + + services.clear(); + } +}