ADD: Utility class Bundles for convenient Bundle building.

This commit is contained in:
Oasis 2016-10-06 17:06:36 +08:00
parent 1e13d9f5c6
commit 5ebc931b3a
3 changed files with 56 additions and 4 deletions

View File

@ -0,0 +1,38 @@
package com.oasisfeng.android.os;
import android.os.Bundle;
import android.os.Parcelable;
import com.oasisfeng.android.util.Consumer;
/**
* Bundle-related helpers
*
* Created by Oasis on 2016/10/4.
*/
public class Bundles {
@SafeVarargs public static Bundle build(final Consumer<Bundle>... actions) {
final Bundle bundle = new Bundle(actions.length);
for (final Consumer<Bundle> action : actions)
action.accept(bundle);
return bundle;
}
/** Make a Bundle for a single key/value pair. */
public static Bundle forPair(final String key, final String value) {
final Bundle b = new Bundle(1);
b.putString(key, value);
return b;
}
public static Bundle forPair(final String key, final Parcelable value) {
final Bundle b = new Bundle(1);
b.putParcelable(key, value);
return b;
}
public static Bundle of(final Parcelable value) { return forPair(null, value); }
private Bundles() {}
}

View File

@ -0,0 +1,18 @@
package com.oasisfeng.android.util;
/**
* Represents an operation that accepts a single input argument and returns no
* result. Unlike most other functional interfaces, {@code Consumer} is expected
* to operate via side-effects.
*
* @param <T> the type of the input to the operation
*/
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
}

View File

@ -23,10 +23,6 @@ import java.util.concurrent.ThreadPoolExecutor;
*/
public abstract class SafeAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
public interface Consumer<T> {
void accept(T object);
}
public static void execute(final Activity activity, final Consumer<Activity> runnable) {
final WeakReference<Activity> reference = new WeakReference<>(activity);
AsyncTask.execute(new SafeTask<>(new Supplier<Activity>() { @Override public Activity get() {