Skip to content

Commit 6de8410

Browse files
committed
sip
1 parent 08e9241 commit 6de8410

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

core-kotlin-modules/core-kotlin-collections-6/src/test/kotlin/com/baeldung/parallelOperationsCollections/ParallelOperationCollectionsUnitTest.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ class ParallelOperationCollectionsUnitTest {
4242
}
4343

4444
private val columnScheme = "%-25s %-45s %-40s"
45+
46+
private fun Person.setAdultStatus(){
47+
this.isAdult = this.age >= 18
4548

46-
private fun Person.printFormattedInfo() {
4749
println(
4850
columnScheme.format(
4951
SimpleDateFormat("yyyy-MM-dd:HH:mm:ss:SSS").format(Date.from(Instant.now())),
@@ -68,8 +70,7 @@ class ParallelOperationCollectionsUnitTest {
6870

6971
val filteredPeople = people.map { person ->
7072
async {
71-
person.isAdult = person.age >= 18
72-
person.printFormattedInfo()
73+
person.setAdultStatus()
7374
person
7475
}
7576
}.awaitAll().filter { it.age > 15 }.sortedBy { it.age }
@@ -88,8 +89,7 @@ class ParallelOperationCollectionsUnitTest {
8889
val filteredPeople = people.asFlow().flatMapMerge { person ->
8990
flow {
9091
emit(async {
91-
person.isAdult = person.age >= 18
92-
person.printFormattedInfo()
92+
person.setAdultStatus()
9393
person
9494
}.await())
9595
}
@@ -107,8 +107,7 @@ class ParallelOperationCollectionsUnitTest {
107107

108108
val observable = Observable.fromIterable(people).flatMap({
109109
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
110-
person.isAdult = person.age >= 18
111-
person.printFormattedInfo()
110+
person.setAdultStatus()
112111
}
113112
}, people.size) // Uses maxConcurrency for the number of elements
114113
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
@@ -125,8 +124,7 @@ class ParallelOperationCollectionsUnitTest {
125124

126125
val observable = people.toObservable().flatMap({
127126
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
128-
person.isAdult = person.age >= 18
129-
person.printFormattedInfo()
127+
person.setAdultStatus()
130128
}
131129
}, people.size) // Uses maxConcurrency for the number of elements
132130
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
@@ -143,8 +141,7 @@ class ParallelOperationCollectionsUnitTest {
143141

144142
val observable =
145143
people.toObservable().subscribeOn(Schedulers.io()).flatMap { Observable.just(it) }.doOnNext { person ->
146-
person.isAdult = person.age >= 18
147-
person.printFormattedInfo()
144+
person.setAdultStatus()
148145
}.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
149146

150147
startTime.printTotalTime()
@@ -158,8 +155,8 @@ class ParallelOperationCollectionsUnitTest {
158155
val startTime = Instant.now()
159156

160157
val filteredPeople = people.parallelStream().map { person ->
161-
person.isAdult = person.age >= 18
162-
person.printFormattedInfo()
158+
159+
person.setAdultStatus()
163160
person
164161
}.filter { it.age > 15 }.sorted { p1, p2 -> p1.age.compareTo(p2.age) }.collect(Collectors.toList())
165162

@@ -176,8 +173,7 @@ class ParallelOperationCollectionsUnitTest {
176173
val executor = Executors.newFixedThreadPool(people.size)
177174
val futures = people.map { person ->
178175
executor.submit(Callable {
179-
person.isAdult = person.age >= 18
180-
person.printFormattedInfo()
176+
person.setAdultStatus()
181177
person
182178
})
183179
}.map { it.get() }.filter { it.age > 15 }.sortedBy { it.age }

0 commit comments

Comments
 (0)