io.vavr.control.Try.onSuccess()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(543)

本文整理了Java中io.vavr.control.Try.onSuccess()方法的一些代码示例,展示了Try.onSuccess()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.onSuccess()方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:onSuccess

Try.onSuccess介绍

[英]Consumes the value if this is a Try.Success.

  1. // prints "1"

[中]如果这是一次尝试,则消耗该值。成功

  1. // prints "1"

代码示例

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Performs the action once the Future is complete and the result is a {@link Try.Success}.
  3. *
  4. * @param action An action to be performed when this future succeeded.
  5. * @return this Future
  6. * @throws NullPointerException if {@code action} is null.
  7. */
  8. default Future<T> onSuccess(Consumer<? super T> action) {
  9. Objects.requireNonNull(action, "action is null");
  10. return onComplete(result -> result.onSuccess(action));
  11. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Converts this to a {@link CompletableFuture}
  3. *
  4. * @return A new {@link CompletableFuture} containing the value
  5. */
  6. @GwtIncompatible
  7. default CompletableFuture<T> toCompletableFuture() {
  8. final CompletableFuture<T> completableFuture = new CompletableFuture<>();
  9. Try.of(this::get)
  10. .onSuccess(completableFuture::complete)
  11. .onFailure(completableFuture::completeExceptionally);
  12. return completableFuture;
  13. }

代码示例来源:origin: vavr-io/vavr

  1. default <U> Future<U> flatMapTry(CheckedFunction1<? super T, ? extends Future<? extends U>> mapper) {
  2. Objects.requireNonNull(mapper, "mapper is null");
  3. return run(executor(), complete ->
  4. onComplete(result -> result.mapTry(mapper)
  5. .onSuccess(future -> future.onComplete(complete::with))
  6. .onFailure(x -> complete.with(Try.failure(x)))
  7. )
  8. );
  9. }

代码示例来源:origin: vavr-io/vavr

  1. .onSuccess(value -> completed.set(complete.with(Try.success(Option.some(value)))))
  2. .onFailure(ignored -> {
  3. if (wasLast) {

代码示例来源:origin: io.vavr/vavr

  1. /**
  2. * Performs the action once the Future is complete and the result is a {@link Try.Success}.
  3. *
  4. * @param action An action to be performed when this future succeeded.
  5. * @return this Future
  6. * @throws NullPointerException if {@code action} is null.
  7. */
  8. default Future<T> onSuccess(Consumer<? super T> action) {
  9. Objects.requireNonNull(action, "action is null");
  10. return onComplete(result -> result.onSuccess(action));
  11. }

代码示例来源:origin: io.vavr/vavr

  1. /**
  2. * Converts this to a {@link CompletableFuture}
  3. *
  4. * @return A new {@link CompletableFuture} containing the value
  5. */
  6. @GwtIncompatible
  7. default CompletableFuture<T> toCompletableFuture() {
  8. final CompletableFuture<T> completableFuture = new CompletableFuture<>();
  9. Try.of(this::get)
  10. .onSuccess(completableFuture::complete)
  11. .onFailure(completableFuture::completeExceptionally);
  12. return completableFuture;
  13. }

代码示例来源:origin: Tristan971/Lyrebird

  1. @Override
  2. public void refresh() {
  3. if (!sessionManager.isLoggedInProperty().getValue()) {
  4. LOG.debug("Logged out, not refreshing direct messages.");
  5. return;
  6. }
  7. CompletableFuture.runAsync(() -> {
  8. LOG.debug("Requesting last direct messages.");
  9. sessionManager.getCurrentTwitter()
  10. .mapTry(twitter -> twitter.getDirectMessages(20))
  11. .onSuccess(this::addDirectMessages)
  12. .onFailure(err -> LOG.error("Could not load direct messages successfully!", err));
  13. });
  14. }

代码示例来源:origin: Tristan971/Lyrebird

  1. public void openUserDetails(final long targetUserId) {
  2. findUser(targetUserId)
  3. .onSuccess(this::openUserDetails)
  4. .onFailure(err -> ExceptionHandler.displayExceptionPane(
  5. "Unknown user!",
  6. "Can't map this user's userId to an actual Twitter user!",
  7. err
  8. ));
  9. }

代码示例来源:origin: Tristan971/Lyrebird

  1. public void openUserDetails(final String screenName) {
  2. findUser(screenName)
  3. .onSuccess(this::openUserDetails)
  4. .onFailure(err -> ExceptionHandler.displayExceptionPane(
  5. "Unknown user!",
  6. "Can't map this user's screen name (@...) to an actual Twitter user!",
  7. err
  8. ));
  9. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Called when the user requests the displaying of the current user's detailed view.
  3. *
  4. * @see Screen#USER_VIEW
  5. */
  6. private void loadDetailsForCurrentUser() {
  7. sessionManager.currentSessionProperty()
  8. .getValue()
  9. .getTwitterUser()
  10. .onSuccess(userDetailsService::openUserDetails);
  11. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Fetches, shows and moves the main application stage to the front.
  3. */
  4. private void showMainStage() {
  5. CompletableFuture.runAsync(
  6. () -> stageManager.getSingle(Screen.ROOT_VIEW)
  7. .toTry(IllegalStateException::new)
  8. .onSuccess(stage -> {
  9. stage.show();
  10. stage.setIconified(false);
  11. }).onFailure(err -> LOG.error("Could not show main stage!", err)),
  12. Platform::runLater
  13. );
  14. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Asynchronously loads the last tweets available
  3. */
  4. public void refresh() {
  5. CompletableFuture.runAsync(() -> {
  6. if (sessionManager.getCurrentTwitter().getOrElse((Twitter) null) == null) {
  7. return;
  8. }
  9. getLocalLogger().debug("Requesting last tweets in timeline.");
  10. sessionManager.getCurrentTwitter()
  11. .mapTry(this::initialLoad)
  12. .onSuccess(this::addTweets)
  13. .onFailure(err -> getLocalLogger().error("Could not refresh!", err))
  14. .andThen(() -> isFirstCall.set(false));
  15. });
  16. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Asynchronous load and set of the user profile picture.
  3. *
  4. * @param user The user with which to work, mostly because we do not keep a reference to it inside this class.
  5. */
  6. private void loadAndSetUserAvatar(final Try<User> user) {
  7. user.map(User::getOriginalProfileImageURLHttps)
  8. .map(imageUrl -> asyncIO.loadImageMiniature(imageUrl, 128.0, 128.0))
  9. .onSuccess(loadRequest -> loadRequest.thenAcceptAsync(userProfilePicture::setImage, Platform::runLater));
  10. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Loads the current user's account view on the top of the bar.
  3. */
  4. private void loadCurrentAccountPanel() {
  5. easyFxml.loadNode(CURRENT_ACCOUNT)
  6. .getNode()
  7. .onSuccess(container::setTop)
  8. .onFailure(err -> displayExceptionPane(
  9. "Could not load current user!",
  10. "There was an error mapping the current session to a twitter account.",
  11. err
  12. ));
  13. }

代码示例来源:origin: Tristan971/Lyrebird

  1. private void createTabForPal(final User user) {
  2. if (loadedPals.contains(user)) return;
  3. LOG.debug("Creating a conversation tab for conversation with {}", user.getScreenName());
  4. easyFxml.loadNode(DIRECT_MESSAGE_CONVERSATION, Pane.class, DMConversationController.class)
  5. .afterControllerLoaded(dmc -> dmc.setPal(user))
  6. .getNode()
  7. .recover(ExceptionHandler::fromThrowable)
  8. .map(conversation -> new Tab(user.getName(), conversation))
  9. .onSuccess(tab -> Platform.runLater(() -> {
  10. LOG.debug("Adding [{}] as tab for user {}", tab.getText(), user.getScreenName());
  11. this.conversationsManaged.add(tab);
  12. }));
  13. loadedPals.add(user);
  14. }

代码示例来源:origin: io.vavr/vavr

  1. default <U> Future<U> flatMapTry(CheckedFunction1<? super T, ? extends Future<? extends U>> mapper) {
  2. Objects.requireNonNull(mapper, "mapper is null");
  3. return run(executor(), complete ->
  4. onComplete(result -> result.mapTry(mapper)
  5. .onSuccess(future -> future.onComplete(complete::with))
  6. .onFailure(x -> complete.with(Try.failure(x)))
  7. )
  8. );
  9. }

代码示例来源:origin: Tristan971/Lyrebird

  1. private void displayApplicationAuthor() {
  2. applicationAuthorProfileLink.setOnAction(e -> userDetailsService.openUserDetails("_tristan971_"));
  3. userDetailsService.findUser("_tristan971_")
  4. .map(User::getProfileImageURLHttps)
  5. .map(cachedMedia::loadImage)
  6. .onSuccess(applicationAuthorProfilePicture::setImage)
  7. .andThen(() -> applicationAuthorProfilePicture.setClip(Clipping.getCircleClip(16.0)));
  8. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Loads the target user's timeline into the view.
  3. */
  4. private void loadTargetUserTimeline() {
  5. final User user = targetUserProp.getValue();
  6. easyFxml.loadNode(FxComponent.USER_TIMELINE, Pane.class, UserTimelineController.class)
  7. .afterControllerLoaded(utc -> utc.setTargetUser(user))
  8. .afterNodeLoaded(userDetailsTimeline -> VBox.setVgrow(userDetailsTimeline, Priority.ALWAYS))
  9. .getNode()
  10. .recover(ExceptionHandler::fromThrowable)
  11. .onSuccess(container.getChildren()::add);
  12. }

代码示例来源:origin: Tristan971/Lyrebird

  1. /**
  2. * Opens the detailed view of a given user.
  3. *
  4. * @param targetUser the user whose detailed view is requested to be shown
  5. */
  6. public void openUserDetails(final User targetUser) {
  7. LOG.info("Opening detailed view of user : {} (@{})", targetUser.getName(), targetUser.getScreenName());
  8. easyFxml.loadNode(Screen.USER_VIEW, Pane.class, UserViewController.class)
  9. .afterControllerLoaded(uvc -> uvc.targetUserProperty().setValue(targetUser))
  10. .getNode()
  11. .recover(ExceptionHandler::fromThrowable)
  12. .onSuccess(userDetailsPane -> {
  13. final String stageTitle = targetUser.getName() + " (@" + targetUser.getScreenName() + ")";
  14. Stages.stageOf(stageTitle, userDetailsPane).thenAcceptAsync(Stages::scheduleDisplaying);
  15. });
  16. }

代码示例来源:origin: Tristan971/Lyrebird

  1. private void setUpInteractionButtons() {
  2. interactionBox.visibleProperty().bind(embeddedProperty.not());
  3. interactionBox.managedProperty().bind(embeddedProperty.not());
  4. easyFxml.loadNode(TWEET_INTERACTION_BOX, Pane.class, TweetInteractionPaneController.class)
  5. .afterControllerLoaded(tipc -> tipc.targetStatusProperty().bind(currentStatus))
  6. .getNode()
  7. .recover(ExceptionHandler::fromThrowable)
  8. .onSuccess(interactionBox::setCenter);
  9. }

相关文章