]> source.dussan.org Git - sonarqube.git/commitdiff
Refactor a bit ComponentContainer
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Feb 2016 13:53:59 +0000 (14:53 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 12 Feb 2016 16:10:56 +0000 (17:10 +0100)
sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java

index 3d01d8f482e55fdc2dcf533af7aac9931434f452..6e7d1161bb422d16822a066c0363d8c0db2eeb89 100644 (file)
@@ -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<ComponentContainer> 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<ComponentContainer> 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<ComponentContainer> getChildren() {
-    return children;
+    return copyOf(children);
   }
 
   public MutablePicoContainer getPicoContainer() {