From e93851854a5ab728fba5e83b81b7a4a24208481a Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 12 Feb 2016 14:53:59 +0100 Subject: Refactor a bit ComponentContainer --- .../sonar/core/platform/ComponentContainer.java | 25 ++++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'sonar-core') diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java index 3d01d8f482e..6e7d1161bb4 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java @@ -22,6 +22,7 @@ package org.sonar.core.platform; import com.google.common.collect.Iterables; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import javax.annotation.Nullable; import org.picocontainer.Characteristics; @@ -41,6 +42,9 @@ import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; +import static com.google.common.collect.ImmutableList.copyOf; +import static java.util.Objects.requireNonNull; + @BatchSide @ServerSide public class ComponentContainer implements ContainerPopulator.Container { @@ -270,10 +274,14 @@ public class ComponentContainer implements ContainerPopulator.Container { } public ComponentContainer removeChild(ComponentContainer childToBeRemoved) { - for (ComponentContainer child : children) { + requireNonNull(childToBeRemoved); + Iterator childrenIterator = children.iterator(); + while (childrenIterator.hasNext()) { + ComponentContainer child = childrenIterator.next(); if (child == childToBeRemoved) { - pico.removeChildContainer(child.pico); - children.remove(child); + if (pico.removeChildContainer(child.pico)) { + childrenIterator.remove(); + } break; } } @@ -281,10 +289,13 @@ public class ComponentContainer implements ContainerPopulator.Container { } private ComponentContainer removeChildren() { - for (ComponentContainer child : children) { - pico.removeChildContainer(child.pico); + Iterator childrenIterator = children.iterator(); + while (childrenIterator.hasNext()) { + ComponentContainer child = childrenIterator.next(); + if (pico.removeChildContainer(child.pico)) { + childrenIterator.remove(); + } } - children.clear(); return this; } @@ -310,7 +321,7 @@ public class ComponentContainer implements ContainerPopulator.Container { } public List getChildren() { - return children; + return copyOf(children); } public MutablePicoContainer getPicoContainer() { -- cgit v1.2.3