forked from politrons/reactive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObservableWindow.java
50 lines (40 loc) · 1.42 KB
/
ObservableWindow.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
package rx.observables.transforming;
import org.junit.Test;
import rx.Observable;
import java.util.ArrayList;
import java.util.List;
/**
* @author Pablo Perez
* <p>
* Window is similar to buffer, but instead emitt the list of items buffered, it will return constantClass new observable with those items.
*/
public class ObservableWindow {
/**
* In this example since we set the window in 3 items, it will create two observables.
* First one will emit 0,1,2 item, and second will emit 3,4
*/
@Test
public void windowCountObservable() {
Integer[] numbers = {0, 1, 2, 3, 4};
Observable.from(numbers)
.window(3)
.flatMap(o -> {
System.out.println("New Observable");
return o;
})
.subscribe(number -> System.out.println("Number:" + number));
}
@Test
public void stringBuffer() {
List<String> elements = new ArrayList<>();
Integer[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Observable.from(numbers)
.window(4)
.flatMap(ns -> ns
.map(number -> "uniqueKey=" + number + "&")
.reduce("", String::concat))
.map(query -> query.substring(0, query.length() - 1))
.subscribe(elements::add);
System.out.println(elements);
}
}