package net.ichigotake.common.os;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;

import java.io.Serializable;

public final class RestoreBundle {

    private final Bundle arguments;
    private final Bundle savedInstanceState;

    public RestoreBundle(Intent intent, Bundle savedInstanceState) {
        this((intent != null) ? intent.getExtras() : null, savedInstanceState);
    }

    public RestoreBundle(Fragment fragment, Bundle savedInstanceState) {
        this(fragment.getArguments(), savedInstanceState);
    }

    private RestoreBundle(Bundle arguments, Bundle savedInstanceState) {
        this.arguments = (arguments != null) ? arguments : new Bundle();
        this.savedInstanceState = (savedInstanceState != null) ? savedInstanceState : new Bundle();
    }

    public boolean containsKey(String key) {
        return arguments.containsKey(key) || savedInstanceState.containsKey(key);
    }

    public <T extends Parcelable> T getParcelable(String key) {
        T restoreObject;
        if (savedInstanceState.containsKey(key)) {
            restoreObject = savedInstanceState.getParcelable(key);
        } else if (arguments.containsKey(key)) {
            restoreObject = arguments.getParcelable(key);
        } else {
            restoreObject = null;
        }
        return restoreObject;
    }

    public <T extends Serializable> T getSerializable(String key) {
        T restoreObject;
        if (savedInstanceState.containsKey(key)) {
            restoreObject = (T)savedInstanceState.getSerializable(key);
        } else if (arguments.containsKey(key)) {
            restoreObject = (T)arguments.getSerializable(key);
        } else {
            restoreObject = null;
        }
        return restoreObject;
    }

    public int getInt(String key) {
        int restoreObject;
        if (savedInstanceState.containsKey(key)) {
            restoreObject = savedInstanceState.getInt(key);
        } else if (arguments.containsKey(key)) {
            restoreObject = arguments.getInt(key);
        } else {
            restoreObject = 0;
        }
        return restoreObject;
    }
}

@@ -251,61 +251,13 @@
 dle 
-arguments;%0A    private final Bundle savedInstanceStat
+bundl
 e;%0A%0A
@@ -547,24 +547,135 @@
 te);%0A    %7D%0A%0A
+    public RestoreBundle(Bundle bundle) %7B%0A        this.bundle = bundle != null ? bundle : new Bundle();%0A    %7D%0A%0A
     private 
@@ -747,17 +747,23 @@
     this
-.
+(merge(
 argument
@@ -767,157 +767,567 @@
 ents
- = (arguments != null) ? arguments : new Bundle();%0A        this.savedInstanceState = (savedInstanceState != null) ? savedInstanceState : new Bundle()
+, savedInstanceState));%0A    %7D%0A%0A    private static Bundle merge(Bundle arguments, Bundle savedInstanceState) %7B%0A        Bundle mergedBundle;%0A        if (arguments != null && savedInstanceState != null) %7B%0A            arguments.putAll(savedInstanceState);%0A            mergedBundle = arguments;%0A        %7D else if (arguments != null) %7B%0A            mergedBundle = arguments;%0A        %7D else if (savedInstanceState != null) %7B%0A            mergedBundle = savedInstanceState;%0A        %7D else %7B%0A            mergedBundle = new Bundle();%0A        %7D%0A        return mergedBundle
 ;%0A  
@@ -1395,55 +1395,13 @@
 urn 
-arguments.containsKey(key) %7C%7C savedInstanceStat
+bundl
 e.co
@@ -1524,33 +1524,21 @@
     if (
-savedInstanceStat
+bundl
 e.contai
@@ -1583,133 +1583,14 @@
 t = 
-savedInstanceState.getParcelable(key);%0A        %7D else if (arguments.containsKey(key)) %7B%0A            restoreObject = arguments
+bundle
 .get
@@ -1813,147 +1813,14 @@
 if (
-savedInstanceState.containsKey(key)) %7B%0A            restoreObject = (T)savedInstanceState.getSerializable(key);%0A        %7D else if (arguments
+bundle
 .con
@@ -1867,25 +1867,22 @@
 ct = (T)
-arguments
+bundle
 .getSeri
@@ -2073,135 +2073,14 @@
 if (
-savedInstanceState.containsKey(key)) %7B%0A            restoreObject = savedInstanceState.getInt(key);%0A        %7D else if (arguments
+bundle
 .con
@@ -2124,25 +2124,22 @@
 bject = 
-arguments
+bundle
 .getInt(

add RestoreBundle constructor as single argument