Bug 760956 - Part 1: Introduce variadic concatenateSelectionArgs r=rnewman
authorAndrzej Hunt <ahunt@mozilla.com>
Wed, 17 Feb 2016 09:13:34 -0800
changeset 286229 f0d9ab6ffd64bd61e90dc2a13120df985c91c823
parent 286228 0acb1e76bc20072af11683f06d1958c96dbbcd3e
child 286230 d6e7971cd98b8db5ed78421652e52848d29f71fa
push id72696
push usercbook@mozilla.com
push dateTue, 01 Mar 2016 14:25:42 +0000
treeherdermozilla-inbound@0f47155c48a1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs760956
milestone47.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 760956 - Part 1: Introduce variadic concatenateSelectionArgs r=rnewman This allows us to more easily append multiple sets of args. TODO: is it worth naming this appendSelectionArgs? We should keep the 2-param version appendSelectionArgs since it's more efficient for that case (and the vast majority of uses are with 2-params) - it's probably simpler for development to have both versions have the same name, and as far as I can tell the compiler will prefer the non varargs version when possible. MozReview-Commit-ID: LcI6zNwpIxh
mobile/android/base/java/org/mozilla/gecko/db/DBUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/db/DBUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/db/DBUtils.java
@@ -58,16 +58,44 @@ public class DBUtils {
 
         String[] result = new String[originalValues.length + newValues.length];
         System.arraycopy(originalValues, 0, result, 0, originalValues.length);
         System.arraycopy(newValues, 0, result, originalValues.length, newValues.length);
 
         return result;
     }
 
+    /**
+     * Concatenate multiple lists of selection arguments. <code>values</code> may be <code>null</code>.
+     */
+    public static String[] concatenateSelectionArgs(String[]... values) {
+        // Since we're most likely to be concatenating a few arrays of many values, it is most
+        // efficient to iterate over the arrays once to obtain their lengths, allowing us to create one target array
+        // (as opposed to copying arrays on every iteration, which would result in many more copies).
+        int totalLength = 0;
+        for (String[] v : values) {
+            if (v != null) {
+                totalLength += v.length;
+            }
+        }
+
+        String[] result = new String[totalLength];
+
+        int position = 0;
+        for (String[] v: values) {
+            if (v != null) {
+                int currentLength = v.length;
+                System.arraycopy(v, 0, result, position, currentLength);
+                position += currentLength;
+            }
+        }
+
+        return result;
+    }
+
     public static void replaceKey(ContentValues aValues, String aOriginalKey,
                                   String aNewKey, String aDefault) {
         String value = aDefault;
         if (aOriginalKey != null && aValues.containsKey(aOriginalKey)) {
             value = aValues.get(aOriginalKey).toString();
             aValues.remove(aOriginalKey);
         }