Skip to content

Commit

Permalink
If empty review issues 1
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kec <daniel.kec@oracle.com>
  • Loading branch information
danielkec committed Oct 6, 2021
1 parent b20a794 commit d3adb3f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ default Multi<T> onTerminate(Runnable onTerminate) {
* @return Multi
*/
default Multi<T> ifEmpty(Runnable ifEmpty) {
Objects.requireNonNull(ifEmpty, "IfEmpty callback is null");
Objects.requireNonNull(ifEmpty, "ifEmpty callback is null");
return new MultiIfEmptyPublisher<>(this, ifEmpty);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ default Single<T> onTerminate(Runnable onTerminate) {
* @return Multi
*/
default Single<T> ifEmpty(Runnable ifEmpty) {
Objects.requireNonNull(ifEmpty, "IfEmpty callback is null");
Objects.requireNonNull(ifEmpty, "ifEmpty callback is null");
return new SingleIfEmptyPublisher<>(this, ifEmpty);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.junit.jupiter.api.Test;

Expand All @@ -39,6 +41,21 @@ void empty() {
assertThat(result, contains("ifEmpty", "onComplete"));
}

@Test
void multipleEmpty() {
List<String> result = new ArrayList<>();
Single.just(Optional.<String>empty())
.flatMapOptional(Function.identity())
.ifEmpty(() -> result.add("ifEmptyOptional"))
.flatMap(s -> Single.<String>empty())
.ifEmpty(() -> result.add("ifEmpty"))
.onComplete(() -> result.add("onComplete"))
.peek(result::add)
.onError(t -> result.add("onError"))
.ignoreElements();
assertThat(result, contains("ifEmptyOptional", "ifEmpty", "onComplete"));
}

@Test
void nonEmpty() {
List<String> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.junit.jupiter.api.Test;

Expand All @@ -39,6 +41,21 @@ void empty() {
assertThat(result, contains("ifEmpty", "onComplete"));
}

@Test
void multipleEmpty() {
List<String> result = new ArrayList<>();
Single.just(Optional.<String>empty())
.flatMapOptional(Function.identity())
.ifEmpty(() -> result.add("ifEmptyOptional"))
.flatMapSingle(s -> Single.<String>empty())
.ifEmpty(() -> result.add("ifEmpty"))
.onComplete(() -> result.add("onComplete"))
.peek(result::add)
.onError(t -> result.add("onError"))
.ignoreElement();
assertThat(result, contains("ifEmptyOptional", "ifEmpty", "onComplete"));
}

@Test
void nonEmpty() {
List<String> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,14 @@ public void update(Routing.Rules rules) {
*/
private void generateWallet(ServerRequest req, ServerResponse res) {
autonomousDbRx.generateWallet(GenerateAutonomousDatabaseWallet.Request.builder())
.map(ApiOptionalResponse::entity)
.flatMap(e -> e.map(Single::just).orElseGet(() -> {
res.status(404).send();
return Single.empty();
}))
.flatMapOptional(ApiOptionalResponse::entity)
.map(GenerateAutonomousDatabaseWallet.Response::walletArchive)
.flatMap(archive -> createDbClient(archive))
.ifEmpty(() -> LOGGER.severe("Unable to obtain wallet!"))
.flatMapSingle(this::createDbClient)
.flatMap(dbClient -> dbClient.execute(exec -> exec.query("SELECT 'Hello world!!' FROM DUAL")))
.first()
.map(dbRow -> dbRow.column(1).as(String.class))
.ifEmpty(() -> res.status(404).send())
.onError(res::send)
.forSingle(res::send);
}
Expand Down

0 comments on commit d3adb3f

Please sign in to comment.