11package itrx .chapter3 .combining ;
22
3+ import java .io .IOException ;
34import java .util .Arrays ;
45import java .util .concurrent .TimeUnit ;
56
1213
1314public class ZipTest {
1415
16+ public static void main (String [] args ) throws IOException {
17+ new ZipTest ().exampleZipWithIterable ();
18+ System .in .read ();
19+ }
20+
1521 public void example () {
1622 Observable .zip (
1723 Observable .interval (100 , TimeUnit .MILLISECONDS )
@@ -46,6 +52,36 @@ public void example() {
4652 // 5 - 5
4753 }
4854
55+ public void exampleZipWith () {
56+ Observable .interval (100 , TimeUnit .MILLISECONDS )
57+ .zipWith (
58+ Observable .interval (150 , TimeUnit .MILLISECONDS ),
59+ (i1 ,i2 ) -> i1 + " - " + i2 )
60+ .take (6 )
61+ .subscribe (System .out ::println );
62+
63+ // 0 - 0
64+ // 1 - 1
65+ // 2 - 2
66+ // 3 - 3
67+ // 4 - 4
68+ // 5 - 5
69+ }
70+
71+ public void exampleZipWithIterable () {
72+ Observable .range (0 , 5 )
73+ .zipWith (
74+ Arrays .asList (0 ,2 ,4 ,6 ,8 ),
75+ (i1 ,i2 ) -> i1 + " - " + i2 )
76+ .subscribe (System .out ::println );
77+
78+ // 0 - 0
79+ // 1 - 2
80+ // 2 - 4
81+ // 3 - 6
82+ // 4 - 8
83+ }
84+
4985
5086 //
5187 // Test
@@ -64,7 +100,27 @@ public void test() {
64100 .subscribe (tester );
65101
66102 scheduler .advanceTimeBy (600 , TimeUnit .MILLISECONDS );
103+ tester .assertReceivedOnNext (Arrays .asList (
104+ "0 - 0" ,
105+ "1 - 1" ,
106+ "2 - 2" ,
107+ "3 - 3"
108+ ));
109+ tester .assertNoErrors ();
110+ }
111+
112+ @ Test
113+ public void testZipWith () {
114+ TestSubscriber <String > tester = new TestSubscriber <>();
115+ TestScheduler scheduler = Schedulers .test ();
116+
117+ Observable .interval (100 , TimeUnit .MILLISECONDS , scheduler )
118+ .zipWith (
119+ Observable .interval (150 , TimeUnit .MILLISECONDS , scheduler ),
120+ (i1 ,i2 ) -> i1 + " - " + i2 )
121+ .subscribe (tester );
67122
123+ scheduler .advanceTimeBy (600 , TimeUnit .MILLISECONDS );
68124 tester .assertReceivedOnNext (Arrays .asList (
69125 "0 - 0" ,
70126 "1 - 1" ,
@@ -73,5 +129,26 @@ public void test() {
73129 ));
74130 tester .assertNoErrors ();
75131 }
132+
133+ @ Test
134+ public void testZipWithIterable () {
135+ TestSubscriber <String > tester = new TestSubscriber <>();
136+
137+ Observable .range (0 , 5 )
138+ .zipWith (
139+ Arrays .asList (0 ,2 ,4 ,6 ,8 ),
140+ (i1 ,i2 ) -> i1 + " - " + i2 )
141+ .subscribe (tester );
142+
143+ tester .assertReceivedOnNext (Arrays .asList (
144+ "0 - 0" ,
145+ "1 - 2" ,
146+ "2 - 4" ,
147+ "3 - 6" ,
148+ "4 - 8"
149+ ));
150+ tester .assertTerminalEvent ();
151+ tester .assertNoErrors ();
152+ }
76153
77154}
0 commit comments