-
Notifications
You must be signed in to change notification settings - Fork 7
/
CombiningExampleActivity.kt
150 lines (133 loc) · 5.01 KB
/
CombiningExampleActivity.kt
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.example.rxjavalecture.operator.combining
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.rxjavalecture.R
import com.example.rxjavalecture.databinding.ActivityCombiningExampleBinding
import com.example.rxjavalecture.util.TAG_TRANSFORM_OPERATOR
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.BiFunction
import java.util.concurrent.TimeUnit
class CombiningExampleActivity : AppCompatActivity() {
private lateinit var binding: ActivityCombiningExampleBinding
private val compositeDisposable = CompositeDisposable()
private var startedTime = 0L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_combining_example)
binding.btnMerge.setOnClickListener {
runMergeExample()
}
binding.btnConcat.setOnClickListener {
runConcatExample()
}
binding.btnConcatEager.setOnClickListener {
runConcatEagerExample()
}
binding.btnZip.setOnClickListener {
runZipExample()
}
binding.btnCombineLatest.setOnClickListener {
runCombineLatestExample()
}
}
private val integerInterval = Observable
.interval(1000, TimeUnit.MILLISECONDS)
.doOnNext { timeStampedLog("integer 데이터 $it 발행 되었음 !!") }
.takeWhile { it < 10 }
private val stringInterval = Observable
.interval(1500, TimeUnit.MILLISECONDS)
.takeWhile { it < 10 }
.doOnNext { timeStampedLog("String 데이터 $it 발행 되었음 !!") }
.map { "$it 번쨰 String 데이터" }
private fun runMergeExample() {
startedTime = System.currentTimeMillis()
compositeDisposable
.add(
Observable
.merge(
integerInterval,
stringInterval
)
.subscribe({
timeStampedLog(it)
}, { it.printStackTrace() })
)
}
private fun runConcatExample() {
startedTime = System.currentTimeMillis()
compositeDisposable
.add(
Observable
.concat(
integerInterval,
stringInterval
)
.subscribe({
timeStampedLog(it)
}, { it.printStackTrace() })
)
}
private fun runConcatEagerExample() {
startedTime = System.currentTimeMillis()
val observableList = listOf(integerInterval, stringInterval)
compositeDisposable
.add(
Observable
.concatEager(observableList)
.subscribe({
timeStampedLog(it)
}, { it.printStackTrace() })
)
}
private fun timeStampedLog(message: Any) {
Log.d(
TAG_TRANSFORM_OPERATOR,
"thread name = ${Thread.currentThread().name} 실행 후 흐른 시간 = ${System.currentTimeMillis() - startedTime} / message = $message"
)
}
private fun runZipExample() {
startedTime = System.currentTimeMillis()
compositeDisposable
.add(
Observable
.zip(
integerInterval,
stringInterval,
BiFunction { integerResult: Long, stringResult: String ->
return@BiFunction String.format(
"String 데이터는 %s 이고 / integer 데이터는 %d 번째 데이터가 발행 됨",
stringResult,
integerResult
)
}
)
.subscribe({
timeStampedLog(it)
}, { it.printStackTrace() })
)
}
private fun runCombineLatestExample() {
startedTime = System.currentTimeMillis()
compositeDisposable
.add(
Observable
.combineLatest(
integerInterval,
stringInterval,
BiFunction { integerResult: Long, stringResult: String ->
return@BiFunction String.format(
"String 데이터는 %s 이고 / integer 데이터는 %d 번째 데이터가 발행 됨",
stringResult,
integerResult
)
}
)
.subscribe({
timeStampedLog(it)
}, { it.printStackTrace() })
)
}
}