-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathCreatingObservablesWithFrom.java
More file actions
70 lines (57 loc) · 1.71 KB
/
Copy pathCreatingObservablesWithFrom.java
File metadata and controls
70 lines (57 loc) · 1.71 KB
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
package com.packtpub.reactive.chapter03;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import rx.Observable;
import rx.functions.Action1;
import com.packtpub.reactive.common.Program;
/**
* A set of examples of using Observable.from.
*
* @author meddle
*/
public class CreatingObservablesWithFrom implements Program {
public static Action1<?> N = (v) -> {
};
public static Action1<Throwable> NE = (e) -> {
};
@Override
public String name() {
return "Creating Observables with Observable.from";
}
@Override
public int chapter() {
return 3;
}
@Override
public void run() {
// from(list)
List<String> list = Arrays.asList("blue", "red", "green", "yellow",
"orange", "cyan", "purple");
Observable<String> listObservable = Observable.from(list);
listObservable.subscribe(System.out::println);
listObservable.subscribe(color -> System.out.print(color + "|"), NE,
System.out::println);
listObservable.subscribe(color -> System.out.print(color + "/"), NE,
System.out::println);
// from(Iterable)
Path resources = Paths.get("src", "main", "resources");
try (DirectoryStream<Path> dStream = Files.newDirectoryStream(resources)) {
Observable<Path> dirObservable = Observable.from(dStream);
dirObservable.subscribe(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
// from(array)
Observable<Integer> arrayObservable = Observable.from(new Integer[] {
3, 5, 8 });
arrayObservable.subscribe(System.out::println);
}
public static void main(String[] args) {
new CreatingObservablesWithFrom().run();
}
}