-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRxJavaObservableFactoryMethods.java
79 lines (74 loc) · 2.31 KB
/
RxJavaObservableFactoryMethods.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package java_rxjava134;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
/**
* @version RxJava-1.3.4
*
* Observable factory methods
* 1. from
* 2. just
* 3. create
*
* @author ÌÆÁú
*
*/
public class RxJavaObservableFactoryMethods {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
/** creating the Observable instances from collections or arrays using <method>from</method>*/
// from a list
List<String> list = Arrays.asList("red", "blue", "green");
Observable<String> listObservable = Observable.from(list);
listObservable.subscribe(System.out::println);
// from a array
Observable<Integer> arrayObservable = Observable.from(new Integer[] {3, 5, 8});
arrayObservable.subscribe(System.out::println);
/** creating the Observable instances from object using <method>just</method> */
// The <method>just</method> method emits its parameter(s) as OnNext
// notifications, and after that, it emits an OnCompleted notification
// one letter:
Observable.just('S').subscribe(System.out::println);
// a sequence of letters:
Observable
.just('R', 'x', 'J', 'a', 'v', 'a')
.subscribe(
System.out::print,
System.out::println,
System.out::println
);
// get the length of a string
Observable
.just("Hello RxJava") // transform
.map(s->("Length of '"+ s + "': " + s.length())) // process
.subscribe(System.out::println); // action
/** The Observable.create method */
/** another method */
Commons.subscribePrint(
Observable.interval(500L, TimeUnit.MILLISECONDS),
"Interval Observable"
);
Commons.subscribePrint(
Observable.timer(0L, 1L, TimeUnit.SECONDS),
"Timed Interval Observable"
);
Commons.subscribePrint(
Observable.timer(1L, TimeUnit.SECONDS),
"Timer Observable"
);
Commons.subscribePrint(
Observable.error(new Exception("Test Error!")),
"Error Observable"
);
Commons.subscribePrint(Observable.empty(), "Empty Observable");
Commons.subscribePrint(Observable.never(), "Never Observable");
Commons.subscribePrint(Observable.range(1, 3), "Range Observable");
// sleep 2s
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}