add Service.register overload allowing to register an object by one of it's sub/super types instead
This commit is contained in:
parent
e815936e63
commit
b5fccb3fd4
|
@ -20,7 +20,17 @@ public class Services {
|
||||||
if (service == null)
|
if (service == null)
|
||||||
throw new IllegalArgumentException("service can not be null.");
|
throw new IllegalArgumentException("service can not be null.");
|
||||||
|
|
||||||
Class<?> type = service.getClass();
|
register(service.getClass(), service);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void register(Class<?> type, Object service) {
|
||||||
|
if (type == null)
|
||||||
|
throw new IllegalArgumentException("type can not be null.");
|
||||||
|
if (service == null)
|
||||||
|
throw new IllegalArgumentException("service can not be null.");
|
||||||
|
if (!type.isInstance(service))
|
||||||
|
throw new IllegalArgumentException("service object is not of the type specified.");
|
||||||
|
|
||||||
if (services.containsKey(type))
|
if (services.containsKey(type))
|
||||||
throw new UnsupportedOperationException("Another service of this type has already been registered.");
|
throw new UnsupportedOperationException("Another service of this type has already been registered.");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue