Bug 760956 - Part 1: Introduce variadic concatenateSelectionArgs r=rnewman draft
authorAndrzej Hunt <ahunt@mozilla.com>
Wed, 17 Feb 2016 09:13:34 -0800
changeset 334765 fa8ab644085e169ee0bc27f6115e1ddd70a987c3
parent 334764 89eb5bb84d99324522bad7a707e08ff34932e6e9
child 334766 3919c0243930a4757c02d232df5a8d40efbd2309
child 334781 9a65bdbda968008fcfff5a13f13af440cb5c78b4
child 335010 11af4b9b124808d5eb3710bb4ba6055ba960d482
push id11636
push userahunt@mozilla.com
push dateFri, 26 Feb 2016 01:13:00 +0000
reviewersrnewman
bugs760956
milestone47.0a1
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);
         }