Skip to content

Instantly share code, notes, and snippets.

@f2prateek
Last active September 22, 2017 11:29
Show Gist options
  • Save f2prateek/8228793 to your computer and use it in GitHub Desktop.
Save f2prateek/8228793 to your computer and use it in GitHub Desktop.
package com.f2prateek.articuno.util;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.util.SparseArray;
import java.io.Serializable;
import java.util.ArrayList;
/**
* Fluent API for {@link android.os.Bundle}
* {@code Bundle bundle = new Bundler().put(....).put(....).get();}
*/
public class Bundler {
private final Bundle bundle;
// Construct a bundler
public Bundler() {
bundle = new Bundle();
}
/**
* Use the provided bundle to back this Bundler.
* The bundle that is passed will be modified directly.
*/
public Bundler(Bundle bundle) {
this.bundle = bundle;
}
public Bundler put(String key, boolean value) {
bundle.putBoolean(key, value);
return this;
}
public Bundler put(String key, boolean[] value) {
bundle.putBooleanArray(key, value);
return this;
}
public Bundler put(String key, IBinder value) {
// Uncommment this line if your minimum sdk version is API level 18
//bundle.putBinder(key, value);
return this;
}
public Bundler put(String key, int value) {
bundle.putInt(key, value);
return this;
}
public Bundler put(String key, int[] value) {
bundle.putIntArray(key, value);
return this;
}
public Bundler putIntegerArrayList(String key, ArrayList<Integer> value) {
bundle.putIntegerArrayList(key, value);
return this;
}
public Bundler put(String key, Bundle value) {
bundle.putBundle(key, value);
return this;
}
public Bundler put(String key, byte value) {
bundle.putByte(key, value);
return this;
}
public Bundler put(String key, byte[] value) {
bundle.putByteArray(key, value);
return this;
}
public Bundler put(String key, String value) {
bundle.putString(key, value);
return this;
}
public Bundler put(String key, String[] value) {
bundle.putStringArray(key, value);
return this;
}
public Bundler putStringArrayList(String key, ArrayList<String> value) {
bundle.putStringArrayList(key, value);
return this;
}
public Bundler put(String key, long value) {
bundle.putLong(key, value);
return this;
}
public Bundler put(String key, long[] value) {
bundle.putLongArray(key, value);
return this;
}
public Bundler put(String key, float value) {
bundle.putFloat(key, value);
return this;
}
public Bundler put(String key, float[] value) {
bundle.putFloatArray(key, value);
return this;
}
public Bundler put(String key, char value) {
bundle.putChar(key, value);
return this;
}
public Bundler put(String key, char[] value) {
bundle.putCharArray(key, value);
return this;
}
public Bundler put(String key, CharSequence value) {
bundle.putCharSequence(key, value);
return this;
}
public Bundler put(String key, CharSequence[] value) {
bundle.putCharSequenceArray(key, value);
return this;
}
public Bundler putCharSequenceArrayList(String key, ArrayList<CharSequence> value) {
bundle.putCharSequenceArrayList(key, value);
return this;
}
public Bundler put(String key, double value) {
bundle.putDouble(key, value);
return this;
}
public Bundler put(String key, double[] value) {
bundle.putDoubleArray(key, value);
return this;
}
public Bundler put(String key, Parcelable value) {
bundle.putParcelable(key, value);
return this;
}
public Bundler put(String key, Parcelable[] value) {
bundle.putParcelableArray(key, value);
return this;
}
public Bundler putParcelableArrayList(String key, ArrayList<? extends Parcelable> value) {
bundle.putParcelableArrayList(key, value);
return this;
}
public Bundler putSparseParcelableArray(String key, SparseArray<? extends Parcelable> value) {
bundle.putSparseParcelableArray(key, value);
return this;
}
public Bundler put(String key, short value) {
bundle.putShort(key, value);
return this;
}
public Bundler put(String key, short[] value) {
bundle.putShortArray(key, value);
return this;
}
public Bundler put(String key, Serializable value) {
bundle.putSerializable(key, value);
return this;
}
public Bundler putAll(Bundle map) {
bundle.putAll(map);
return this;
}
/**
* Get the underlying bundle.
*/
public Bundle get() {
return bundle;
}
/**
* Copy the underlying bundle.
*/
public Bundle copy() {
return new Bundle(bundle);
}
/**
* Initialize a Bundler that is copied form the given bundle. The bundle that is passed will not be modified.
*/
public static Bundler copyFrom(Bundle bundle) {
return new Bundler().putAll(bundle);
}
}
@Turbo87
Copy link

Turbo87 commented Jan 30, 2014

@f2prateek why don't you put it in a project and distribute it via maven?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment