Add get() without clazz 25/83425/1
authorJim Hahn <jrh3@att.com>
Tue, 26 Mar 2019 23:32:56 +0000 (19:32 -0400)
committerJim Hahn <jrh3@att.com>
Tue, 26 Mar 2019 23:32:56 +0000 (19:32 -0400)
Added Registry.get() that does not need a class argument.

Change-Id: I3e8b0e3cc14eda914e0b1b201e6ddf67a9e3462e
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
utils/src/main/java/org/onap/policy/common/utils/services/Registry.java

index c209379..13fb338 100644 (file)
@@ -110,6 +110,23 @@ public class Registry {
         return (instance.name2object.remove(name) != null);
     }
 
+    /**
+     * Gets the object by the given name.
+     *
+     * @param name name of the object to get
+     * @return the object
+     * @throws IllegalArgumentException if no object is registered by the given name
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T get(String name) {
+        Object obj = instance.name2object.get(name);
+        if (obj == null) {
+            throw new IllegalArgumentException("not registered: " + name);
+        }
+
+        return (T) obj;
+    }
+
     /**
      * Gets the object by the given name.
      *