From 7faedcc5ceecc78a015b9cb33d042a5c79f0558a Mon Sep 17 00:00:00 2001 From: Mark Wei Date: Wed, 7 Dec 2016 18:09:10 -0800 Subject: [PATCH] Implement map() for android. Summary: Fixes https://github.com/material-motion/streams-android/issues/2 Reviewers: featherless, O2 Material Motion, O6 Material Motion Android platform reviewers Reviewed By: featherless, O2 Material Motion, O6 Material Motion Android platform reviewers Subscribers: featherless Tags: #material_motion Differential Revision: http://codereview.cc/D2157 --- .../motion/streams/MotionObservable.java | 43 +++++++++++++++---- .../motion/streams/sample/MainActivity.java | 11 +++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java b/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java index 633383b..6b64579 100644 --- a/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java +++ b/library/src/main/java/com/google/android/material/motion/streams/MotionObservable.java @@ -74,36 +74,49 @@ public interface MotionObserver extends Observer { void next(T value); /** - * A method to handle new state values from upstream. + * A method to handle new incoming state values. */ void state(@MotionState int state); } /** * An operation is able to transform incoming values before choosing whether or not to pass them - * downstream. + * to the observer. * * @param The incoming value type. - * @param The downstream value type. + * @param The observer value type. */ public interface Operation { /** - * Modifies the given value before passing it downstream, or blocks the value. + * Transforms the incoming value before passing it to the observer, or blocks the value. * - * @param observer Downstream. - * @param value The value from upstream. + * @param value The incoming value. */ void next(MotionObserver observer, T value); } + /** + * A transformation transforms incoming values before they are passed downstream. + * + * @param The incoming value type. + * @param The downstream value type. + */ + public interface Transformation { + + /** + * Transforms the given value. + */ + U transform(T value); + } + /** * A predicate evaluates whether to pass a value downstream. */ public interface Predicate { /** - * Evaluates whether to pass the value downstream. + * Evaluates whether to pass the value. */ boolean evaluate(T value); } @@ -147,6 +160,21 @@ public void unsubscribe() { }); } + /** + * Transform the items emitted by an Observable by applying a function to each item. + * + * @see The + * filter() specification + */ + public MotionObservable map(final Transformation transformation) { + return operator(new Operation() { + @Override + public void next(MotionObserver observer, T value) { + observer.next(transformation.transform(value)); + } + }); + } + /** * Only emit those values from an Observable that satisfy a predicate. * @@ -155,7 +183,6 @@ public void unsubscribe() { */ public MotionObservable filter(final Predicate predicate) { return operator(new Operation() { - @Override public void next(MotionObserver observer, T value) { if (predicate.evaluate(value)) { diff --git a/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java b/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java index eaa170d..6bc7080 100644 --- a/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java +++ b/sample/src/main/java/com/google/android/material/motion/streams/sample/MainActivity.java @@ -33,7 +33,6 @@ import com.google.android.material.motion.streams.MotionObservable; import com.google.android.material.motion.streams.MotionObservable.MotionObserver; import com.google.android.material.motion.streams.MotionObservable.MotionState; -import com.google.android.material.motion.streams.MotionObservable.Operation; import static com.google.android.material.motion.streams.MotionObservable.ACTIVE; import static com.google.android.material.motion.streams.MotionObservable.AT_REST; @@ -85,14 +84,14 @@ public boolean evaluate(String value) { return value != "skip"; } }) - .operator(new Operation() { + .map(new MotionObservable.Transformation() { @Override - public void next(MotionObserver observer, String value) { - CharSequence charSequence = italicizeAndCapitalize(value); - observer.next(charSequence); + public CharSequence transform(String value) { + return italicizeAndCapitalize(value); } - }).subscribe(new MotionObserver() { + }) + .subscribe(new MotionObserver() { @Override public void next(CharSequence value) {