diff options
author | dcashman <dcashman@google.com> | 2014-06-02 17:34:08 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2014-06-02 17:34:09 +0000 |
commit | 559321abbfd2e66763accc172f3596e4cf7f9172 (patch) | |
tree | e94adc95c49eb3cb672f2e17f68e7ae729039d40 /core/java | |
parent | 24bec88db04f17fbaa0022f215efe6931b9bf5c1 (diff) | |
parent | 874d0d4032dc940327a81359f144d38d3cb580a3 (diff) | |
download | frameworks_base-559321abbfd2e66763accc172f3596e4cf7f9172.zip frameworks_base-559321abbfd2e66763accc172f3596e4cf7f9172.tar.gz frameworks_base-559321abbfd2e66763accc172f3596e4cf7f9172.tar.bz2 |
Merge "Add ArrayUtils methods and tests for consumption by KeySet code."
Diffstat (limited to 'core/java')
-rw-r--r-- | core/java/com/android/internal/util/ArrayUtils.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/core/java/com/android/internal/util/ArrayUtils.java b/core/java/com/android/internal/util/ArrayUtils.java index a56fa36..d66ef83 100644 --- a/core/java/com/android/internal/util/ArrayUtils.java +++ b/core/java/com/android/internal/util/ArrayUtils.java @@ -169,6 +169,15 @@ public class ArrayUtils return false; } + public static boolean contains(long[] array, long value) { + for (long element : array) { + if (element == value) { + return true; + } + } + return false; + } + public static long total(long[] array) { long total = 0; for (long value : array) { @@ -229,6 +238,14 @@ public class ArrayUtils return array; } + /** + * Appends a new value to a copy of the array and returns the copy. If + * the value is already present, the original array is returned + * @param cur The original array, or null to represent an empty array. + * @param val The value to add. + * @return A new array that contains all of the values of the original array + * with the new value added, or the original array. + */ public static int[] appendInt(int[] cur, int val) { if (cur == null) { return new int[] { val }; @@ -264,4 +281,48 @@ public class ArrayUtils } return cur; } + + /** + * Appends a new value to a copy of the array and returns the copy. If + * the value is already present, the original array is returned + * @param cur The original array, or null to represent an empty array. + * @param val The value to add. + * @return A new array that contains all of the values of the original array + * with the new value added, or the original array. + */ + public static long[] appendLong(long[] cur, long val) { + if (cur == null) { + return new long[] { val }; + } + final int N = cur.length; + for (int i = 0; i < N; i++) { + if (cur[i] == val) { + return cur; + } + } + long[] ret = new long[N + 1]; + System.arraycopy(cur, 0, ret, 0, N); + ret[N] = val; + return ret; + } + + public static long[] removeLong(long[] cur, long val) { + if (cur == null) { + return null; + } + final int N = cur.length; + for (int i = 0; i < N; i++) { + if (cur[i] == val) { + long[] ret = new long[N - 1]; + if (i > 0) { + System.arraycopy(cur, 0, ret, 0, i); + } + if (i < (N - 1)) { + System.arraycopy(cur, i + 1, ret, i, N - i - 1); + } + return ret; + } + } + return cur; + } } |