summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Al Sutton <alsutton@google.com> 2019-08-02 07:46:26 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2019-08-02 07:46:26 +0000
commitc71fb23b4bed7f3d279cd6e4f8b0fd9b22d60e8f (patch)
treec414b669736867346835701a7e4cd33ff3349cb5
parent3f214d5867083ca071d42408d3362e7a573cfed0 (diff)
parent3c072e6fe6db9650f0a160647e8c90ab0db42fea (diff)
Merge "Support varargs for concatElements"
-rw-r--r--core/java/com/android/internal/util/ArrayUtils.java64
-rw-r--r--core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java36
2 files changed, 88 insertions, 12 deletions
diff --git a/core/java/com/android/internal/util/ArrayUtils.java b/core/java/com/android/internal/util/ArrayUtils.java
index b73ecd1974aa..e3264a054a43 100644
--- a/core/java/com/android/internal/util/ArrayUtils.java
+++ b/core/java/com/android/internal/util/ArrayUtils.java
@@ -320,23 +320,63 @@ public class ArrayUtils {
return array;
}
+ /**
+ * Combine multiple arrays into a single array.
+ *
+ * @param kind The class of the array elements
+ * @param arrays The arrays to combine
+ * @param <T> The class of the array elements (inferred from kind).
+ * @return A single array containing all the elements of the parameter arrays.
+ */
@SuppressWarnings("unchecked")
- public static @NonNull <T> T[] concatElements(Class<T> kind, @Nullable T[] a, @Nullable T[] b) {
- final int an = (a != null) ? a.length : 0;
- final int bn = (b != null) ? b.length : 0;
- if (an == 0 && bn == 0) {
- if (kind == String.class) {
- return (T[]) EmptyArray.STRING;
- } else if (kind == Object.class) {
- return (T[]) EmptyArray.OBJECT;
+ public static @NonNull <T> T[] concatElements(Class<T> kind, @Nullable T[]... arrays) {
+ if (arrays == null || arrays.length == 0) {
+ return createEmptyArray(kind);
+ }
+
+ int totalLength = 0;
+ int maxLength = 0;
+ T[] maxLengthArray = arrays[0];
+ for (T[] item : arrays) {
+ if (item == null) {
+ continue;
+ }
+
+ totalLength += item.length;
+ if (item.length > maxLength) {
+ maxLengthArray = item;
+ maxLength = item.length;
+ }
+ }
+
+ // Optimization for entirely empty arrays.
+ if (totalLength == 0) {
+ return createEmptyArray(kind);
+ }
+
+ final T[] all = (T[]) Array.newInstance(kind, totalLength);
+ int pos = 0;
+ for (T[] item : arrays) {
+ if (item == null || item.length == 0) {
+ continue;
}
+ System.arraycopy(item, 0, all, pos, item.length);
+ pos += item.length;
}
- final T[] res = (T[]) Array.newInstance(kind, an + bn);
- if (an > 0) System.arraycopy(a, 0, res, 0, an);
- if (bn > 0) System.arraycopy(b, 0, res, an, bn);
- return res;
+ return all;
}
+ private static @NonNull <T> T[] createEmptyArray(Class<T> kind) {
+ if (kind == String.class) {
+ return (T[]) EmptyArray.STRING;
+ } else if (kind == Object.class) {
+ return (T[]) EmptyArray.OBJECT;
+ }
+
+ return (T[]) Array.newInstance(kind, 0);
+ }
+
+
/**
* Adds value to given array if not already present, providing set-like
* behavior.
diff --git a/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java b/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java
index 39bb84a20d7a..cb30b3fe5b82 100644
--- a/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java
+++ b/core/tests/utiltests/src/com/android/internal/util/ArrayUtilsTest.java
@@ -177,4 +177,40 @@ public class ArrayUtilsTest extends TestCase {
assertArrayEquals(new Long[] { 1L, 2L, 3L, 4L },
concatElements(Long.class, new Long[] { 1L, 2L }, new Long[] { 3L, 4L }));
}
+
+ public void testConcatElements_threeWay() {
+ String[] array1 = { "1", "2" };
+ String[] array2 = { "3", "4" };
+ String[] array3 = { "5", "6" };
+ String[] expectation = {"1", "2", "3", "4", "5", "6"};
+
+ String[] concatResult = ArrayUtils.concatElements(String.class, array1, array2, array3);
+ assertArrayEquals(expectation, concatResult);
+ }
+
+
+ public void testConcatElements_threeWayWithNull() {
+ String[] array1 = { "1", "2" };
+ String[] array2 = null;
+ String[] array3 = { "5", "6" };
+ String[] expectation = {"1", "2", "5", "6"};
+
+ String[] concatResult = ArrayUtils.concatElements(String.class, array1, array2, array3);
+ assertArrayEquals(expectation, concatResult);
+ }
+
+ public void testConcatElements_zeroElements() {
+ String[] expectation = new String[0];
+
+ String[] concatResult = ArrayUtils.concatElements(String.class);
+ assertArrayEquals(expectation, concatResult);
+ }
+
+ public void testConcatElements_oneNullElement() {
+ String[] expectation = new String[0];
+
+ String[] concatResult = ArrayUtils.concatElements(String.class, null);
+ assertArrayEquals(expectation, concatResult);
+ }
+
}