Bug 1437701 - Add GeckoBundle.getLong() r=esawin
MozReview-Commit-ID: K8qWKsP9HE9
--- 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) {