commit
5ba675be35
|
@ -3,9 +3,9 @@ package com.baelding.rxjava.operator;
|
||||||
import rx.Observable.Operator;
|
import rx.Observable.Operator;
|
||||||
import rx.Subscriber;
|
import rx.Subscriber;
|
||||||
|
|
||||||
public class cleanString implements Operator<String, String> {
|
public class CleanString implements Operator<String, String> {
|
||||||
|
|
||||||
public cleanString() {
|
public CleanString() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baelding.rxjava.operator;
|
||||||
|
|
||||||
|
import rx.Observable;
|
||||||
|
import rx.Observable.Transformer;
|
||||||
|
import rx.functions.Func1;
|
||||||
|
|
||||||
|
public class ToLength implements Transformer<String, Integer> {
|
||||||
|
public ToLength() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Observable<Integer> call(Observable<String> source) {
|
||||||
|
return source.map(String::length);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,22 +0,0 @@
|
||||||
package com.baelding.rxjava.operator;
|
|
||||||
|
|
||||||
import rx.Observable;
|
|
||||||
import rx.Observable.Transformer;
|
|
||||||
import rx.functions.Func1;
|
|
||||||
|
|
||||||
public class toLength implements Transformer<String, Integer> {
|
|
||||||
public toLength() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Observable<Integer> call(Observable<String> source) {
|
|
||||||
|
|
||||||
return source.map(new Func1<String, Integer>() {
|
|
||||||
@Override
|
|
||||||
public Integer call(String str) {
|
|
||||||
return str.length();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -15,10 +15,9 @@ import rx.Observable;
|
||||||
import rx.Observable.Operator;
|
import rx.Observable.Operator;
|
||||||
import rx.Observable.Transformer;
|
import rx.Observable.Transformer;
|
||||||
import rx.Subscriber;
|
import rx.Subscriber;
|
||||||
import rx.functions.Func1;
|
|
||||||
|
|
||||||
import com.baelding.rxjava.operator.cleanString;
|
import com.baelding.rxjava.operator.CleanString;
|
||||||
import com.baelding.rxjava.operator.toLength;
|
import com.baelding.rxjava.operator.ToLength;
|
||||||
|
|
||||||
public class RxJavaCustomOperatorUnitTest {
|
public class RxJavaCustomOperatorUnitTest {
|
||||||
|
|
||||||
|
@ -28,7 +27,7 @@ public class RxJavaCustomOperatorUnitTest {
|
||||||
final List<String> results = new ArrayList<String>();
|
final List<String> results = new ArrayList<String>();
|
||||||
|
|
||||||
final Observable<String> observable = Observable.from(list)
|
final Observable<String> observable = Observable.from(list)
|
||||||
.lift(new cleanString());
|
.lift(new CleanString());
|
||||||
|
|
||||||
// when
|
// when
|
||||||
observable.subscribe(results::add);
|
observable.subscribe(results::add);
|
||||||
|
@ -45,7 +44,7 @@ public class RxJavaCustomOperatorUnitTest {
|
||||||
final List<Integer> results = new ArrayList<Integer>();
|
final List<Integer> results = new ArrayList<Integer>();
|
||||||
|
|
||||||
final Observable<Integer> observable = Observable.from(list)
|
final Observable<Integer> observable = Observable.from(list)
|
||||||
.compose(new toLength());
|
.compose(new ToLength());
|
||||||
|
|
||||||
// when
|
// when
|
||||||
observable.subscribe(results::add);
|
observable.subscribe(results::add);
|
||||||
|
@ -96,14 +95,7 @@ public class RxJavaCustomOperatorUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseFunctionTransformer_thenSuccess() {
|
public void whenUseFunctionTransformer_thenSuccess() {
|
||||||
final Transformer<String, Integer> toLengthFn = source -> {
|
final Transformer<String, Integer> toLengthFn = source -> source.map(String::length);
|
||||||
return source.map(new Func1<String, Integer>() {
|
|
||||||
@Override
|
|
||||||
public Integer call(String str) {
|
|
||||||
return str.length();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
final List<Integer> results = new ArrayList<Integer>();
|
final List<Integer> results = new ArrayList<Integer>();
|
||||||
Observable.from(Arrays.asList("apple", "orange"))
|
Observable.from(Arrays.asList("apple", "orange"))
|
||||||
|
|
Loading…
Reference in New Issue