Android Open Source - android-tools Bundle Builder






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.comfreeze.lib;
//from   w  ww  .ja  v  a2s  .c  o  m
import android.os.Bundle;
import android.os.Parcelable;
import android.util.SparseArray;

import org.json.custom.JSONArray;
import org.json.custom.JSONException;
import org.json.custom.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;

public class BundleBuilder {
    private Bundle data = new Bundle();

    public static BundleBuilder instance() {
        return new BundleBuilder();
    }

    public static BundleBuilder instance(Bundle data) {
        BundleBuilder builder = new BundleBuilder();
        if (null != data)
            builder.data = new Bundle(data);
        return builder;
    }

    public static BundleBuilder instance(ClassLoader loader) {
        BundleBuilder builder = new BundleBuilder();
        builder.data = new Bundle(loader);
        return builder;
    }

    public static BundleBuilder instance(int capacity) {
        BundleBuilder builder = new BundleBuilder();
        builder.data = new Bundle(capacity);
        return builder;
    }

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

    public BundleBuilder setArg(String key, Object value) {
        if (value instanceof Boolean)
            setArg(key, (Boolean) value);
        else if (value instanceof Boolean[])
            setArg(key, (boolean[]) value);
        else if (value instanceof Bundle)
            setArg(key, (Bundle) value);
        else if (value instanceof Byte)
            setArg(key, (Byte) value);
        else if (value instanceof Byte[])
            setArg(key, (byte[]) value);
        else if (value instanceof CharSequence)
            setArg(key, (CharSequence) value);
        else if (value instanceof CharSequence[])
            setArg(key, (CharSequence[]) value);
        else if (value instanceof Double)
            setArg(key, (Double) value);
        else if (value instanceof Double[])
            setArg(key, (double[]) value);
        else if (value instanceof Float)
            setArg(key, (Float) value);
        else if (value instanceof Float[])
            setArg(key, (float[]) value);
        else if (value instanceof Integer)
            setArg(key, (Integer) value);
        else if (value instanceof Integer[])
            setArg(key, (int[]) value);
        else if (value instanceof Long)
            setArg(key, (Long) value);
        else if (value instanceof Long[])
            setArg(key, (long[]) value);
        else if (value instanceof Parcelable)
            setArg(key, (Parcelable) value);
        else if (value instanceof Parcelable[])
            setArg(key, (Parcelable[]) value);
        else if (value instanceof Serializable)
            setArg(key, (Serializable) value);
        else if (value instanceof Short)
            setArg(key, (Short) value);
        else if (value instanceof Short[])
            setArg(key, (short[]) value);
        else if (value instanceof SparseArray)
            setArg(key, (SparseArray) value);
        else if (value instanceof String)
            setArg(key, (String) value);
        else if (value instanceof String[])
            setArg(key, (String[]) value);
        else if (value instanceof ArrayList)
            setArg(key, (ArrayList) value);
        return this;
    }

    public BundleBuilder setArg(String key, boolean value) {
        data.putBoolean(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, boolean[] value) {
        data.putBooleanArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, Bundle value) {
        data.putBundle(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, byte value) {
        data.putByte(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, byte[] value) {
        data.putByteArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, char value) {
        data.putChar(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, char[] value) {
        data.putCharArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, CharSequence value) {
        data.putCharSequence(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, CharSequence[] value) {
        data.putCharSequenceArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, double value) {
        data.putDouble(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, double[] value) {
        data.putDoubleArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, float value) {
        data.putFloat(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, float[] value) {
        data.putFloatArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, int value) {
        data.putInt(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, int[] value) {
        data.putIntArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, long value) {
        data.putLong(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, long[] value) {
        data.putLongArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, Parcelable value) {
        data.putParcelable(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, Parcelable[] value) {
        data.putParcelableArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, Serializable value) {
        data.putSerializable(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, short value) {
        data.putShort(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, short[] value) {
        data.putShortArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, SparseArray<Parcelable> value) {
        data.putSparseParcelableArray(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, String value) {
        data.putString(key, value);
        return this;
    }

    public BundleBuilder setArg(String key, String[] value) {
        data.putStringArray(key, value);
        return this;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public BundleBuilder setArg(String key, ArrayList value) {
        Object rawValue = value.get(0);
        if (rawValue instanceof CharSequence)
            data.putCharSequenceArrayList(key, value);
        else if (rawValue instanceof Integer)
            data.putIntegerArrayList(key, value);
        else if (rawValue instanceof Parcelable)
            data.putParcelableArrayList(key, value);
        else if (rawValue instanceof String)
            data.putStringArrayList(key, value);
        return this;
    }

    public Bundle build() {
        return this.data;
    }

    public org.json.JSONObject buildStandardJson() {
        return buildStandardJson(null);
    }

    public org.json.JSONObject buildStandardJson(Bundle source) {
        if (null == source)
            source = this.data;
        if (null == source)
            return null;
        org.json.JSONObject output = new org.json.JSONObject();
        for (String key : source.keySet()) {
            try {
                Object param = source.get(key);
                // String param = parameters.getString(key);
                if (param instanceof String) {
                    final String paramString = (String) param;
                    // if (null != param) {
                    if (paramString.startsWith("[")) {
                        output.put(key, new org.json.JSONArray(paramString));
                    } else if (paramString.startsWith("{")) {
                        output.put(key, new org.json.JSONObject(paramString));
                    } else {
                        output.put(key, paramString);
                    }
                } else if (param instanceof String[]) {
                    JSONArray outArray = new JSONArray();
                    for (String item : (String[]) param)
                        outArray.put(item);
                    output.put(key, outArray);
                } else if (param instanceof ArrayList<?>) {
                    if (param instanceof String) {
                        output.put(key, param);
                    } else {
                        try {
                            @SuppressWarnings("unchecked")
                            ArrayList<Bundle> outArray = (ArrayList<Bundle>) param;
                            if (outArray.get(0) instanceof Bundle) {
                                JSONArray dataArray = new JSONArray();
                                for (int i = 0; i < outArray.size(); i++) {
                                    dataArray.put(buildJson(outArray.get(i)));
                                }
                                output.put(key, dataArray);
                            } else {
                                output.put(key, param);
                            }
                        } catch (Exception e) {
                            output.put(key, param);
                        }
                    }
                } else if (param instanceof Bundle) {
                    output.put(key, buildJson((Bundle) param));
                } else {
                    output.put(key, param);
                }
            } catch (org.json.JSONException e) {
                e.printStackTrace();
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        return output;
    }

    public JSONObject buildJson() {
        return buildJson(null);
    }

    public JSONObject buildJson(Bundle source) {
        if (null == source)
            source = this.data;
        if (null == source)
            return null;
        JSONObject output = new JSONObject();
        for (String key : source.keySet()) {
            try {
                Object param = source.get(key);
                // String param = parameters.getString(key);
                if (param instanceof String) {
                    final String paramString = (String) param;
                    // if (null != param) {
                    if (paramString.startsWith("[")) {
                        output.put(key, new JSONArray(paramString));
                    } else if (paramString.startsWith("{")) {
                        output.put(key, new JSONObject(paramString));
                    } else {
                        output.put(key, paramString);
                    }
                } else if (param instanceof String[]) {
                    JSONArray outArray = new JSONArray();
                    for (String item : (String[]) param)
                        outArray.put(item);
                    output.put(key, outArray);
                } else if (param instanceof ArrayList<?>) {
                    if (param instanceof String) {
                        output.put(key, param);
                    } else {
                        try {
                            @SuppressWarnings("unchecked")
                            ArrayList<Bundle> outArray = (ArrayList<Bundle>) param;
                            if (outArray.get(0) instanceof Bundle) {
                                JSONArray dataArray = new JSONArray();
                                for (int i = 0; i < outArray.size(); i++) {
                                    dataArray.put(buildJson(outArray.get(i)));
                                }
                                output.put(key, dataArray);
                            } else {
                                output.put(key, param);
                            }
                        } catch (Exception e) {
                            output.put(key, param);
                        }
                    }
                } else if (param instanceof Bundle) {
                    output.put(key, buildJson((Bundle) param));
                } else {
                    output.put(key, param);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        return output;
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java