forked from PacktPublishing/Learning-RxJava-Second-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh9_04a.java
More file actions
36 lines (28 loc) · 1.12 KB
/
Copy pathCh9_04a.java
File metadata and controls
36 lines (28 loc) · 1.12 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
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableTransformer;
import java.util.concurrent.atomic.AtomicInteger;
public class Ch9_04a {
public static void main(String[] args) {
Observable<IndexedValue<String>> indexedStrings =
Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon")
.compose(withIndex());
indexedStrings.subscribe(v -> System.out.println("Subscriber 1: " + v));
indexedStrings.subscribe(v -> System.out.println("Subscriber 2: " + v));
}
private static <T> ObservableTransformer<T, IndexedValue<T>> withIndex() {
final AtomicInteger indexer = new AtomicInteger(-1);
return upstream -> upstream.map(v -> new IndexedValue<T>(indexer.incrementAndGet(), v));
}
private static final class IndexedValue<T> {
final int index;
final T value;
IndexedValue(int index, T value) {
this.index = index;
this.value = value;
}
@Override
public String toString() {
return index + " - " + value;
}
}
}