Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename collectItems() and groupItems() into collect() and group() #413

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testDeferredWithMultipleItems() {
} catch (Throwable t) {
emitter.fail(t);
}
}).start()).collectItems().asList();
}).start()).collect().asList();

List<Integer> result = latch.await().indefinitely();
assertThat(result).hasSize(3).allSatisfy(i -> assertThat(i).isEqualTo(2));
Expand All @@ -151,7 +151,7 @@ public void testGenerator() {
return r;
});

int result = multi.collectItems().first().await().indefinitely();
int result = multi.collect().first().await().indefinitely();
assertThat(result).isEqualTo(2);
}

Expand Down
4 changes: 2 additions & 2 deletions documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public class RepetitionsTest {
// tag::distinct[]
List<Integer> list = multi
.transform().byDroppingDuplicates()
.collectItems().asList()
.collect().asList()
.await().indefinitely();
// end::distinct[]

// tag::repetition[]
List<Integer> list2 = multi
.transform().byDroppingRepetitions()
.collectItems().asList()
.collect().asList()
.await().indefinitely();
// end::repetition[]
assertThat(list).containsExactly(1, 2, 3, 4, 5, 6);
Expand Down
Binary file modified documentation/src/main/jekyll/assets/images/collect-as-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion documentation/src/main/jekyll/guides/collecting-items.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This is also called a _reduction_ in functional programming.

The `Uni` fires its item when the `Multi` completes.
Mutiny provides multiple operators to deal with that scenario.
They are available from the `collect()` group.
For example, you can store the items in a list, emit the list on completion, or use a Java `Collector` to customize the aggregation.

IMPORTANT: Don't collect items from infinite streams or you will likely end with an out-of-memory failure!
Expand Down Expand Up @@ -78,7 +79,7 @@ include::{include_dir}/CollectingItemsTest.java[tag=collector]

== Getting the first and last items

While they are not strictly speaking collecting items, the `collectItems().first()` and `collectItems().last()` allows retrieving the first and last item from a `Multi`:
While they are not strictly speaking collecting items, the `collect().first()` and `collect().last()` allows retrieving the first and last item from a `Multi`:

[source, java, indent=0]
----
Expand Down
4 changes: 2 additions & 2 deletions documentation/src/test/java/guides/CompletionStageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public void test() {
.onItem().transform(String::toUpperCase);
// end::create-multi[]

assertThat(multi1.collectItems().asList().await().indefinitely()).containsExactly("HELLO");
assertThat(multi2.collectItems().asList().await().indefinitely()).containsExactly("HELLO");
assertThat(multi1.collect().asList().await().indefinitely()).containsExactly("HELLO");
assertThat(multi2.collect().asList().await().indefinitely()).containsExactly("HELLO");

}
}
12 changes: 6 additions & 6 deletions documentation/src/test/java/guides/CreatingMultiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void creation() {
Multi<Integer> multiFromItems = Multi.createFrom().items(1, 2, 3, 4);
Multi<Integer> multiFromIterable = Multi.createFrom().iterable(Arrays.asList(1, 2, 3, 4, 5));
// end::simple[]
assertThat(multiFromItems.collectItems().asList().await().indefinitely()).containsExactly(1, 2, 3, 4);
assertThat(multiFromIterable.collectItems().asList().await().indefinitely()).containsExactly(1, 2, 3, 4, 5);
assertThat(multiFromItems.collect().asList().await().indefinitely()).containsExactly(1, 2, 3, 4);
assertThat(multiFromIterable.collect().asList().await().indefinitely()).containsExactly(1, 2, 3, 4, 5);
}

{
Expand All @@ -64,9 +64,9 @@ public void creation() {
Multi<Integer> multi = Multi.createFrom().items(() ->
IntStream.range(counter.getAndIncrement(), counter.get() * 2).boxed());
// end::supplier[]
assertThat(multi.collectItems().asList().await().indefinitely()).containsExactly(0, 1);
assertThat(multi.collectItems().asList().await().indefinitely()).containsExactly(1, 2, 3);
assertThat(multi.collectItems().asList().await().indefinitely()).containsExactly(2, 3, 4, 5);
assertThat(multi.collect().asList().await().indefinitely()).containsExactly(0, 1);
assertThat(multi.collect().asList().await().indefinitely()).containsExactly(1, 2, 3);
assertThat(multi.collect().asList().await().indefinitely()).containsExactly(2, 3, 4, 5);
}

{
Expand Down Expand Up @@ -101,7 +101,7 @@ public void creation() {
em.complete();
});
// end::emitter[]
assertThat(multi.collectItems().asList().await().indefinitely()).containsExactly(1, 2, 3);
assertThat(multi.collect().asList().await().indefinitely()).containsExactly(1, 2, 3);
}

{
Expand Down
6 changes: 3 additions & 3 deletions documentation/src/test/java/guides/HandlingFailuresTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testInvoke(SystemOut out) {
.onFailure().invoke(failure -> log(failure));
// end::invoke[]
assertThatThrownBy(() -> u.await().indefinitely()).hasMessageContaining("boom");
assertThatThrownBy(() -> m.collectItems().asList()
assertThatThrownBy(() -> m.collect().asList()
.await().indefinitely()).hasMessageContaining("boom");
assertThat(out.get()).contains("boom");
}
Expand Down Expand Up @@ -64,7 +64,7 @@ public void testCompletionOnFailure() {
Multi<String> m = multi
.onFailure().recoverWithCompletion();
// end::recover-completion[]
assertThat(m.collectItems().asList().await().indefinitely()).isEmpty();
assertThat(m.collect().asList().await().indefinitely()).isEmpty();
}

@Test
Expand All @@ -79,7 +79,7 @@ public void testSwitch() {
.onFailure().recoverWithMulti(f -> getFallbackMulti(f));
// end::recover-switch[]
assertThat(u.await().indefinitely()).isEqualTo("hello");
assertThat(m.collectItems().asList().await().indefinitely()).containsExactly("hey");
assertThat(m.collect().asList().await().indefinitely()).containsExactly("hey");
}

private Multi<? extends String> getFallbackMulti(Throwable f) {
Expand Down
2 changes: 1 addition & 1 deletion documentation/src/test/java/guides/RetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void testRetryAtMost() {
.onFailure().retry().atMost(3);
// end::retry-at-most[]
assertThatThrownBy(() -> u.await().indefinitely()).hasMessageContaining("boom");
assertThatThrownBy(() -> m.collectItems().asList()
assertThatThrownBy(() -> m.collect().asList()
.await().indefinitely()).hasMessageContaining("boom");
}

Expand Down
16 changes: 8 additions & 8 deletions documentation/src/test/java/guides/RxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public void rx() {

List<Integer> list = multi
.map(i -> i + 1)
.collectItems().asList()
.collect().asList()
.await().indefinitely();

List<Integer> list2 = multi
.flatMap(i -> Multi.createFrom().items(i, i))
.collectItems().asList()
.collect().asList()
.await().indefinitely();

List<Integer> list3 = multi
.concatMap(i -> Multi.createFrom().items(i, i))
.collectItems().asList()
.collect().asList()
.await().indefinitely();

// end::rx[]
Expand Down Expand Up @@ -68,31 +68,31 @@ public void mutiny() {

List<Integer> list = multi
.onItem().transform(i -> i + 1)
.collectItems().asList()
.collect().asList()
.await().indefinitely();

List<Integer> list2 = multi
.onItem().transformToMultiAndMerge(i -> Multi.createFrom().items(i, i))
.collectItems().asList()
.collect().asList()
.await().indefinitely();

// Equivalent to transformToMultiAndMerge but let you configure the flattening process,
// failure management, concurrency...
List<Integer> list3 = multi
.onItem().transformToMulti(i -> Multi.createFrom().items(i, i)).merge()
.collectItems().asList()
.collect().asList()
.await().indefinitely();

List<Integer> list4 = multi
.onItem().transformToMultiAndConcatenate(i -> Multi.createFrom().items(i, i))
.collectItems().asList()
.collect().asList()
.await().indefinitely();

// Equivalent to transformToMultiAndConcatenate but let you configure the flattening process,
// failure management...
List<Integer> list5 = multi
.onItem().transformToMulti(i -> Multi.createFrom().items(i, i)).concatenate()
.collectItems().asList()
.collect().asList()
.await().indefinitely();


Expand Down
10 changes: 5 additions & 5 deletions documentation/src/test/java/guides/TransformItemsAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testUniToMulti(SystemOut out) {
Multi<String> result = uni
.onItem().transformToMulti(item -> Multi.createFrom().items(item, item));
// end::uni-to-multi[]
assertThat(result.collectItems().asList().await().indefinitely()).containsExactly("Cameron", "Cameron");
assertThat(result.collect().asList().await().indefinitely()).containsExactly("Cameron", "Cameron");

// tag::uni-to-multi-2[]
uni
Expand All @@ -90,8 +90,8 @@ public void testMergeAndConcatUni() {
.onItem().transformToUniAndConcatenate(name -> invokeRemoteGreetingService(name));
// end::merge-concat[]

assertThat(merged.collectItems().asList().await().indefinitely()).containsExactly("a", "b");
assertThat(concat.collectItems().asList().await().indefinitely()).containsExactly("a", "b");
assertThat(merged.collect().asList().await().indefinitely()).containsExactly("a", "b");
assertThat(concat.collect().asList().await().indefinitely()).containsExactly("a", "b");
}

@Test
Expand All @@ -106,8 +106,8 @@ public void testMergeAndConcatMulti() {
.onItem().transformToMultiAndConcatenate(item -> someMulti(item));
// end::merge-concat-multi[]

assertThat(merged.collectItems().asList().await().indefinitely()).containsExactly("a", "b");
assertThat(concat.collectItems().asList().await().indefinitely()).containsExactly("a", "b");
assertThat(merged.collect().asList().await().indefinitely()).containsExactly("a", "b");
assertThat(concat.collect().asList().await().indefinitely()).containsExactly("a", "b");
}

private Multi<String> someMulti(String item) {
Expand Down
2 changes: 1 addition & 1 deletion documentation/src/test/java/guides/TransformItemsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void testMultiTransform() {
Multi<String> m = multi
.onItem().transform(i -> i.toUpperCase());
// end::multi-transform[]
assertThat(m.collectItems().asList().await().indefinitely()).containsExactly("A", "B", "C");
assertThat(m.collect().asList().await().indefinitely()).containsExactly("A", "B", "C");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public void testMultiCreation() {

List<String> list = multiFromFlux
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).containsExactly("a", "b", "c");

list = multiFromMono
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).containsExactly("a");

}
Expand Down
12 changes: 6 additions & 6 deletions documentation/src/test/java/guides/integration/RxJavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public void testMultiCreation() {

List<String> list = multiFromObservable
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).containsExactly("a", "b", "c");

list = multiFromFlowable
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).containsExactly("a", "b", "c");

Completable completable = getCompletable();
Expand All @@ -59,22 +59,22 @@ public void testMultiCreation() {

list = multiFromCompletable
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).isEmpty();

list = multiFromSingle
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).containsExactly("a");

list = multiFromMaybe
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).containsExactly("a");

list = multiFromEmptyMaybe
.onItem().transform(Object::toString)
.collectItems().asList().await().indefinitely();
.collect().asList().await().indefinitely();
assertThat(list).isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class CollectingItemsTest {
public void testList() {
// tag::list[]
Multi<String> multi = getMulti();
Uni<List<String>> uni = multi.collectItems().asList();
Uni<List<String>> uni = multi.collect().asList();
// end::list[]

assertThat(uni.await().indefinitely()).containsExactly("a", "b", "c");

// tag::first[]
Uni<String> first = multi.collectItems().first();
Uni<String> last = multi.collectItems().last();
Uni<String> first = multi.collect().first();
Uni<String> last = multi.collect().last();
// end::first[]

assertThat(first.await().indefinitely()).isEqualTo("a");
Expand All @@ -40,7 +40,7 @@ public void testMap() {
// tag::map[]
Multi<String> multi = getMulti();
Uni<Map<String, String>> uni =
multi.collectItems()
multi.collect()
.asMap(item -> getUniqueKeyForItem(item));
// end::map[]

Expand All @@ -52,7 +52,7 @@ public void testMultiMap() {
// tag::multimap[]
Multi<String> multi = getMulti();
Uni<Map<String, Collection<String>>> uni =
multi.collectItems()
multi.collect()
.asMultiMap(item -> getKeyForItem(item));
// end::multimap[]

Expand All @@ -63,14 +63,14 @@ public void testMultiMap() {
public void testCustomAccumulator() {
// tag::accumulator[]
Multi<String> multi = getMulti();
Uni<MyCollection> uni = multi.collectItems()
Uni<MyCollection> uni = multi.collect()
.in(MyCollection::new, (col, item) -> col.add(item));
// end::accumulator[]

assertThat(uni.await().indefinitely()).hasSize(3);

// tag::collector[]
Uni<Long> count = multi.collectItems()
Uni<Long> count = multi.collect()
.with(Collectors.counting());
// end::collector[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public void testCombineLast() {
.latestItems().using(list -> combineItems(list));
// end::combine-last[]

List<Tuple2<A, B>> list = multi1.collectItems().asList().await().indefinitely();
List<Tuple2<A, B>> list = multi1.collect().asList().await().indefinitely();
assertThat(list).hasSize(3);
assertThat(list.toString()).contains("a3", "b1", "b2", "b3").doesNotContain("a1", "a2");
List<String> strings = multi2.collectItems().asList().await().indefinitely();
List<String> strings = multi2.collect().asList().await().indefinitely();
assertThat(strings).hasSize(3).containsExactly("a3b1", "a3b2", "a3b3");
}

Expand Down
6 changes: 3 additions & 3 deletions documentation/src/test/java/guides/operators/DelayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void testDelayMulti() {
Uni.createFrom().nullItem().onItem().delayIt().by(Duration.ofMillis(10))
);
// end::delay-multi[]
assertThat(delayed.collectItems().asList().await().indefinitely()).containsExactly(1, 2, 3, 4, 5);
assertThat(delayed.collect().asList().await().indefinitely()).containsExactly(1, 2, 3, 4, 5);
}

@Test
Expand All @@ -60,7 +60,7 @@ public void testThrottling() {
Multi<Integer> delayed = Multi.createBy().combining().streams(ticks, multi)
.using((x, item) -> item);
// end::throttling-multi[]
assertThat(delayed.collectItems().asList().await().indefinitely()).containsExactly(1, 2, 3, 4, 5);
assertThat(delayed.collect().asList().await().indefinitely()).containsExactly(1, 2, 3, 4, 5);
}

@Test
Expand All @@ -73,7 +73,7 @@ public void testDelayMultiRandom() {
return Uni.createFrom().nullItem().onItem().delayIt().by(delay);
});
// end::delay-multi-random[]
assertThat(delayed.collectItems().asList()
assertThat(delayed.collect().asList()
.await().indefinitely())
.containsExactlyInAnyOrder(1, 2, 3, 4, 5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void test(SystemOut out) {
.onItem()
.invoke(s -> System.out.println("Received item `" + s + "` on thread: "
+ Thread.currentThread().getName()))
.collectItems().first()
.collect().first()
.await().indefinitely();
// end::code[]

Expand Down
Loading