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
This commit is contained in:
Eric Biggers
2022-07-29 17:57:51 +00:00
parent 4ce1f9d47d
commit 1174b2fd68
3 changed files with 38 additions and 44 deletions

View File

@@ -359,7 +359,7 @@ public class ArrayUtils {
* @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[]... arrays) {
public static @NonNull <T> T[] concat(Class<T> kind, @Nullable T[]... arrays) {
if (arrays == null || arrays.length == 0) {
return createEmptyArray(kind);
}

View File

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