Bug 1437701 - Add GeckoBundle.getLong() r=esawin draft
authorJames Willcox <snorp@snorp.net>
Tue, 27 Mar 2018 14:00:04 -0500
changeset 774437 6a76caab52b3680dbf4c7ca431ec315ae60ddb86
parent 774436 d8368cd1423a2e985d7efd086ed22ecbcc818f26
child 774438 efdc4d6f10238cdb5794d0b0df3f88b098c3415c
push id104401
push userbmo:snorp@snorp.net
push dateWed, 28 Mar 2018 22:12:07 +0000
reviewersesawin
bugs1437701
milestone61.0a1
Bug 1437701 - Add GeckoBundle.getLong() r=esawin MozReview-Commit-ID: K8qWKsP9HE9
mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/GeckoBundle.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/GeckoBundle.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/GeckoBundle.java
@@ -30,16 +30,17 @@ import java.util.Iterator;
 @RobocopTarget
 public final class GeckoBundle implements Parcelable {
     private static final String LOGTAG = "GeckoBundle";
     private static final boolean DEBUG = false;
 
     @WrapForJNI(calledFrom = "gecko")
     private static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
     private static final int[] EMPTY_INT_ARRAY = new int[0];
+    private static final long[] EMPTY_LONG_ARRAY = new long[0];
     private static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
     private static final String[] EMPTY_STRING_ARRAY = new String[0];
     private static final GeckoBundle[] EMPTY_BUNDLE_ARRAY = new GeckoBundle[0];
 
     @WrapForJNI(calledFrom = "gecko")
     private static Object box(boolean b) { return b; }
     @WrapForJNI(calledFrom = "gecko")
     private static Object box(int i) { return i; }
@@ -243,16 +244,62 @@ public final class GeckoBundle implement
      */
     public int[] getIntArray(final String key) {
         final Object value = mMap.get(key);
         return value == null ? null : Array.getLength(value) == 0 ? EMPTY_INT_ARRAY :
                value instanceof double[] ? getIntArray((double[]) value) : (int[]) value;
     }
 
     /**
+     * Returns the value associated with a long mapping, or defaultValue if the mapping
+     * does not exist.
+     *
+     * @param key Key to look for.
+     * @param defaultValue Value to return if mapping does not exist.
+     * @return long value
+     */
+    public long getLong(final String key, final long defaultValue) {
+        final Object value = mMap.get(key);
+        return value == null ? defaultValue : ((Number) value).longValue();
+    }
+
+    /**
+     * Returns the value associated with a long mapping, or defaultValue if the mapping
+     * does not exist.
+     *
+     * @param key Key to look for.
+     * @return long value
+     */
+    public long getLong(final String key) {
+        return getLong(key, 0L);
+    }
+
+    private static long[] getLongArray(final double[] array) {
+        final int len = array.length;
+        final long[] ret = new long[len];
+        for (int i = 0; i < len; i++) {
+            ret[i] = (long) array[i];
+        }
+        return ret;
+    }
+
+    /**
+     * Returns the value associated with a long array mapping, or null if the mapping does
+     * not exist.
+     *
+     * @param key Key to look for.
+     * @return long array value
+     */
+    public long[] getLongArray(final String key) {
+        final Object value = mMap.get(key);
+        return value == null ? null : Array.getLength(value) == 0 ? EMPTY_LONG_ARRAY :
+                value instanceof double[] ? getLongArray((double[]) value) : (long[]) value;
+    }
+
+    /**
      * Returns the value associated with a String mapping, or defaultValue if the mapping
      * does not exist.
      *
      * @param key Key to look for.
      * @param defaultValue Value to return if mapping value is null or mapping does not exist.
      * @return String value
      */
     public String getString(final String key, final String defaultValue) {