Skip to content

Instantly share code, notes, and snippets.

@ncruces
Forked from f2prateek/Bundler.java
Last active July 2, 2021 09:48
Show Gist options
  • Save ncruces/c7349f997eb33d26249d0709af570e99 to your computer and use it in GitHub Desktop.
Save ncruces/c7349f997eb33d26249d0709af570e99 to your computer and use it in GitHub Desktop.
Fluent API for working with bundles
package io.github.ncruces.utils;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.util.Size;
import android.util.SizeF;
import android.util.SparseArray;
import androidx.annotation.RequiresApi;
import java.io.Serializable;
import java.util.ArrayList;
/**
* Fluent API for {@link android.os.Bundle}
* {@code Bundle bundle = new Bundler().put(....).put(....).get();}
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public final 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;
}
@RequiresApi(18)
public Bundler put(String key, IBinder value) {
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;
}
@RequiresApi(8)
public Bundler put(String key, CharSequence[] value) {
bundle.putCharSequenceArray(key, value);
return this;
}
@RequiresApi(8)
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;
}
@RequiresApi(21)
public Bundler put(String key, Size value) {
bundle.putSize(key, value);
return this;
}
@RequiresApi(21)
public Bundler put(String key, SizeF value) {
bundle.putSizeF(key, value);
return this;
}
public Bundler putAll(Bundle map) {
bundle.putAll(map);
return this;
}
@RequiresApi(21)
public Bundler putAll(PersistableBundle 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(new Bundle(bundle));
}
/**
* Initialize a Bundler that is copied form the given PersistableBundle. The bundle that is passed will not be modified.
*/
@RequiresApi(21)
public static Bundler copyFrom(PersistableBundle bundle) {
return new Bundler(new Bundle(bundle));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment