diff options
author | Sergey Prigogin | 2009-10-04 05:43:07 +0000 |
---|---|---|
committer | Sergey Prigogin | 2009-10-04 05:43:07 +0000 |
commit | 68a906b0a6380ca0f83cf2713e4c183bd3e0fe79 (patch) | |
tree | bab6b9bd5c506ac25eb271bc70c347931da1b135 | |
parent | 508050270cd4cd3b99a94ee252b7d1e92c29765f (diff) | |
download | org.eclipse.cdt-68a906b0a6380ca0f83cf2713e4c183bd3e0fe79.tar.gz org.eclipse.cdt-68a906b0a6380ca0f83cf2713e4c183bd3e0fe79.tar.xz org.eclipse.cdt-68a906b0a6380ca0f83cf2713e4c183bd3e0fe79.zip |
Type safe version of addAll.
-rw-r--r-- | core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ArrayUtil.java | 57 |
1 files changed, 48 insertions, 9 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ArrayUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ArrayUtil.java index aec42dd23d7..7d7b5b26317 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ArrayUtil.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/ArrayUtil.java @@ -10,6 +10,7 @@ * Markus Schorn (Wind River Systems) * Andrew Ferguson (Symbian) * Mike Kucera (IBM) + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.core.parser.util; @@ -17,9 +18,8 @@ import java.lang.reflect.Array; /** * @noextend This interface is not intended to be extended by clients. - * @noinstantiate This class is not intended to be instantiated by clients. */ -public class ArrayUtil { +public abstract class ArrayUtil { private static final int DEFAULT_LENGTH = 2; /** @@ -56,18 +56,17 @@ public class ArrayUtil { private static int findFirstNull(Object[] array) { boolean haveNull= false; int left= 0; - int right= array.length-1; + int right= array.length - 1; while (left <= right) { - int mid= (left+right)/2; + int mid= (left + right) / 2; if (array[mid] == null) { haveNull= true; - right= mid-1; - } - else { - left= mid+1; + right= mid - 1; + } else { + left= mid + 1; } } - return haveNull ? right+1 : -1; + return haveNull ? right + 1 : -1; } @@ -245,6 +244,46 @@ public class ArrayUtil { System.arraycopy(source, 0, temp, firstFree, numToAdd); return temp; } + + /** + * Concatenates two arrays skipping <code>null</code> elements. + * Assumes that both arrays contain <code>null</code>s at the end, only. + * @since 5.2 + */ + @SuppressWarnings("unchecked") + public static <T> T[] addAll(T[] dest, T[] source) { + if (source == null || source.length == 0) + return dest; + + int numToAdd = findFirstNull(source); + if (numToAdd <= 0) { + if (numToAdd == 0) { + return dest; + } + numToAdd= source.length; + } + + if (dest == null || dest.length == 0) { + Class c = dest != null ? dest.getClass().getComponentType() : source.getClass().getComponentType(); + dest = (T[]) Array.newInstance(c, numToAdd); + System.arraycopy(source, 0, dest, 0, numToAdd); + return dest; + } + + int firstFree = findFirstNull(dest); + if (firstFree < 0) { + firstFree= dest.length; + } + + if (firstFree + numToAdd <= dest.length) { + System.arraycopy(source, 0, dest, firstFree, numToAdd); + return dest; + } + T[] temp = (T[]) Array.newInstance(dest.getClass().getComponentType(), firstFree + numToAdd); + System.arraycopy(dest, 0, temp, 0, firstFree); + System.arraycopy(source, 0, temp, firstFree, numToAdd); + return temp; + } /** * Returns whether the specified array contains the specified object. Comparison is by |