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
--- 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);
}