Skip to content

Commit

Permalink
refactor: reworked Uni combination APIs
Browse files Browse the repository at this point in the history
- Replaces combinedWith by with((a,b,c) -> d).
- Introduces withUni((a,b,c) -> Uni).

Signed-off-by: Julien Ponge <[email protected]>
  • Loading branch information
lordvlad authored and jponge committed Sep 5, 2023
1 parent 2c4872a commit 7baf62f
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .build/WaitForCentral.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ boolean get(WebClient client, List<String> urls) {
}

Uni<Boolean> completed = Uni.combine().all().unis(unis)
.combinedWith(list -> {
.with(list -> {
boolean done = true;
for (Object r : list) {
Result res = (Result) r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void testZip3() throws InterruptedException {
}).runSubscriptionOn(executor);

Uni.combine().all().unis(uni1, uni2, uni3)
.combinedWith((item1, item2, item3) -> {
.with((item1, item2, item3) -> {
assertThat(ctx).isSameAs(MyContext.get());
return item1 + item2 + item3;
})
Expand Down Expand Up @@ -213,7 +213,7 @@ public void testZip2() throws InterruptedException {

// Same but with biFunction
Uni.combine().all().unis(uni1, uni2)
.combinedWith((a, b) -> {
.with((a, b) -> {
assertThat(ctx).isSameAs(MyContext.get());
return a + b;
})
Expand Down Expand Up @@ -284,7 +284,7 @@ public void testZip() throws InterruptedException {
}).runSubscriptionOn(executor);

Uni.combine().all().unis(uni1, uni2, uni3, uni4)
.combinedWith((item1, item2, item3, item4) -> {
.with((item1, item2, item3, item4) -> {
assertThat(ctx).isSameAs(MyContext.get());
return item1 + item2 + item3 + item4;
})
Expand All @@ -297,7 +297,7 @@ public void testZip() throws InterruptedException {
});

Uni.combine().all().unis(uni1, uni2, uni3, uni4, uni5)
.combinedWith((item1, item2, item3, item4, item5) -> {
.with((item1, item2, item3, item4, item5) -> {
assertThat(ctx).isSameAs(MyContext.get());
return item1 + item2 + item3 + item4 + item5;
})
Expand All @@ -310,7 +310,7 @@ public void testZip() throws InterruptedException {
});

Uni.combine().all().unis(uni1, uni2, uni3, uni4, uni5, uni6)
.combinedWith((item1, item2, item3, item4, item5, item6) -> {
.with((item1, item2, item3, item4, item5, item6) -> {
assertThat(ctx).isSameAs(MyContext.get());
return item1 + item2 + item3 + item4 + item5 + item6;
})
Expand All @@ -323,7 +323,7 @@ public void testZip() throws InterruptedException {
});

Uni.combine().all().unis(uni1, uni2, uni3, uni4, uni5, uni6, uni7)
.combinedWith((item1, item2, item3, item4, item5, item6, item7) -> {
.with((item1, item2, item3, item4, item5, item6, item7) -> {
assertThat(ctx).isSameAs(MyContext.get());
return item1 + item2 + item3 + item4 + item5 + item6 + item7;
})
Expand All @@ -336,7 +336,7 @@ public void testZip() throws InterruptedException {
});

Uni.combine().all().unis(uni1, uni2, uni3, uni4, uni5, uni6, uni7, uni8)
.combinedWith((item1, item2, item3, item4, item5, item6, item7, item8) -> {
.with((item1, item2, item3, item4, item5, item6, item7, item8) -> {
assertThat(ctx).isSameAs(MyContext.get());
return item1 + item2 + item3 + item4 + item5 + item6 + item7 + item8;
})
Expand All @@ -349,7 +349,7 @@ public void testZip() throws InterruptedException {
});

Uni.combine().all().unis(uni1, uni2, uni3, uni4, uni5, uni6, uni7, uni8, uni9)
.combinedWith((item1, item2, item3, item4, item5, item6, item7, item8, item9) -> {
.with((item1, item2, item3, item4, item5, item6, item7, item8, item9) -> {
assertThat(ctx).isSameAs(MyContext.get());
return item1 + item2 + item3 + item4 + item5 + item6 + item7 + item8 + item9;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import guides.extension.SystemOutCaptureExtension;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.tuples.Tuple;
import io.smallrye.mutiny.tuples.Tuple2;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -49,7 +48,7 @@ public void testWithUni(SystemOut out) throws InterruptedException {

// <combined-with>
Uni<Map<String, Response>> uni = Uni.combine()
.all().unis(uniA, uniB).combinedWith(
.all().unis(uniA, uniB).with(
listOfResponses -> {
Map<String, Response> map = new LinkedHashMap<>();
map.put("A", (Response) listOfResponses.get(0));
Expand All @@ -61,6 +60,21 @@ public void testWithUni(SystemOut out) throws InterruptedException {

assertThat(uni.await().indefinitely()).containsKeys("A", "B");

// <combined-with-uni>
Uni<Map<String, Response>> uni1 = Uni.combine()
.all().unis(uniA, uniB).withUni(
listOfResponses -> {
Map<String, Response> map = new LinkedHashMap<>();
map.put("A", (Response) listOfResponses.get(0));
map.put("B", (Response) listOfResponses.get(1));
return Uni.createFrom().item(map);
}
);
// </combined-with-uni>

assertThat(uni1.await().indefinitely()).containsKeys("A", "B");


Thread.sleep(100);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,24 @@ public Uni<Tuple2<T1, T2>> asTuple() {
*
* @param combinator the combinator function, must not be {@code null}
* @param <O> the type of item
* @return the resulting {@link Uni}. The items are combined into a {@link Tuple2 Tuple2&lt;T1, T2&gt;}.
* @return the resulting {@code Uni<O>}. The items are combined into {@link O}
* @deprecated use {@link #with(BiFunction)} instead
*/
@Deprecated
@CheckReturnValue
public <O> Uni<O> combinedWith(BiFunction<T1, T2, O> combinator) {
return with(combinator);
}

/**
* Creates the resulting {@link Uni}. The items are combined using the given combinator function.
*
* @param combinator the combinator function, must not be {@code null}
* @param <O> the type of item
* @return the resulting {@code Uni<O>}. The items are combined into {@link O}
*/
@CheckReturnValue
public <O> Uni<O> with(BiFunction<T1, T2, O> combinator) {
BiFunction<T1, T2, O> actual = Infrastructure.decorate(nonNull(combinator, "combinator"));
return combine(actual);
}
Expand All @@ -68,7 +82,32 @@ private <O> Uni<O> combine(BiFunction<T1, T2, O> combinator) {
T2 item2 = (T2) list.get(1);
return combinator.apply(item1, item2);
};
return super.combinedWith(function);
return super.with(function);
}

/**
* Creates the resulting {@link Uni}. The items are combined using the given combinator function,
* and the resulting {@code Uni<Uni<O>>} is flattened.
*
* @param combinator the combinator function, must not be {@code null}
* @param <O> the type of item
* @return the resulting {@link Uni}. The items are combined into a {@link Tuple2 Tuple2&lt;T1, T2&gt;}.
*/
@CheckReturnValue
public <O> Uni<O> withUni(BiFunction<T1, T2, Uni<O>> combinator) {
BiFunction<T1, T2, Uni<O>> actual = Infrastructure.decorate(nonNull(combinator, "combinator"));
return combineUni(actual);
}

@SuppressWarnings("unchecked")
private <O> Uni<O> combineUni(BiFunction<T1, T2, Uni<O>> combinator) {
Function<List<?>, Uni<O>> function = list -> {
Tuples.ensureArity(list, 2);
T1 item1 = (T1) list.get(0);
T2 item2 = (T2) list.get(1);
return combinator.apply(item1, item2);
};
return super.with(function).flatMap(Function.identity());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ public Uni<Tuple3<T1, T2, T3>> asTuple() {
return combine(Tuple3::of);
}

/**
* @deprecated use {@link #with(Functions.Function3)} instead
*/
@Deprecated
@CheckReturnValue
public <O> Uni<O> combinedWith(Functions.Function3<T1, T2, T3, O> combinator) {
return with(combinator);
}

@CheckReturnValue
public <O> Uni<O> with(Functions.Function3<T1, T2, T3, O> combinator) {
Functions.Function3<T1, T2, T3, O> actual = Infrastructure.decorate(nonNull(combinator, "combinator"));
return combine(actual);
}
Expand All @@ -45,7 +54,24 @@ private <O> Uni<O> combine(Functions.Function3<T1, T2, T3, O> combinator) {
T3 item3 = (T3) list.get(2);
return combinator.apply(item1, item2, item3);
};
return super.combinedWith(function);
return super.with(function);
}

@CheckReturnValue
public <O> Uni<O> withUni(Functions.Function3<T1, T2, T3, Uni<O>> combinator) {
Functions.Function3<T1, T2, T3, Uni<O>> actual = Infrastructure.decorate(nonNull(combinator, "combinator"));
return combineUni(actual);
}

@SuppressWarnings("unchecked")
private <O> Uni<O> combineUni(Functions.Function3<T1, T2, T3, Uni<O>> combinator) {
Function<List<?>, Uni<O>> function = list -> {
Tuples.ensureArity(list, 3);
T1 item1 = (T1) list.get(0);
T2 item2 = (T2) list.get(1);
T3 item3 = (T3) list.get(2);
return combinator.apply(item1, item2, item3);
};
return super.with(function).flatMap(Function.identity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ public Uni<Tuple4<T1, T2, T3, T4>> asTuple() {
return combine(Tuple4::of);
}

/**
* @deprecated use {@link #with(Functions.Function4)} instead
*/
@Deprecated
@CheckReturnValue
public <O> Uni<O> combinedWith(Functions.Function4<T1, T2, T3, T4, O> combinator) {
return with(combinator);
}

@CheckReturnValue
public <O> Uni<O> with(Functions.Function4<T1, T2, T3, T4, O> combinator) {
Functions.Function4<T1, T2, T3, T4, O> actual = Infrastructure.decorate(nonNull(combinator, "combinator"));
return combine(actual);
}
Expand All @@ -46,7 +55,26 @@ private <O> Uni<O> combine(Functions.Function4<T1, T2, T3, T4, O> combinator) {
T4 item4 = (T4) list.get(3);
return combinator.apply(item1, item2, item3, item4);
};
return super.combinedWith(function);
return super.with(function);
}

@CheckReturnValue
public <O> Uni<O> withUni(Functions.Function4<T1, T2, T3, T4, Uni<O>> combinator) {
Functions.Function4<T1, T2, T3, T4, Uni<O>> actual = Infrastructure.decorate(nonNull(combinator, "combinator"));
return combineUni(actual);
}

@SuppressWarnings("unchecked")
private <O> Uni<O> combineUni(Functions.Function4<T1, T2, T3, T4, Uni<O>> combinator) {
Function<List<?>, Uni<O>> function = list -> {
Tuples.ensureArity(list, 4);
T1 item1 = (T1) list.get(0);
T2 item2 = (T2) list.get(1);
T3 item3 = (T3) list.get(2);
T4 item4 = (T4) list.get(3);
return combinator.apply(item1, item2, item3, item4);
};
return super.with(function).flatMap(Function.identity());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ public Uni<Tuple5<T1, T2, T3, T4, T5>> asTuple() {
return combine(Tuple5::of);
}

/**
* @deprecated use {@link #with(Functions.Function5)} instead
*/
@Deprecated
@CheckReturnValue
public <O> Uni<O> combinedWith(Functions.Function5<T1, T2, T3, T4, T5, O> combinator) {
return with(combinator);
}

@CheckReturnValue
public <O> Uni<O> with(Functions.Function5<T1, T2, T3, T4, T5, O> combinator) {
Functions.Function5<T1, T2, T3, T4, T5, O> actual = Infrastructure
.decorate(nonNull(combinator, "combinator"));
return combine(actual);
Expand All @@ -49,7 +58,27 @@ private <O> Uni<O> combine(Functions.Function5<T1, T2, T3, T4, T5, O> combinator
T5 item5 = (T5) list.get(4);
return combinator.apply(item1, item2, item3, item4, item5);
};
return super.combinedWith(function);
return super.with(function);
}

@CheckReturnValue
public <O> Uni<O> withUni(Functions.Function5<T1, T2, T3, T4, T5, Uni<O>> combinator) {
Functions.Function5<T1, T2, T3, T4, T5, Uni<O>> actual = Infrastructure
.decorate(nonNull(combinator, "combinator"));
return combineUni(actual);
}

@SuppressWarnings("unchecked")
private <O> Uni<O> combineUni(Functions.Function5<T1, T2, T3, T4, T5, Uni<O>> combinator) {
Function<List<?>, Uni<O>> function = list -> {
Tuples.ensureArity(list, 5);
T1 item1 = (T1) list.get(0);
T2 item2 = (T2) list.get(1);
T3 item3 = (T3) list.get(2);
T4 item4 = (T4) list.get(3);
T5 item5 = (T5) list.get(4);
return combinator.apply(item1, item2, item3, item4, item5);
};
return super.with(function).flatMap(Function.identity());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ public Uni<Tuple6<T1, T2, T3, T4, T5, T6>> asTuple() {
return combine(Tuple6::of);
}

/**
* @deprecated use {@link #with(Functions.Function6)} instead
*/
@Deprecated
@CheckReturnValue
public <O> Uni<O> combinedWith(Functions.Function6<T1, T2, T3, T4, T5, T6, O> combinator) {
return with(combinator);
}

@CheckReturnValue
public <O> Uni<O> with(Functions.Function6<T1, T2, T3, T4, T5, T6, O> combinator) {
Functions.Function6<T1, T2, T3, T4, T5, T6, O> actual = Infrastructure
.decorate(nonNull(combinator, "combinator"));
return combine(actual);
Expand All @@ -50,7 +59,29 @@ private <O> Uni<O> combine(Functions.Function6<T1, T2, T3, T4, T5, T6, O> combin
T6 item6 = (T6) list.get(5);
return combinator.apply(item1, item2, item3, item4, item5, item6);
};
return super.combinedWith(function);
return super.with(function);
}

@CheckReturnValue
public <O> Uni<O> withUni(Functions.Function6<T1, T2, T3, T4, T5, T6, Uni<O>> combinator) {
Functions.Function6<T1, T2, T3, T4, T5, T6, Uni<O>> actual = Infrastructure
.decorate(nonNull(combinator, "combinator"));
return combineUni(actual);
}

@SuppressWarnings("unchecked")
private <O> Uni<O> combineUni(Functions.Function6<T1, T2, T3, T4, T5, T6, Uni<O>> combinator) {
Function<List<?>, Uni<O>> function = list -> {
Tuples.ensureArity(list, 6);
T1 item1 = (T1) list.get(0);
T2 item2 = (T2) list.get(1);
T3 item3 = (T3) list.get(2);
T4 item4 = (T4) list.get(3);
T5 item5 = (T5) list.get(4);
T6 item6 = (T6) list.get(5);
return combinator.apply(item1, item2, item3, item4, item5, item6);
};
return super.with(function).flatMap(Function.identity());
}

}
Loading

0 comments on commit 7baf62f

Please sign in to comment.