Bug 543638 - Improved MultiStatus#add performance

Changed the internal children array to an ArrayList to improve
performance when adding many children and simplify code.

Change-Id: I98fc3547c3d4866f68e7133532c21b98a6657460
Signed-off-by: Julian Honnen <julian.honnen@vector.com>
diff --git a/bundles/org.eclipse.equinox.common/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.common/META-INF/MANIFEST.MF
index 8490b13..8591f04 100644
--- a/bundles/org.eclipse.equinox.common/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.equinox.common/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.equinox.common; singleton:=true
-Bundle-Version: 3.10.200.qualifier
+Bundle-Version: 3.10.300.qualifier
 Bundle-Localization: plugin
 Export-Package: org.eclipse.core.internal.boot;x-friends:="org.eclipse.core.resources,org.eclipse.core.runtime.compatibility,org.eclipse.pde.build",
  org.eclipse.core.internal.runtime;common=split;mandatory:=common;
diff --git a/bundles/org.eclipse.equinox.common/pom.xml b/bundles/org.eclipse.equinox.common/pom.xml
index 0be09dd..b91c9ef 100644
--- a/bundles/org.eclipse.equinox.common/pom.xml
+++ b/bundles/org.eclipse.equinox.common/pom.xml
@@ -19,6 +19,6 @@
   </parent>
   <groupId>org.eclipse.equinox</groupId>
   <artifactId>org.eclipse.equinox.common</artifactId>
-  <version>3.10.200-SNAPSHOT</version>
+  <version>3.10.300-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/MultiStatus.java b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/MultiStatus.java
index a1f875f..a30c485 100644
--- a/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/MultiStatus.java
+++ b/bundles/org.eclipse.equinox.common/src/org/eclipse/core/runtime/MultiStatus.java
@@ -13,6 +13,8 @@
  *******************************************************************************/
 package org.eclipse.core.runtime;
 
+import java.util.*;
+
 /**
  * A concrete multi-status implementation, 
  * suitable either for instantiating or subclassing.
@@ -24,7 +26,7 @@
 
 	/** List of child statuses.
 	 */
-	private IStatus[] children;
+	private final List<IStatus> children = new ArrayList<>();
 
 	/**
 	 * Creates and returns a new multi-status object with the given children.
@@ -40,16 +42,7 @@
 	public MultiStatus(String pluginId, int code, IStatus[] newChildren, String message, Throwable exception) {
 		this(pluginId, code, message, exception);
 		Assert.isLegal(newChildren != null);
-		int maxSeverity = getSeverity();
-		for (int i = 0; i < newChildren.length; i++) {
-			Assert.isLegal(newChildren[i] != null);
-			int severity = newChildren[i].getSeverity();
-			if (severity > maxSeverity)
-				maxSeverity = severity;
-		}
-		this.children = new IStatus[newChildren.length];
-		setSeverity(maxSeverity);
-		System.arraycopy(newChildren, 0, this.children, 0, newChildren.length);
+		addAllInternal(newChildren);
 	}
 
 	/**
@@ -64,7 +57,6 @@
 	 */
 	public MultiStatus(String pluginId, int code, String message, Throwable exception) {
 		super(OK, pluginId, code, message, exception);
-		children = new IStatus[0];
 	}
 
 	/**
@@ -74,10 +66,7 @@
 	 */
 	public void add(IStatus status) {
 		Assert.isLegal(status != null);
-		IStatus[] result = new IStatus[children.length + 1];
-		System.arraycopy(children, 0, result, 0, children.length);
-		result[result.length - 1] = status;
-		children = result;
+		children.add(status);
 		int newSev = status.getSeverity();
 		if (newSev > getSeverity()) {
 			setSeverity(newSev);
@@ -93,15 +82,24 @@
 	 */
 	public void addAll(IStatus status) {
 		Assert.isLegal(status != null);
-		IStatus[] statuses = status.getChildren();
-		for (int i = 0; i < statuses.length; i++) {
-			add(statuses[i]);
+		addAllInternal(status.getChildren());
+	}
+
+	private void addAllInternal(IStatus[] newChildren) {
+		int maxSeverity = getSeverity();
+		for (IStatus child : newChildren) {
+			Assert.isLegal(child != null);
+			int severity = child.getSeverity();
+			if (severity > maxSeverity)
+				maxSeverity = severity;
 		}
+		this.children.addAll(Arrays.asList(newChildren));
+		setSeverity(maxSeverity);
 	}
 
 	@Override
 	public IStatus[] getChildren() {
-		return children;
+		return children.toArray(new IStatus[0]);
 	}
 
 	@Override
@@ -135,15 +133,10 @@
 	 */
 	@Override
 	public String toString() {
-		StringBuffer buf = new StringBuffer(super.toString());
-		buf.append(" children=["); //$NON-NLS-1$
-		for (int i = 0; i < children.length; i++) {
-			if (i != 0) {
-				buf.append(" "); //$NON-NLS-1$
-			}
-			buf.append(children[i].toString());
+		StringJoiner joiner = new StringJoiner(" ", super.toString() + " children=[", "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+		for (IStatus child : children) {
+			joiner.add(child.toString());
 		}
-		buf.append("]"); //$NON-NLS-1$
-		return buf.toString();
+		return joiner.toString();
 	}
 }