package com.danikula.android.garden.utils;

import java.io.Serializable;

import android.os.Bundle;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Contains useful utility methods to simplify work with {@link Bundle}.
 * 
 * @author Alexey Danilov <danikula@gmail.com>
 */
public class Bundles {

    public static Bundle newLongValueBundle(String argName, long longValue) {
        checkNotNull(argName, "Argument's name must be not null!");

        Bundle args = new Bundle();
        args.putLong(argName, longValue);
        return args;
    }

    public static Bundle newIntValueBundle(String argName, int intValue) {
        checkNotNull(argName, "Argument's name must be not null!");

        Bundle args = new Bundle();
        args.putInt(argName, intValue);
        return args;
    }

    public static Bundle newStringValueBundle(String argName, String stringValue) {
        checkNotNull(argName, "Argument's name must be not null!");

        Bundle args = new Bundle();
        args.putString(argName, stringValue);
        return args;
    }

    public static Bundle newBooleanValueBundle(String argName, boolean value) {
        checkNotNull(argName, "Argument's name must be not null!");
        
        Bundle args = new Bundle();
        args.putBoolean(argName, value);
        return args;
    }
    
    public static Bundle newBundle(String argName0, int intValue0, String argName1, int intValue1) {
        checkNotNull(argName0, "Argument's name must be not null!");
        checkNotNull(argName1, "Argument's name must be not null!");
        
        Bundle bundle = newIntValueBundle(argName0, intValue0);
        bundle.putInt(argName1, intValue1);
        return bundle;
    }

    public static Bundle newBundle(String argName0, String stringValue0, String argName1, String stringValue1) {
        checkNotNull(argName0, "Argument's name must be not null!");
        checkNotNull(argName1, "Argument's name must be not null!");
        
        Bundle bundle = newStringValueBundle(argName0, stringValue0);
        bundle.putString(argName1, stringValue1);
        return bundle;
    }
    
    public static long getRequiredLongValue(Bundle bundle, String argName) {
        checkContains(bundle, argName);

        return bundle.getLong(argName);
    }

    public static int getRequiredIntValue(Bundle bundle, String argName) {
        checkContains(bundle, argName);

        return bundle.getInt(argName);
    }

    public static String getRequiredStringValue(Bundle bundle, String argName) {
        checkContains(bundle, argName);

        return bundle.getString(argName);
    }

    public static boolean getRequiredBooleanValue(Bundle bundle, String argName) {
        checkContains(bundle, argName);

        return bundle.getBoolean(argName);
    }

    public static <T> T getRequiredSerializableValue(Bundle bundle, String argName) {
        checkContains(bundle, argName);
        
        return (T) bundle.getSerializable(argName);
    }

    public static void checkContains(Bundle bundle, String... argNames) {
        checkNotNull(bundle, "Bundle must be not null!");
        for (String argName : argNames) {
            checkArgument(bundle.containsKey(argName), "Bundle doesn't contain required argument '%s'", argName);
        }
    }
}

@@ -41,38 +41,8 @@
 s;%0A%0A
-import java.io.Serializable;%0A%0A
 impo
@@ -63,16 +63,16 @@
 Bundle;%0A
+
 %0Aimport 
@@ -275,17 +275,16 @@
 dle%7D.%0A *
- 
 %0A * @aut
@@ -1259,33 +1259,25 @@
 ot null!%22);%0A
-        
 %0A
+
         Bund
@@ -1360,37 +1360,33 @@
 urn args;%0A    %7D%0A
-    
 %0A
+
     public stati
@@ -1604,33 +1604,25 @@
 ot null!%22);%0A
-        
 %0A
+
         Bund
@@ -1998,25 +1998,17 @@
 ull!%22);%0A
-        
 %0A
+
         
@@ -2148,21 +2148,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     publ
@@ -2951,17 +2951,9 @@
 e);%0A
-        
 %0A
+
     
@@ -3291,24 +3291,24 @@
 , argName);%0A
-
         %7D%0A  
@@ -3310,11 +3310,719 @@
  %7D%0A    %7D
+%0A%0A    public static String dump(Bundle bundle) %7B%0A        if (bundle == null) %7B%0A            return %22%22;%0A        %7D%0A        StringBuilder result = new StringBuilder(%22%22);%0A        for (String key : bundle.keySet()) %7B%0A            if (result.length() != 0) %7B%0A                result.append(%22, %22);%0A            %7D%0A            Object value = bundle.get(key);%0A            String valueType = value == null ? %22unknown type%22 : value.getClass().getName();%0A            value = Bundle.class.getName().equals(valueType) ? dump((Bundle) value) : value;%0A            result.append(String.format(%22%25s=%25s (%25s)%22, key, value, valueType));%0A        %7D%0A        result.insert(0, %22Bundle%5B%22).append(%22%5D%22);%0A        return result.toString();%0A    %7D
 %0A%7D%0A

add Bundles.dump