protected boolean matchesSafely()

in future/src/main/java/com/spotify/hamcrest/future/SuccessfullyCompletedCompletionStage.java [47:79]


  protected boolean matchesSafely(
      final CompletionStage<? extends T> stage, final Description mismatchDescription) {
    final CompletableFuture<? extends T> future = stage.toCompletableFuture();
    if (future.isDone()) {
      if (future.isCancelled()) {
        mismatchDescription.appendText("a stage that was cancelled");
        return false;
      } else if (future.isCompletedExceptionally()) {
        try {
          future.getNow(null);
          throw new AssertionError(
              "This should never happen because the future has completed exceptionally.");
        } catch (CompletionException e) {
          mismatchDescription
              .appendText("a stage that completed exceptionally with ")
              .appendText(getStackTraceAsString(e.getCause()));
        }
        return false;
      } else {
        final T item = future.getNow(null);
        if (matcher.matches(item)) {
          return true;
        } else {
          mismatchDescription.appendText("a stage that completed to a value that ");
          matcher.describeMismatch(item, mismatchDescription);
          return false;
        }
      }
    } else {
      mismatchDescription.appendText("a stage that was not done");
      return false;
    }
  }