Skip to content

Commit b0e0563

Browse files
Doha2012pivovarit
authored andcommitted
rxjava custom operator (eugenp#2352)
* minor logging fix * spring security sso * use basic auth * use form login * cleanup * cleanup * final cleanup * second client app for sso * spring boot bootstrap * add logic * cleanup * add simple controller * add thymeleaf and security * minor fix * minor fix * add more boot properties * fix live test * fix live test * minor fix * semaphores * fix configuration * kotlin collection * add more collection examples * minor upgrade * cucumber java8 * minor fix * rxjava custom operator
1 parent a3ed5a5 commit b0e0563

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baelding.rxjava.operator;
2+
3+
import rx.Observable.Operator;
4+
import rx.Subscriber;
5+
6+
public class cleanString implements Operator<String, String> {
7+
8+
public cleanString() {
9+
super();
10+
}
11+
12+
@Override
13+
public Subscriber<? super String> call(final Subscriber<? super String> subscriber) {
14+
return new Subscriber<String>(subscriber) {
15+
@Override
16+
public void onCompleted() {
17+
if (!subscriber.isUnsubscribed()) {
18+
subscriber.onCompleted();
19+
}
20+
}
21+
22+
@Override
23+
public void onError(Throwable t) {
24+
if (!subscriber.isUnsubscribed()) {
25+
subscriber.onError(t);
26+
}
27+
}
28+
29+
@Override
30+
public void onNext(String item) {
31+
if (!subscriber.isUnsubscribed()) {
32+
final String result = item.replaceAll("[^A-Za-z0-9]", "");
33+
subscriber.onNext(result);
34+
}
35+
}
36+
};
37+
}
38+
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baelding.rxjava.operator;
2+
3+
import rx.Observable;
4+
import rx.Observable.Transformer;
5+
import rx.functions.Func1;
6+
7+
public class toLength implements Transformer<String, Integer> {
8+
public toLength() {
9+
super();
10+
}
11+
12+
@Override
13+
public Observable<Integer> call(Observable<String> source) {
14+
15+
return source.map(new Func1<String, Integer>() {
16+
@Override
17+
public Integer call(String str) {
18+
return str.length();
19+
}
20+
});
21+
}
22+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.baeldung.rxjava;
2+
3+
import static org.hamcrest.Matchers.hasItems;
4+
import static org.hamcrest.Matchers.hasSize;
5+
import static org.hamcrest.Matchers.notNullValue;
6+
import static org.junit.Assert.assertThat;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
import org.junit.Test;
13+
14+
import rx.Observable;
15+
import rx.Observable.Operator;
16+
import rx.Observable.Transformer;
17+
import rx.Subscriber;
18+
import rx.functions.Func1;
19+
20+
import com.baelding.rxjava.operator.cleanString;
21+
import com.baelding.rxjava.operator.toLength;
22+
23+
public class RxJavaCustomOperatorUnitTest {
24+
25+
@Test
26+
public void whenUseCleanStringOperator_thenSuccess() {
27+
final List<String> list = Arrays.asList("john_1", "tom-3");
28+
final List<String> results = new ArrayList<String>();
29+
30+
final Observable<String> observable = Observable.from(list)
31+
.lift(new cleanString());
32+
33+
// when
34+
observable.subscribe(results::add);
35+
36+
// then
37+
assertThat(results, notNullValue());
38+
assertThat(results, hasSize(2));
39+
assertThat(results, hasItems("john1", "tom3"));
40+
}
41+
42+
@Test
43+
public void whenUseToLengthOperator_thenSuccess() {
44+
final List<String> list = Arrays.asList("john", "tom");
45+
final List<Integer> results = new ArrayList<Integer>();
46+
47+
final Observable<Integer> observable = Observable.from(list)
48+
.compose(new toLength());
49+
50+
// when
51+
observable.subscribe(results::add);
52+
53+
// then
54+
assertThat(results, notNullValue());
55+
assertThat(results, hasSize(2));
56+
assertThat(results, hasItems(4, 3));
57+
}
58+
59+
@Test
60+
public void whenUseFunctionOperator_thenSuccess() {
61+
final Operator<String, String> cleanStringFn = subscriber -> {
62+
return new Subscriber<String>(subscriber) {
63+
@Override
64+
public void onCompleted() {
65+
if (!subscriber.isUnsubscribed()) {
66+
subscriber.onCompleted();
67+
}
68+
}
69+
70+
@Override
71+
public void onError(Throwable t) {
72+
if (!subscriber.isUnsubscribed()) {
73+
subscriber.onError(t);
74+
}
75+
}
76+
77+
@Override
78+
public void onNext(String str) {
79+
if (!subscriber.isUnsubscribed()) {
80+
final String result = str.replaceAll("[^A-Za-z0-9]", "");
81+
subscriber.onNext(result);
82+
}
83+
}
84+
};
85+
};
86+
87+
final List<String> results = new ArrayList<String>();
88+
Observable.from(Arrays.asList("ap_p-l@e", "or-an?ge"))
89+
.lift(cleanStringFn)
90+
.subscribe(results::add);
91+
92+
assertThat(results, notNullValue());
93+
assertThat(results, hasSize(2));
94+
assertThat(results, hasItems("apple", "orange"));
95+
}
96+
97+
@Test
98+
public void whenUseFunctionTransformer_thenSuccess() {
99+
final Transformer<String, Integer> toLengthFn = source -> {
100+
return source.map(new Func1<String, Integer>() {
101+
@Override
102+
public Integer call(String str) {
103+
return str.length();
104+
}
105+
});
106+
};
107+
108+
final List<Integer> results = new ArrayList<Integer>();
109+
Observable.from(Arrays.asList("apple", "orange"))
110+
.compose(toLengthFn)
111+
.subscribe(results::add);
112+
113+
assertThat(results, notNullValue());
114+
assertThat(results, hasSize(2));
115+
assertThat(results, hasItems(5, 6));
116+
}
117+
}

0 commit comments

Comments
 (0)