Skip to content

Commit

Permalink
Use Optional::or instead of Stream::findFirst on a temporary stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Melnychuk authored and kokosing committed Aug 10, 2021
1 parent 9d1e6ac commit 5d63f07
Showing 1 changed file with 3 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Streams;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Optional;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;

Expand All @@ -38,13 +36,9 @@ public BigQueryCredentialsSupplier(Optional<String> credentialsKey, Optional<Str
requireNonNull(credentialsKey, "credentialsKey is null");
requireNonNull(credentialsFile, "credentialsFile is null");
// lazy creation, cache once it's created
this.credentialsCreator = Suppliers.memoize(() -> {
Optional<Credentials> credentialsFromKey = credentialsKey.map(BigQueryCredentialsSupplier::createCredentialsFromKey);
Optional<Credentials> credentialsFromFile = credentialsFile.map(BigQueryCredentialsSupplier::createCredentialsFromFile);
return Stream.of(credentialsFromKey, credentialsFromFile)
.flatMap(Streams::stream)
.findFirst();
});
this.credentialsCreator = Suppliers.memoize(() ->
credentialsKey.map(BigQueryCredentialsSupplier::createCredentialsFromKey)
.or(() -> credentialsFile.map(BigQueryCredentialsSupplier::createCredentialsFromFile)));
}

private static Credentials createCredentialsFromKey(String key)
Expand Down

0 comments on commit 5d63f07

Please sign in to comment.