import com.google.common.collect.Iterables;
import java.lang.annotation.Annotation;
+import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import org.picocontainer.Characteristics;
}
}
- // no need for multiple children
- ComponentContainer parent;
- ComponentContainer child;
- MutablePicoContainer pico;
- PropertyDefinitions propertyDefinitions;
- ComponentKeys componentKeys;
+ private ComponentContainer parent;
+ private final List<ComponentContainer> children = new ArrayList<>();
+ private MutablePicoContainer pico;
+ private PropertyDefinitions propertyDefinitions;
+ private ComponentKeys componentKeys;
/**
* Create root container
protected ComponentContainer(MutablePicoContainer picoContainer) {
this.parent = null;
- this.child = null;
this.pico = picoContainer;
this.componentKeys = new ComponentKeys();
propertyDefinitions = new PropertyDefinitions();
protected ComponentContainer(ComponentContainer parent) {
this.parent = parent;
this.pico = parent.pico.makeChildContainer();
- this.parent.child = this;
+ this.parent.children.add(this);
this.propertyDefinitions = parent.propertyDefinitions;
this.componentKeys = new ComponentKeys();
addSingleton(this);
throw PicoUtils.propagate(e);
}
} finally {
- removeChild();
+ removeChildren();
if (parent != null) {
- parent.removeChild();
+ parent.removeChild(this);
}
}
return this;
return pico.getComponents(tClass);
}
- public ComponentContainer removeChild() {
- if (child != null) {
+ public ComponentContainer removeChild(ComponentContainer childToBeRemoved) {
+ for (ComponentContainer child : children) {
+ if (child == childToBeRemoved) {
+ pico.removeChildContainer(child.pico);
+ children.remove(child);
+ break;
+ }
+ }
+ return this;
+ }
+
+ private ComponentContainer removeChildren() {
+ for (ComponentContainer child : children) {
pico.removeChildContainer(child.pico);
- child = null;
}
+ children.clear();
return this;
}
return parent;
}
- public ComponentContainer getChild() {
- return child;
+ public List<ComponentContainer> getChildren() {
+ return children;
}
public MutablePicoContainer getPicoContainer() {
child.startComponents();
assertThat(child.getParent()).isSameAs(parent);
- assertThat(parent.getChild()).isSameAs(child);
+ assertThat(parent.getChildren()).containsOnly(child);
assertThat(child.getComponentByType(ComponentContainer.class)).isSameAs(child);
assertThat(parent.getComponentByType(ComponentContainer.class)).isSameAs(parent);
assertThat(child.getComponentByType(StartableComponent.class)).isNotNull();
parent.startComponents();
ComponentContainer child = parent.createChild();
- assertThat(parent.getChild()).isSameAs(child);
+ assertThat(parent.getChildren()).containsOnly(child);
- parent.removeChild();
- assertThat(parent.getChild()).isNull();
+ parent.removeChild(child);
+ assertThat(parent.getChildren()).isEmpty();
}
+ @Test
+ public void support_multiple_children() {
+ ComponentContainer parent = new ComponentContainer();
+ parent.startComponents();
+ ComponentContainer child1 = parent.createChild();
+ child1.startComponents();
+ ComponentContainer child2 = parent.createChild();
+ child2.startComponents();
+ assertThat(parent.getChildren()).containsOnly(child1, child2);
+
+ child1.stopComponents();
+ assertThat(parent.getChildren()).containsOnly(child2);
+
+ parent.stopComponents();
+ assertThat(parent.getChildren()).isEmpty();
+ }
+
+
@Test
public void shouldForwardStartAndStopToDescendants() {
ComponentContainer grandParent = new ComponentContainer();