package geeks.expressive;

import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoBuilder;
import org.picocontainer.PicoContainer;
import org.picocontainer.monitors.ComposingMonitor;

import java.util.Map;
import java.util.HashMap;

/**
 * A wrapper for picocontainer.
 *
 * @author pabstec
 */
public class DefaultObjectFactory implements ObjectFactory {
  private final MutablePicoContainer container;
  private final Map<Class<?>, Object> addedComponents = new HashMap<Class<?>, Object>();

  public DefaultObjectFactory() {
    this.container = new PicoBuilder().withConstructorInjection().withMonitor(new ComposingMonitor(new ComposingMonitor.Composer() {
      @Override
      public Object compose(PicoContainer picoContainer, Object o) {
        if (o instanceof Class) {
          return getInstance((Class<?>) o);
        }
        return null;
      }
    })).build();
  }

  public <T> T getInstance(Class<T> componentClass) {
    @SuppressWarnings({"unchecked"})
    T instance = (T) addedComponents.get(componentClass);
    if (instance != null) {
      return instance;
    }
    //noinspection ConstantIfStatement
    if (false) {
      //todo why doesn't this work?
      container.addComponent(componentClass);
      return container.getComponent(componentClass);
    }
    if (!addedComponents.containsKey(componentClass)) {
      container.addComponent(componentClass);
      addedComponents.put(componentClass, container.getComponent(componentClass));
    }
    //noinspection unchecked
    return (T) addedComponents.get(componentClass);
  }

  public void addInstance(Object instance) {
    container.addComponent(instance);
    addedComponents.put(instance.getClass(), instance);
  }
}

@@ -202,57 +202,8 @@
 r;%0A%0A
-import java.util.Map;%0Aimport java.util.HashMap;%0A%0A
 /**%0A
@@ -372,97 +372,8 @@
 ner;
-%0A  private final Map%3CClass%3C?%3E, Object%3E addedComponents = new HashMap%3CClass%3C?%3E, Object%3E();
 %0A%0A  
@@ -443,16 +443,30 @@
 ilder().
+withCaching().
 withCons
@@ -684,219 +684,70 @@
     
-return getInstance((Class%3C?%3E) o);%0A        %7D%0A        return null;%0A      %7D%0A    %7D)).build();%0A  %7D%0A%0A  public %3CT%3E T getInstance(Class%3CT%3E componentClass) %7B%0A    @SuppressWarnings(%7B%22unchecked%22%7D)%0A    T instance = (T) adde
+Class%3C?%3E componentClass = (Class%3C?%3E) o;%0A          container.ad
 dCom
@@ -744,37 +744,32 @@
 ner.addComponent
-s.get
 (componentClass)
@@ -778,32 +778,8 @@
     
-if (instance != null) %7B%0A
     
@@ -791,474 +791,184 @@
 urn 
-i
+getI
 nstance
-;%0A    %7D%0A    //noinspection ConstantIfStatement%0A    if (false) %7B%0A      //todo why doesn't this work?%0A      container.addComponent(componentClass);%0A      return container.getComponent(componentClass
+(componentClass);%0A        %7D%0A        return null;%0A      %7D%0A    %7D)).build(
 );%0A  
- 
- 
 %7D
+%0A
 %0A  
-  if (!addedComponents.containsKey(componentClass)) %7B%0A      container.addComponent(componentClass);%0A      addedComponents.put(componentClass, container.getComponent(componentClass));%0A    %7D%0A    //noinspection unchecked%0A    return (T) addedComponents.ge
+public %3CT%3E T getInstance(Class%3CT%3E componentClass) %7B%0A    return container.getComponen
 t(co
@@ -1074,64 +1074,8 @@
 e);%0A
-    addedComponents.put(instance.getClass(), instance);%0A
   %7D%0A

Support auto-registering of requested concrete component classes.