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

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

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

Try.failure介绍

[英]Creates a Failure that contains the given exception. Shortcut for new Failure<>(exception).
[中]创建包含给定异常的故障。新故障的快捷方式<>(例外)。

代码示例

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

  1. /**
  2. * Alias for {@link Try#failure(Throwable)}
  3. *
  4. * @param <T> Component type of the {@code Try}.
  5. * @param exception An exception.
  6. * @return A new {@link Try.Failure}.
  7. */
  8. @SuppressWarnings("unchecked")
  9. public static <T> Try.Failure<T> Failure(Throwable exception) {
  10. return (Try.Failure<T>) Try.failure(exception);
  11. }

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

  1. /**
  2. * Completes this {@code Promise} with the given {@code exception}.
  3. *
  4. * @param exception An exception.
  5. * @return This {@code Promise}.
  6. * @throws IllegalStateException if this {@code Promise} has already been completed.
  7. */
  8. default Promise<T> failure(Throwable exception) {
  9. return complete(Try.failure(exception));
  10. }

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

  1. /**
  2. * Completes this {@code Promise} with the given {@code exception}.
  3. *
  4. * @param exception An exception.
  5. * @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
  6. */
  7. default boolean tryFailure(Throwable exception) {
  8. return tryComplete(Try.failure(exception));
  9. }
  10. }

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

  1. private void handleUncaughtException(Throwable x) {
  2. tryComplete(Try.failure(x));
  3. }

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

  1. /**
  2. * Creates a failed {@code Future} with the given {@code exception}, backed by the given {@link Executor}.
  3. *
  4. * @param executor An {@link Executor}.
  5. * @param exception The reason why it failed.
  6. * @param <T> The value type of a successful result.
  7. * @return A failed {@code Future}.
  8. * @throws NullPointerException if executor or exception is null
  9. */
  10. static <T> Future<T> failed(Executor executor, Throwable exception) {
  11. Objects.requireNonNull(executor, "executor is null");
  12. Objects.requireNonNull(exception, "exception is null");
  13. return FutureImpl.of(executor, Try.failure(exception));
  14. }

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

  1. /**
  2. * Converts this to a {@link Try}.
  3. * <p>
  4. * If this value is undefined, i.e. empty, then a new {@code Failure(ifEmpty.get())} is returned,
  5. * otherwise a new {@code Success(value)} is returned.
  6. *
  7. * @param ifEmpty an exception supplier
  8. * @return A new {@link Try}.
  9. */
  10. default Try<T> toTry(Supplier<? extends Throwable> ifEmpty) {
  11. Objects.requireNonNull(ifEmpty, "ifEmpty is null");
  12. return isEmpty() ? Try.failure(ifEmpty.get()) : toTry();
  13. }

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

  1. private FutureImpl(Executor executor, Option<Try<T>> value, Queue<Consumer<Try<T>>> actions, Queue<Thread> waiters, Computation<T> computation) {
  2. this.executor = executor;
  3. synchronized (lock) {
  4. this.cancelled = false;
  5. this.value = value;
  6. this.actions = actions;
  7. this.waiters = waiters;
  8. try {
  9. computation.execute(this::tryComplete, this::updateThread);
  10. } catch (Throwable x) {
  11. tryComplete(Try.failure(x));
  12. }
  13. }
  14. }

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

  1. @Override
  2. public boolean cancel(boolean mayInterruptIfRunning) {
  3. if (!isCompleted()) {
  4. synchronized (lock) {
  5. if (!isCompleted()) {
  6. if (mayInterruptIfRunning && this.thread != null) {
  7. this.thread.interrupt();
  8. }
  9. this.cancelled = tryComplete(Try.failure(new CancellationException()));
  10. return this.cancelled;
  11. }
  12. }
  13. }
  14. return false;
  15. }

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

  1. /**
  2. * Returns {@code this} if this is a Failure or this is a Success and the value satisfies the predicate.
  3. * <p>
  4. * Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception
  5. * occurs testing the predicate. The returned Failure wraps a Throwable instance provided by the given
  6. * {@code errorProvider}.
  7. *
  8. * @param predicate A checked predicate
  9. * @param errorProvider A provider of a throwable
  10. * @return a {@code Try} instance
  11. * @throws NullPointerException if {@code predicate} or {@code errorProvider} is null
  12. */
  13. default Try<T> filterTry(CheckedPredicate<? super T> predicate, CheckedFunction1<? super T, ? extends Throwable> errorProvider) {
  14. Objects.requireNonNull(predicate, "predicate is null");
  15. Objects.requireNonNull(errorProvider, "errorProvider is null");
  16. return flatMapTry(t -> predicate.test(t) ? this : failure(errorProvider.apply(t)));
  17. }

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

  1. /**
  2. * Creates a {@code Future} with the given {@link java.util.concurrent.CompletableFuture}, backed by given {@link Executor}
  3. *
  4. * @param executor An {@link Executor}.
  5. * @param future A {@link java.util.concurrent.CompletableFuture}.
  6. * @param <T> Result type of the Future
  7. * @return A new {@code Future} wrapping the result of the {@link java.util.concurrent.CompletableFuture}
  8. * @throws NullPointerException if executor or future is null
  9. */
  10. @GwtIncompatible
  11. static <T> Future<T> fromCompletableFuture(Executor executor, CompletableFuture<T> future) {
  12. Objects.requireNonNull(executor, "executor is null");
  13. Objects.requireNonNull(future, "future is null");
  14. if (future.isDone() || future.isCompletedExceptionally() || future.isCancelled()) {
  15. return fromTry(Try.of(future::get).recoverWith(error -> Try.failure(error.getCause())));
  16. } else {
  17. return run(executor, complete ->
  18. future.handle((t, err) -> complete.with((err == null) ? Try.success(t) : Try.failure(err)))
  19. );
  20. }
  21. }

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

  1. /**
  2. * Transforms the value of this {@code Future}, whether it is a success or a failure.
  3. *
  4. * @param f A transformation
  5. * @param <U> Generic type of transformation {@code Try} result
  6. * @return A {@code Future} of type {@code U}
  7. * @throws NullPointerException if {@code f} is null
  8. */
  9. default <U> Future<U> transformValue(Function<? super Try<T>, ? extends Try<? extends U>> f) {
  10. Objects.requireNonNull(f, "f is null");
  11. return run(executor(), complete ->
  12. onComplete(t -> Try.run(() -> complete.with(f.apply(t)))
  13. .onFailure(x -> complete.with(Try.failure(x)))
  14. )
  15. );
  16. }

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

  1. /**
  2. * Creates a {@code FutureImpl} that is eventually completed.
  3. * The given {@code computation} is <em>asynchronously</em> executed, a new thread is started.
  4. *
  5. * @param executor An {@link Executor} to run and control the computation and to perform the actions.
  6. * @param task A (possibly blocking) computation
  7. * @param <T> value type of the Future
  8. * @return a new {@code FutureImpl} instance
  9. */
  10. static <T> FutureImpl<T> async(Executor executor, Task<? extends T> task) {
  11. // In a single-threaded context this Future may already have been completed during initialization.
  12. return new FutureImpl<>(executor, Option.none(), Queue.empty(), Queue.empty(), (complete, updateThread) ->
  13. executor.execute(() -> {
  14. updateThread.run();
  15. try {
  16. task.run(complete::with);
  17. } catch (Throwable x) {
  18. complete.with(Try.failure(x));
  19. }
  20. })
  21. );
  22. }

代码示例来源: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. /**
  2. * Reduces many {@code Try}s into a single {@code Try} by transforming an
  3. * {@code Iterable<Try<? extends T>>} into a {@code Try<Seq<T>>}. If any of
  4. * the {@code Try}s are {@link Try.Failure}, then this returns a {@link Try.Failure}.
  5. *
  6. * @param values An {@link Iterable} of {@code Try}s
  7. * @param <T> type of the Trys
  8. * @return A {@code Try} of a {@link Seq} of results
  9. * @throws NullPointerException if {@code values} is null
  10. */
  11. static <T> Try<Seq<T>> sequence(Iterable<? extends Try<? extends T>> values) {
  12. Objects.requireNonNull(values, "values is null");
  13. Vector<T> vector = Vector.empty();
  14. for (Try<? extends T> value : values) {
  15. if (value.isFailure()) {
  16. return Try.failure(value.getCause());
  17. }
  18. vector = vector.append(value.get());
  19. }
  20. return Try.success(vector);
  21. }

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

  1. /**
  2. * A projection that inverses the result of this Future.
  3. * <p>
  4. * If this Future succeeds, the failed projection returns a failure containing a {@code NoSuchElementException}.
  5. * <p>
  6. * If this Future fails, the failed projection returns a success containing the exception.
  7. *
  8. * @return A new Future which contains an exception at a point of time.
  9. */
  10. default Future<Throwable> failed() {
  11. return run(executor(), complete ->
  12. onComplete(result -> {
  13. if (result.isFailure()) {
  14. complete.with(Try.success(result.getCause()));
  15. } else {
  16. complete.with(Try.failure(new NoSuchElementException("Future.failed completed without a throwable")));
  17. }
  18. })
  19. );
  20. }

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

  1. /**
  2. * Handles a failure of this Future by returning the result of another Future.
  3. * <p>
  4. * Example:
  5. * <pre><code>
  6. * // = "oh!"
  7. * Future.of(() -&gt; { throw new Error("oh!"); }).recoverWith(x -&gt; Future.of(x::getMessage));
  8. * </code></pre>
  9. *
  10. * @param f A function which takes the exception of a failure and returns a new future.
  11. * @return A new Future.
  12. * @throws NullPointerException if {@code f} is null
  13. */
  14. default Future<T> recoverWith(Function<? super Throwable, ? extends Future<? extends T>> f) {
  15. Objects.requireNonNull(f, "f is null");
  16. return run(executor(), complete ->
  17. onComplete(t -> {
  18. if (t.isFailure()) {
  19. Try.run(() -> f.apply(t.getCause()).onComplete(complete::with))
  20. .onFailure(x -> complete.with(Try.failure(x)));
  21. } else {
  22. complete.with(t);
  23. }
  24. })
  25. );
  26. }

代码示例来源:origin: jdbi/jdbi

  1. @Test
  2. public void testGetTryFailureShouldReturnAllRows() {
  3. List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
  4. .bind("name", Try.failure(new Throwable()))
  5. .mapToBean(Something.class)
  6. .list();
  7. assertThat(result).hasSize(2);
  8. }

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

  1. /**
  2. * Maps the cause to a new exception if this is a {@code Failure} or returns this instance if this is a {@code Success}.
  3. * <p>
  4. * If none of the given cases matches the cause, the same {@code Failure} is returned.
  5. *
  6. * @param cases A not necessarily exhaustive sequence of cases that will be matched against a cause.
  7. * @return A new {@code Try} if this is a {@code Failure}, otherwise this.
  8. */
  9. @GwtIncompatible
  10. @SuppressWarnings({ "unchecked", "varargs" })
  11. default Try<T> mapFailure(Match.Case<? extends Throwable, ? extends Throwable>... cases) {
  12. if (isSuccess()) {
  13. return this;
  14. } else {
  15. final Option<Throwable> x = Match(getCause()).option(cases);
  16. return x.isEmpty() ? this : failure(x.get());
  17. }
  18. }

代码示例来源:origin: jdbi/jdbi

  1. @Test
  2. public void testGetSuccessTryArgumentShouldNotBeEmpty() {
  3. Optional<Argument> arg = unit.build(new GenericType<Try<Integer>>() {}.getType(),
  4. Try.failure(new TestSpecificException()), null);
  5. assertThat(arg).isNotEmpty();
  6. }

代码示例来源:origin: jdbi/jdbi

  1. @Test
  2. public void testGetFailedTryArgumentShouldNotBeEmpty() {
  3. Optional<Argument> arg = unit.build(new GenericType<Try<Integer>>() {}.getType(),
  4. Try.failure(new TestSpecificException()), null);
  5. assertThat(arg).isNotEmpty();
  6. }

相关文章