Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import static com.xtremelabs.robolectric.Robolectric.shadowOf_;
      4 
      5 import java.io.Serializable;
      6 import java.util.ArrayList;
      7 import java.util.HashMap;
      8 import java.util.Map;
      9 import java.util.Set;
     10 
     11 import android.os.Build;
     12 import android.os.Bundle;
     13 import android.os.Parcelable;
     14 
     15 import com.xtremelabs.robolectric.Robolectric;
     16 import com.xtremelabs.robolectric.internal.Implementation;
     17 import com.xtremelabs.robolectric.internal.Implements;
     18 
     19 @SuppressWarnings({ "UnusedDeclaration" })
     20 @Implements(Bundle.class)
     21 public class ShadowBundle {
     22     Map<String, Object> map = new HashMap<String, Object>();
     23 
     24     public void __constructor__(Bundle b) {
     25         putAll(b);
     26     }
     27 
     28     @Implementation
     29     public void clear() {
     30         map.clear();
     31     }
     32 
     33     @Implementation
     34     public void remove(String key) {
     35         map.remove(key);
     36     }
     37 
     38     @Implementation
     39     public Object get(String key) {
     40         return map.get(key);
     41     }
     42 
     43     @Implementation
     44     public void putString(String key, String value) {
     45         map.put(key, value);
     46     }
     47 
     48     @Implementation
     49     public String getString(String key) {
     50         Object value = map.get(key);
     51         return value == null || !(value instanceof String) ? null : (String) value;
     52     }
     53 
     54     @Implementation
     55     public String getString(String key, String defaultValue) {
     56         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
     57             throw new RuntimeException(new NoSuchMethodException("Requires API_12"));
     58         }
     59         Object value = map.get(key);
     60         return value == null || !(value instanceof String) ? defaultValue : (String) value;
     61     }
     62 
     63     @Implementation
     64     public void putLong(String key, long value) {
     65         map.put(key, value);
     66     }
     67 
     68     @Implementation
     69     public long getLong(String key) {
     70         return getLong(key, 0);
     71     }
     72 
     73     @Implementation
     74     public long getLong(String key, long defaultValue) {
     75         Object value = map.get(key);
     76         return value == null || !(value instanceof Long) ? defaultValue : (Long) value;
     77     }
     78 
     79     @Implementation
     80     public void putInt(String key, int value) {
     81         map.put(key, value);
     82     }
     83 
     84     @Implementation
     85     public int getInt(String key) {
     86         return getInt(key, 0);
     87     }
     88 
     89     @Implementation
     90     public int getInt(String key, int defaultValue) {
     91         Object value = map.get(key);
     92         return value == null || !(value instanceof Integer) ? defaultValue : (Integer) value;
     93     }
     94 
     95     @Implementation
     96     public void putDouble(String key, double value) {
     97         map.put(key, value);
     98     }
     99 
    100     @Implementation
    101     public double getDouble(String key) {
    102         return getDouble(key, 0);
    103     }
    104 
    105     @Implementation
    106     public double getDouble(String key, double defaultValue) {
    107         Object value = map.get(key);
    108         return value == null || !(value instanceof Double) ? defaultValue : (Double) value;
    109     }
    110 
    111     @Implementation
    112     public void putByte(String key, byte value) {
    113         map.put(key, value);
    114     }
    115 
    116     @Implementation
    117     public byte getByte(String key) {
    118         return getByte(key, (byte) 0);
    119     }
    120 
    121     @Implementation
    122     public Byte getByte(String key, byte defaultValue) {
    123         Object value = map.get(key);
    124         return value == null || !(value instanceof Byte) ? defaultValue : (Byte) value;
    125     }
    126 
    127     @Implementation
    128     public void putBoolean(String key, boolean value) {
    129         map.put(key, value);
    130     }
    131 
    132     @Implementation
    133     public boolean getBoolean(String key) {
    134         return getBoolean(key, false);
    135     }
    136 
    137     @Implementation
    138     public boolean getBoolean(String key, boolean defaultValue) {
    139         Object value = map.get(key);
    140         return value == null || !(value instanceof Boolean) ? defaultValue : (Boolean) value;
    141     }
    142 
    143     @Implementation
    144     public void putChar(String key, char value) {
    145         map.put(key, value);
    146     }
    147 
    148     @Implementation
    149     public char getChar(String key) {
    150         return getChar(key, (char) 0);
    151     }
    152 
    153     @Implementation
    154     public char getChar(String key, char defaultValue) {
    155         Object value = map.get(key);
    156         return value == null || !(value instanceof Character) ? defaultValue : (Character) value;
    157     }
    158 
    159     @Implementation
    160     public void putCharSequence(String key, CharSequence value) {
    161         map.put(key, value);
    162     }
    163 
    164     @Implementation
    165     public CharSequence getCharSequence(String key) {
    166         Object value = map.get(key);
    167         return value == null || !(value instanceof CharSequence) ? null : (CharSequence) value;
    168     }
    169 
    170     @Implementation
    171     public void putShort(String key, short value) {
    172         map.put(key, value);
    173     }
    174 
    175     @Implementation
    176     public short getShort(String key) {
    177         return getShort(key, (short) 0);
    178     }
    179 
    180     @Implementation
    181     public short getShort(String key, short defaultValue) {
    182         Object value = map.get(key);
    183         return value == null || !(value instanceof Short) ? defaultValue : (Short) value;
    184     }
    185 
    186     @Implementation
    187     public void putFloat(String key, float value) {
    188         map.put(key, value);
    189     }
    190 
    191     @Implementation
    192     public float getFloat(String key) {
    193         return getFloat(key, 0);
    194     }
    195 
    196     @Implementation
    197     public float getFloat(String key, float defaultValue) {
    198         Object value = map.get(key);
    199         return value == null || !(value instanceof Float) ? defaultValue : (Float) value;
    200     }
    201 
    202     @Implementation
    203     public void putSerializable(String key, Serializable value) {
    204         map.put(key, value);
    205     }
    206 
    207     @Implementation
    208     public Serializable getSerializable(String key) {
    209         Object value = map.get(key);
    210         return value == null || !(value instanceof Serializable) ? null : (Serializable) value;
    211     }
    212 
    213     @Implementation
    214     public void putParcelable(String key, Parcelable value) {
    215         map.put(key, value);
    216     }
    217 
    218     @Implementation
    219     public void putParcelableArrayList(String key, ArrayList<? extends Parcelable> value) {
    220         map.put(key, value);
    221     }
    222 
    223     @Implementation
    224     public Parcelable getParcelable(String key) {
    225         Object value = map.get(key);
    226         return value == null || !(value instanceof Parcelable) ? null : (Parcelable) value;
    227     }
    228 
    229     @Implementation
    230     public ArrayList<Parcelable> getParcelableArrayList(String key) {
    231         Object value = map.get(key);
    232         return value == null || !(value instanceof ArrayList) ? null
    233                 : (ArrayList<Parcelable>) value;
    234     }
    235 
    236     @Implementation
    237     public Parcelable[] getParcelableArray(String key) {
    238         Object value = map.get(key);
    239         return value == null || !(value instanceof Parcelable[]) ? null : (Parcelable[]) value;
    240     }
    241 
    242     @Implementation
    243     public void putParcelableArray(String key, Parcelable[] value) {
    244         map.put(key, value);
    245     }
    246 
    247     @Implementation
    248     public void putStringArrayList(String key, ArrayList<String> value) {
    249         map.put(key, value);
    250     }
    251 
    252     @Implementation
    253     public ArrayList<String> getStringArrayList(String key) {
    254         Object value = map.get(key);
    255         return value == null || !(value instanceof ArrayList) ? null : (ArrayList<String>) value;
    256     }
    257 
    258     @Implementation
    259     public void putCharSequenceArrayList(String key, ArrayList<CharSequence> value) {
    260         map.put(key, value);
    261     }
    262 
    263     @Implementation
    264     public ArrayList<CharSequence> getCharSequenceArrayList(String key) {
    265         Object value = map.get(key);
    266         return value == null || !(value instanceof ArrayList) ? null
    267                 : (ArrayList<CharSequence>) value;
    268     }
    269 
    270     @Implementation
    271     public void putIntegerArrayList(String key, ArrayList<Integer> value) {
    272         map.put(key, value);
    273     }
    274 
    275     @Implementation
    276     public ArrayList<Integer> getIntegerArrayList(String key) {
    277         Object value = map.get(key);
    278         return value == null || !(value instanceof ArrayList) ? null : (ArrayList<Integer>) value;
    279     }
    280 
    281     @Implementation
    282     public void putBundle(String key, Bundle value) {
    283         map.put(key, value);
    284     }
    285 
    286     @Implementation
    287     public Bundle getBundle(String key) {
    288         Object value = map.get(key);
    289         return value == null || !(value instanceof Bundle) ? null : (Bundle) value;
    290     }
    291 
    292     @Implementation
    293     public void putBooleanArray(String key, boolean[] value) {
    294         map.put(key, value);
    295     }
    296 
    297     @Implementation
    298     public boolean[] getBooleanArray(String key) {
    299         Object value = map.get(key);
    300         return value == null || !(value instanceof boolean[]) ? null : (boolean[]) value;
    301     }
    302 
    303     @Implementation
    304     public void putByteArray(String key, byte[] value) {
    305         map.put(key, value);
    306     }
    307 
    308     @Implementation
    309     public byte[] getByteArray(String key) {
    310         Object value = map.get(key);
    311         return value == null || !(value instanceof byte[]) ? null : (byte[]) value;
    312     }
    313 
    314     @Implementation
    315     public void putCharArray(String key, char[] value) {
    316         map.put(key, value);
    317     }
    318 
    319     @Implementation
    320     public char[] getCharArray(String key) {
    321         Object value = map.get(key);
    322         return value == null || !(value instanceof char[]) ? null : (char[]) value;
    323     }
    324 
    325     @Implementation
    326     public void putDoubleArray(String key, double[] value) {
    327         map.put(key, value);
    328     }
    329 
    330     @Implementation
    331     public double[] getDoubleArray(String key) {
    332         Object value = map.get(key);
    333         return value == null || !(value instanceof double[]) ? null : (double[]) value;
    334     }
    335 
    336     @Implementation
    337     public void putFloatArray(String key, float[] value) {
    338         map.put(key, value);
    339     }
    340 
    341     @Implementation
    342     public float[] getFloatArray(String key) {
    343         Object value = map.get(key);
    344         return value == null || !(value instanceof float[]) ? null : (float[]) value;
    345     }
    346 
    347     @Implementation
    348     public void putIntArray(String key, int[] value) {
    349         map.put(key, value);
    350     }
    351 
    352     @Implementation
    353     public int[] getIntArray(String key) {
    354         Object value = map.get(key);
    355         return value == null || !(value instanceof int[]) ? null : (int[]) value;
    356     }
    357 
    358     @Implementation
    359     public void putLongArray(String key, long[] value) {
    360         map.put(key, value);
    361     }
    362 
    363     @Implementation
    364     public long[] getLongArray(String key) {
    365         Object value = map.get(key);
    366         return value == null || !(value instanceof long[]) ? null : (long[]) value;
    367     }
    368 
    369     @Implementation
    370     public void putShortArray(String key, short[] value) {
    371         map.put(key, value);
    372     }
    373 
    374     @Implementation
    375     public short[] getShortArray(String key) {
    376         Object value = map.get(key);
    377         return value == null || !(value instanceof short[]) ? null : (short[]) value;
    378     }
    379 
    380     @Implementation
    381     public void putAll(Bundle bundle) {
    382         map.putAll(((ShadowBundle) Robolectric.shadowOf_(bundle)).map);
    383     }
    384 
    385     @Implementation
    386     public void putStringArray(String key, String[] value) {
    387         map.put(key, value);
    388     }
    389 
    390     @Implementation
    391     public String[] getStringArray(String key) {
    392         Object value = map.get(key);
    393         return value == null || !(value instanceof String[]) ? null : (String[]) value;
    394     }
    395 
    396     @Implementation
    397     public void putCharSequenceArray(String key, CharSequence[] value) {
    398         map.put(key, value);
    399     }
    400 
    401     @Implementation
    402     public CharSequence[] getCharSequenceArray(String key) {
    403         Object value = map.get(key);
    404         return value == null || !(value instanceof CharSequence[]) ? null : (CharSequence[]) value;
    405     }
    406 
    407     @Implementation
    408     public boolean containsKey(String key) {
    409         return map.containsKey(key);
    410     }
    411 
    412     @Implementation
    413     public boolean isEmpty() {
    414         return map.isEmpty();
    415     }
    416 
    417     @Implementation
    418     public Set<String> keySet() {
    419         return map.keySet();
    420     }
    421 
    422     @Implementation
    423     public int size() {
    424         return map.size();
    425     }
    426 
    427     @Override
    428     @Implementation
    429     public boolean equals(Object o) {
    430         if (o == null)
    431             return false;
    432         o = shadowOf_(o);
    433         if (o == null)
    434             return false;
    435         if (this == o)
    436             return true;
    437         if (getClass() != o.getClass())
    438             return false;
    439 
    440         ShadowBundle that = (ShadowBundle) o;
    441 
    442         if (map != null ? !map.equals(that.map) : that.map != null)
    443             return false;
    444 
    445         return true;
    446     }
    447 
    448     @Override
    449     @Implementation
    450     public int hashCode() {
    451         return map != null ? map.hashCode() : 0;
    452     }
    453 }
    454