package com.stardust.util;

import android.content.Intent;
import android.util.SparseArray;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Stardust on 2017/7/11.
 */

public class IntentExtras implements Serializable {

    private static AtomicInteger mMaxId = new AtomicInteger(-1);
    private static final String EXTRA_ID = "com.stardust.util.IntentExtras.id";
    private static SparseArray<Map<String, Object>> extraStore = new SparseArray<>();


    public static IntentExtras newExtras() {
        return new IntentExtras();
    }

    public static IntentExtras fromIntent(Intent intent) {
        int id = intent.getIntExtra(EXTRA_ID, -1);
        if (id < 0) {
            throw new IllegalArgumentException();
        }
        return new IntentExtras(id);
    }

    private Map<String, Object> mMap;

    private IntentExtras() {
        mMap = new HashMap<>();
        int id = mMaxId.incrementAndGet();
        extraStore.put(id, mMap);
    }


    private IntentExtras(int id) {
        mMap = extraStore.get(id);
        if (mMap == null) {
            mMap = new HashMap<>();
        }
        extraStore.remove(id);
    }


    @SuppressWarnings("unchecked")
    public <T> T get(String key) {
        return (T) mMap.get(key);
    }

    public IntentExtras put(String key, Object value) {
        mMap.put(key, value);
        return this;
    }

    public Intent putInIntent(Intent intent) {
        intent.putExtra(EXTRA_ID, mMaxId);
        return intent;
    }
}

@@ -906,16 +906,37 @@
 t%3E mMap;
+%0A    private int mId;
 %0A%0A    pr
@@ -994,29 +994,26 @@
 ();%0A        
-int i
+mI
 d = mMaxId.i
@@ -1053,17 +1053,18 @@
 ore.put(
-i
+mI
 d, mMap)
@@ -1104,24 +1104,42 @@
 s(int id) %7B%0A
+        mId = id;%0A
         mMap
@@ -1581,19 +1581,16 @@
 RA_ID, m
-Max
 Id);%0A   

fix hyb1996-guest/auto.js3-issues#5