InstanceMap { public <T> T getOrCreate(String key, Factory<T> factory) { T t = t_get(key); if (t == null) { t = putNew(key, factory); } return t; } @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})  InstanceMap(); @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) protected  InstanceMap(InstanceMap source); Object put(String key, Object value); Object put(Class<?> clazz, Object value, Class... limit); Object put(Class<?> clazz, Object value); T replace(Class<T> clazz, Object value, Class... limit); T replace(Class<T> clazz, Object value); Object get(String key); T get(Class<T> clazz); T putNew(String key, Factory<? extends T> factory); T putNew(Class<?> clazz, Factory<? extends T> factory); T getOrCreate(String key, Factory<T> factory); T getOrCreate(String key, Class<T> impl); T getOrCreate(Class<T> clazz, Factory<? extends T> factory); T getOrCreate(Class<T> clazz, Class<? extends T> impl); T getOrCreate(Class<T> clazz); T get(Class<T> clazz, Inject inject); Object remove(String key); @SuppressWarnings("unchecked") T remove(Class<T> clazz); @SuppressWarnings("unchecked") T remove(Class<T> clazz, Class... limit); T initialize(T o); static Object getFactory(Class<?> clazz, String factory); static final Class<?> ANY_INTERFACE; static final Class[] NO_LIMIT; static final Class[] LIMIT_NO_INTERFACES; static final Class[] LIMIT_INTERFACES_ONLY; }
@Test public void test_getOrCreate() { Foo foo1 = iMap.getOrCreate(Foo.class); assertThat(foo1, _isA(Foo.class)); Foo foo2 = iMap.getOrCreate(Foo.class); assertThat(foo2, is(sameInstance(foo1))); }
@Test public void test_inject_interface_impl() { IFace iface = iMap.getOrCreate(IFace.class); assertThat(iface, _isA(IFaceImpl.class)); }
@Test public void test_param_constructor() { iMap.getOrCreate(Foo.class); ParamConstructor pc = iMap.getOrCreate(ParamConstructor.class); assertThat(pc.foo, is(notNullValue())); }
@Test public void test_default_inject() { DefInject ci = iMap.getOrCreate(DefInject.class); assertThat("foo", ci.foo, is(notNullValue())); assertThat("bar", ci.bar, is(notNullValue())); assertThat("bar2", ci.bar2, is(notNullValue())); assertThat(ci.bar2, is(not(sameInstance(ci.bar)))); }