ArrayUtils: rename concatElements() to concat()

concatElements() was probably named as it is to be like addElement() and
removeElement().  However, that doesn't really make sense because the
parameters of concatElements() are only arrays, not array elements.

Rename it to concat(), as that is just as clear and follows the example
of Guava's ObjectArrays.concat().  And I'd like to add a variant that
operates on byte arrays, and would like to call it concat().

Also clean up the tests a bit.

Test: atest com.android.internal.util.ArrayUtilsTest
BYPASS_INCLUSIVE_LANGUAGE_REASON=existing code in SettingsBackupAgent
Change-Id: I21e958d3d7b3366ec724e757ac4ccc50b36abee1
Merged-In: I21e958d3d7b3366ec724e757ac4ccc50b36abee1
(cherry picked from commit 1174b2fd68)
This commit is contained in:
Eric Biggers
2022-07-29 17:57:51 +00:00
committed by William Escande
parent c97d692350
commit 96abd9452d
3 changed files with 38 additions and 44 deletions

View File

@@ -349,7 +349,7 @@ public class ArrayUtils {
* @return A single array containing all the elements of the parameter arrays. * @return A single array containing all the elements of the parameter arrays.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static @NonNull <T> T[] concatElements(Class<T> kind, @Nullable T[]... arrays) { public static @NonNull <T> T[] concat(Class<T> kind, @Nullable T[]... arrays) {
if (arrays == null || arrays.length == 0) { if (arrays == null || arrays.length == 0) {
return createEmptyArray(kind); return createEmptyArray(kind);
} }

View File

@@ -16,8 +16,6 @@
package com.android.internal.util; package com.android.internal.util;
import static com.android.internal.util.ArrayUtils.concatElements;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import junit.framework.TestCase; import junit.framework.TestCase;
@@ -156,61 +154,57 @@ public class ArrayUtilsTest extends TestCase {
ArrayUtils.removeLong(new long[] { 1, 2, 3, 1 }, 1)); ArrayUtils.removeLong(new long[] { 1, 2, 3, 1 }, 1));
} }
public void testConcatEmpty() throws Exception { public void testConcat_zeroObjectArrays() {
assertArrayEquals(new Long[] {}, // empty varargs array
concatElements(Long.class, null, null)); assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class));
assertArrayEquals(new Long[] {}, // null varargs array
concatElements(Long.class, new Long[] {}, null)); assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class, (String[][]) null));
assertArrayEquals(new Long[] {},
concatElements(Long.class, null, new Long[] {}));
assertArrayEquals(new Long[] {},
concatElements(Long.class, new Long[] {}, new Long[] {}));
} }
public void testconcatElements() throws Exception { public void testConcat_oneObjectArray() {
assertArrayEquals(new String[] { "1", "2" },
ArrayUtils.concat(String.class, new String[] { "1", "2" }));
}
public void testConcat_oneEmptyObjectArray() {
assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class, (String[]) null));
assertArrayEquals(new String[] {}, ArrayUtils.concat(String.class, new String[] {}));
}
public void testConcat_twoObjectArrays() {
assertArrayEquals(new Long[] { 1L }, assertArrayEquals(new Long[] { 1L },
concatElements(Long.class, new Long[] { 1L }, new Long[] {})); ArrayUtils.concat(Long.class, new Long[] { 1L }, new Long[] {}));
assertArrayEquals(new Long[] { 1L }, assertArrayEquals(new Long[] { 1L },
concatElements(Long.class, new Long[] {}, new Long[] { 1L })); ArrayUtils.concat(Long.class, new Long[] {}, new Long[] { 1L }));
assertArrayEquals(new Long[] { 1L, 2L }, assertArrayEquals(new Long[] { 1L, 2L },
concatElements(Long.class, new Long[] { 1L }, new Long[] { 2L })); ArrayUtils.concat(Long.class, new Long[] { 1L }, new Long[] { 2L }));
assertArrayEquals(new Long[] { 1L, 2L, 3L, 4L }, assertArrayEquals(new Long[] { 1L, 2L, 3L, 4L },
concatElements(Long.class, new Long[] { 1L, 2L }, new Long[] { 3L, 4L })); ArrayUtils.concat(Long.class, new Long[] { 1L, 2L }, new Long[] { 3L, 4L }));
} }
public void testConcatElements_threeWay() { public void testConcat_twoEmptyObjectArrays() {
assertArrayEquals(new Long[] {}, ArrayUtils.concat(Long.class, null, null));
assertArrayEquals(new Long[] {}, ArrayUtils.concat(Long.class, new Long[] {}, null));
assertArrayEquals(new Long[] {}, ArrayUtils.concat(Long.class, null, new Long[] {}));
assertArrayEquals(new Long[] {},
ArrayUtils.concat(Long.class, new Long[] {}, new Long[] {}));
}
public void testConcat_threeObjectArrays() {
String[] array1 = { "1", "2" }; String[] array1 = { "1", "2" };
String[] array2 = { "3", "4" }; String[] array2 = { "3", "4" };
String[] array3 = { "5", "6" }; String[] array3 = { "5", "6" };
String[] expectation = { "1", "2", "3", "4", "5", "6" }; String[] expectation = { "1", "2", "3", "4", "5", "6" };
String[] concatResult = ArrayUtils.concatElements(String.class, array1, array2, array3); assertArrayEquals(expectation, ArrayUtils.concat(String.class, array1, array2, array3));
assertArrayEquals(expectation, concatResult);
} }
public void testConcat_threeObjectArraysWithNull() {
public void testConcatElements_threeWayWithNull() {
String[] array1 = { "1", "2" }; String[] array1 = { "1", "2" };
String[] array2 = null; String[] array2 = null;
String[] array3 = { "5", "6" }; String[] array3 = { "5", "6" };
String[] expectation = { "1", "2", "5", "6" }; String[] expectation = { "1", "2", "5", "6" };
String[] concatResult = ArrayUtils.concatElements(String.class, array1, array2, array3); assertArrayEquals(expectation, ArrayUtils.concat(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);
}
} }

View File

@@ -875,16 +875,16 @@ public class SettingsBackupAgent extends BackupAgentHelper {
String[] whitelist; String[] whitelist;
Map<String, Validator> validators = null; Map<String, Validator> validators = null;
if (contentUri.equals(Settings.Secure.CONTENT_URI)) { if (contentUri.equals(Settings.Secure.CONTENT_URI)) {
whitelist = ArrayUtils.concatElements(String.class, SecureSettings.SETTINGS_TO_BACKUP, whitelist = ArrayUtils.concat(String.class, SecureSettings.SETTINGS_TO_BACKUP,
Settings.Secure.LEGACY_RESTORE_SETTINGS, Settings.Secure.LEGACY_RESTORE_SETTINGS,
DeviceSpecificSettings.DEVICE_SPECIFIC_SETTINGS_TO_BACKUP); DeviceSpecificSettings.DEVICE_SPECIFIC_SETTINGS_TO_BACKUP);
validators = SecureSettingsValidators.VALIDATORS; validators = SecureSettingsValidators.VALIDATORS;
} else if (contentUri.equals(Settings.System.CONTENT_URI)) { } else if (contentUri.equals(Settings.System.CONTENT_URI)) {
whitelist = ArrayUtils.concatElements(String.class, SystemSettings.SETTINGS_TO_BACKUP, whitelist = ArrayUtils.concat(String.class, SystemSettings.SETTINGS_TO_BACKUP,
Settings.System.LEGACY_RESTORE_SETTINGS); Settings.System.LEGACY_RESTORE_SETTINGS);
validators = SystemSettingsValidators.VALIDATORS; validators = SystemSettingsValidators.VALIDATORS;
} else if (contentUri.equals(Settings.Global.CONTENT_URI)) { } else if (contentUri.equals(Settings.Global.CONTENT_URI)) {
whitelist = ArrayUtils.concatElements(String.class, GlobalSettings.SETTINGS_TO_BACKUP, whitelist = ArrayUtils.concat(String.class, GlobalSettings.SETTINGS_TO_BACKUP,
Settings.Global.LEGACY_RESTORE_SETTINGS); Settings.Global.LEGACY_RESTORE_SETTINGS);
validators = GlobalSettingsValidators.VALIDATORS; validators = GlobalSettingsValidators.VALIDATORS;
} else { } else {