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

Add additional helper methods to wrap CompletableFutures.successfulAsList #93

Closed

Conversation

KathrynN
Copy link

@KathrynN KathrynN commented Jul 5, 2023

This PR adds two helper functions to CompletableFutures

  • One that takes a list of input and a transformer, to avoid the necessity of many developers writing variations of the below code, only to have the stream they just turned into a list, turned into a stream again.
    CompletableFutures.successfulAsList(
        domainSpecificEntity.videoSource().stream()
            .map(
                video ->
                    originalFileLocationStore.insert(....))
            .toList(), // this bit
        e -> {
          LOG.error("failed to insert video for creator {}", domainSpecificEntity.spotifyUri(), e);
          return null;
        });
  • One that allows the inclusion of a piece of data describing the CompletableFuture, allowing the possibility to create meaningful default objects from the input to a CompletableFuture list.
    var creatorsFuture =
        CompletableFutures.successfulAsList(
            creatorUris.stream().map(creatorDataStore::getCreator).collect(Collectors.toList()),
            t -> {
              if (t instanceof CreatorNotFoundException) {
                return CreatorMetadata.newBuilder()
                    .setSpotifyUri(((CreatorNotFoundException) t).getUri())
                    .build();
              }
              return null; // here because it's not a known exception I lose the creator's info.
            });

Copy link
Contributor

@caesar-ralf caesar-ralf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I would like someone that has been maintaining the library longer to take a look as well. Thanks for the contribution @KathrynN

@mbruggmann
Copy link
Member

mbruggmann commented Jul 5, 2023

The first case is quite well supported already using something like this, no?

values.stream()
    .map(value -> asyncOperation(value))
    .map(future -> future.exceptionally(...))
    .collect(joinList());

@KathrynN
Copy link
Author

Spoken to both Kristofer and Marc, and we all three decided that this contribution didn't add enough value to be merged

@KathrynN KathrynN closed this Nov 22, 2023
@KathrynN KathrynN deleted the more-succesful-as-list-variants branch November 22, 2023 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants