Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 119 additions & 12 deletions src/main/java/android/os/Bundle.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,153 @@
package android.os;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
* A mapping from String keys to values, with the Android class's semantics for the primitive, String and Bundle values
* the ported Firebase libraries use: a getter returns its default when the key is missing or the value has another
* type, {@link #containsKey} answers rather than throws, and keys may be null.
*/
public class Bundle {

private final Map<String, Object> data;

public Bundle() {
this.data = new LinkedHashMap<>();
}

/** A bundle holding a copy of {@code data}, so that it stays mutable whatever map it is created from. */
public Bundle(Map<String, Object> data) {
this.data = data;
this.data = new LinkedHashMap<>(data);
}

public Bundle(Bundle bundle) {
this.data = new HashMap<>(bundle.data);
this.data = new LinkedHashMap<>(bundle.data);
}

public int size() {
return data.size();
}

public boolean isEmpty() {
return data.isEmpty();
}

public void clear() {
data.clear();
}

public boolean containsKey(String key) {
if(data.containsKey(key)) return true;
throw new IllegalArgumentException(key);
return data.containsKey(key);
}

public Object get(String key) {
return data.get(key);
}

public void remove(String key) {
data.remove(key);
}

public void putAll(Bundle bundle) {
data.putAll(bundle.data);
}

public Set<String> keySet() {
return data.keySet();
}

public void putBoolean(String key, boolean value) {
data.put(key, value);
}

public void putInt(String key, int value) {
data.put(key, value);
}

public void putLong(String key, long value) {
data.put(key, value);
}

public void putFloat(String key, float value) {
data.put(key, value);
}

public void putDouble(String key, double value) {
data.put(key, value);
}

public void putString(String key, String value) {
data.put(key, value);
}

public void putBundle(String key, Bundle value) {
data.put(key, value);
}

public boolean getBoolean(String key) {
return getBoolean(key, false);
}

return (Boolean)data.get(key);
public boolean getBoolean(String key, boolean defaultValue) {
return get(key, Boolean.class, defaultValue);
}

public Object get(String key) {
containsKey(key);
return data.get(key);
public int getInt(String key) {
return getInt(key, 0);
}

public int getInt(String key, int defaultValue) {
return get(key, Integer.class, defaultValue);
}

public long getLong(String key) {
return getLong(key, 0L);
}

public long getLong(String key, long defaultValue) {
return get(key, Long.class, defaultValue);
}

public float getFloat(String key) {
return getFloat(key, 0.0f);
}

public float getFloat(String key, float defaultValue) {
return get(key, Float.class, defaultValue);
}

public double getDouble(String key) {
return getDouble(key, 0.0);
}

public double getDouble(String key, double defaultValue) {
return get(key, Double.class, defaultValue);
}

public String getString(String key) {
containsKey(key);
return (String)data.get(key);
return get(key, String.class, null);
}

public int getInt(String key) {
return (Integer)data.get(key);
/** Returns {@code defaultValue} when the key is missing, the value is null or it is not a String. */
public String getString(String key, String defaultValue) {
String value = getString(key);
return value == null ? defaultValue : value;
}

public Bundle getBundle(String key) {
return get(key, Bundle.class, null);
}

private <T> T get(String key, Class<T> type, T defaultValue) {
Object value = data.get(key);
return type.isInstance(value) ? type.cast(value) : defaultValue;
}

@Override
public String toString() {
return "Bundle[" + data + "]";
}
}
76 changes: 76 additions & 0 deletions src/test/kotlin/BundleTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Tests the android.os.Bundle shim against the Android class's semantics for the members the ported
* Firebase libraries and firebase-kotlin-sdk's Analytics use.
*/
import android.os.Bundle
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

class BundleTest {

@Test
fun `stores and reads typed values`() {
val bundle = Bundle()
bundle.putString("string", "value")
bundle.putInt("int", 42)
bundle.putLong("long", 1L shl 40)
bundle.putDouble("double", 42.5)
bundle.putFloat("float", 1.5f)
bundle.putBoolean("boolean", true)
bundle.putBundle("bundle", Bundle().apply { putString("nested", "x") })

assertEquals("value", bundle.getString("string"))
assertEquals(42, bundle.getInt("int"))
assertEquals(1L shl 40, bundle.getLong("long"))
assertEquals(42.5, bundle.getDouble("double"), 0.0)
assertEquals(1.5f, bundle.getFloat("float"), 0.0f)
assertTrue(bundle.getBoolean("boolean"))
assertEquals("x", bundle.getBundle("bundle")?.getString("nested"))
assertEquals(setOf("string", "int", "long", "double", "float", "boolean", "bundle"), bundle.keySet())
assertEquals(7, bundle.size())
assertFalse(bundle.isEmpty())
}

@Test
fun `returns the default for a missing key or another type`() {
val bundle = Bundle()
bundle.putString("string", "value")
bundle.putString("null", null)

assertFalse(bundle.containsKey("missing"))
assertTrue(bundle.containsKey("null"))
assertNull(bundle.get("missing"))
assertNull(bundle.getString("missing"))
assertEquals("default", bundle.getString("missing", "default"))
assertEquals("default", bundle.getString("null", "default"))
assertEquals(0, bundle.getInt("missing"))
assertEquals(7, bundle.getInt("missing", 7))
assertEquals(0, bundle.getInt("string"))
assertEquals(0L, bundle.getLong("string"))
assertEquals(0.0, bundle.getDouble("string"), 0.0)
assertFalse(bundle.getBoolean("string"))
assertTrue(bundle.getBoolean("missing", true))
assertNull(bundle.getBundle("string"))
}

@Test
fun `copies, merges, removes and clears`() {
val source = Bundle(java.util.Collections.singletonMap<String, Any>("a", 1))
source.putString("b", "two") // the map constructor copies, so an immutable map does not make the bundle immutable
val copy = Bundle(source)
copy.putInt("c", 3)
assertEquals(2, source.size())
assertEquals(3, copy.size())

val target = Bundle()
target.putAll(copy)
assertEquals("two", target.getString("b"))
target.remove("b")
assertFalse(target.containsKey("b"))
target.clear()
assertTrue(target.isEmpty())
}
}
Loading