本文整理了Java中io.vavr.control.Try.failure()
方法的一些代码示例,展示了Try.failure()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.failure()
方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:failure
[英]Creates a Failure that contains the given exception. Shortcut for new Failure<>(exception).
[中]创建包含给定异常的故障。新故障的快捷方式<>(例外)。
代码示例来源:origin: vavr-io/vavr
/**
* Alias for {@link Try#failure(Throwable)}
*
* @param <T> Component type of the {@code Try}.
* @param exception An exception.
* @return A new {@link Try.Failure}.
*/
@SuppressWarnings("unchecked")
public static <T> Try.Failure<T> Failure(Throwable exception) {
return (Try.Failure<T>) Try.failure(exception);
}
代码示例来源:origin: vavr-io/vavr
/**
* Completes this {@code Promise} with the given {@code exception}.
*
* @param exception An exception.
* @return This {@code Promise}.
* @throws IllegalStateException if this {@code Promise} has already been completed.
*/
default Promise<T> failure(Throwable exception) {
return complete(Try.failure(exception));
}
代码示例来源:origin: vavr-io/vavr
/**
* Completes this {@code Promise} with the given {@code exception}.
*
* @param exception An exception.
* @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
*/
default boolean tryFailure(Throwable exception) {
return tryComplete(Try.failure(exception));
}
}
代码示例来源:origin: vavr-io/vavr
private void handleUncaughtException(Throwable x) {
tryComplete(Try.failure(x));
}
代码示例来源:origin: vavr-io/vavr
/**
* Creates a failed {@code Future} with the given {@code exception}, backed by the given {@link Executor}.
*
* @param executor An {@link Executor}.
* @param exception The reason why it failed.
* @param <T> The value type of a successful result.
* @return A failed {@code Future}.
* @throws NullPointerException if executor or exception is null
*/
static <T> Future<T> failed(Executor executor, Throwable exception) {
Objects.requireNonNull(executor, "executor is null");
Objects.requireNonNull(exception, "exception is null");
return FutureImpl.of(executor, Try.failure(exception));
}
代码示例来源:origin: vavr-io/vavr
/**
* Converts this to a {@link Try}.
* <p>
* If this value is undefined, i.e. empty, then a new {@code Failure(ifEmpty.get())} is returned,
* otherwise a new {@code Success(value)} is returned.
*
* @param ifEmpty an exception supplier
* @return A new {@link Try}.
*/
default Try<T> toTry(Supplier<? extends Throwable> ifEmpty) {
Objects.requireNonNull(ifEmpty, "ifEmpty is null");
return isEmpty() ? Try.failure(ifEmpty.get()) : toTry();
}
代码示例来源:origin: vavr-io/vavr
private FutureImpl(Executor executor, Option<Try<T>> value, Queue<Consumer<Try<T>>> actions, Queue<Thread> waiters, Computation<T> computation) {
this.executor = executor;
synchronized (lock) {
this.cancelled = false;
this.value = value;
this.actions = actions;
this.waiters = waiters;
try {
computation.execute(this::tryComplete, this::updateThread);
} catch (Throwable x) {
tryComplete(Try.failure(x));
}
}
}
代码示例来源:origin: vavr-io/vavr
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
if (!isCompleted()) {
synchronized (lock) {
if (!isCompleted()) {
if (mayInterruptIfRunning && this.thread != null) {
this.thread.interrupt();
}
this.cancelled = tryComplete(Try.failure(new CancellationException()));
return this.cancelled;
}
}
}
return false;
}
代码示例来源:origin: vavr-io/vavr
/**
* Returns {@code this} if this is a Failure or this is a Success and the value satisfies the predicate.
* <p>
* Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception
* occurs testing the predicate. The returned Failure wraps a Throwable instance provided by the given
* {@code errorProvider}.
*
* @param predicate A checked predicate
* @param errorProvider A provider of a throwable
* @return a {@code Try} instance
* @throws NullPointerException if {@code predicate} or {@code errorProvider} is null
*/
default Try<T> filterTry(CheckedPredicate<? super T> predicate, CheckedFunction1<? super T, ? extends Throwable> errorProvider) {
Objects.requireNonNull(predicate, "predicate is null");
Objects.requireNonNull(errorProvider, "errorProvider is null");
return flatMapTry(t -> predicate.test(t) ? this : failure(errorProvider.apply(t)));
}
代码示例来源:origin: vavr-io/vavr
/**
* Creates a {@code Future} with the given {@link java.util.concurrent.CompletableFuture}, backed by given {@link Executor}
*
* @param executor An {@link Executor}.
* @param future A {@link java.util.concurrent.CompletableFuture}.
* @param <T> Result type of the Future
* @return A new {@code Future} wrapping the result of the {@link java.util.concurrent.CompletableFuture}
* @throws NullPointerException if executor or future is null
*/
@GwtIncompatible
static <T> Future<T> fromCompletableFuture(Executor executor, CompletableFuture<T> future) {
Objects.requireNonNull(executor, "executor is null");
Objects.requireNonNull(future, "future is null");
if (future.isDone() || future.isCompletedExceptionally() || future.isCancelled()) {
return fromTry(Try.of(future::get).recoverWith(error -> Try.failure(error.getCause())));
} else {
return run(executor, complete ->
future.handle((t, err) -> complete.with((err == null) ? Try.success(t) : Try.failure(err)))
);
}
}
代码示例来源:origin: vavr-io/vavr
/**
* Transforms the value of this {@code Future}, whether it is a success or a failure.
*
* @param f A transformation
* @param <U> Generic type of transformation {@code Try} result
* @return A {@code Future} of type {@code U}
* @throws NullPointerException if {@code f} is null
*/
default <U> Future<U> transformValue(Function<? super Try<T>, ? extends Try<? extends U>> f) {
Objects.requireNonNull(f, "f is null");
return run(executor(), complete ->
onComplete(t -> Try.run(() -> complete.with(f.apply(t)))
.onFailure(x -> complete.with(Try.failure(x)))
)
);
}
代码示例来源:origin: vavr-io/vavr
/**
* Creates a {@code FutureImpl} that is eventually completed.
* The given {@code computation} is <em>asynchronously</em> executed, a new thread is started.
*
* @param executor An {@link Executor} to run and control the computation and to perform the actions.
* @param task A (possibly blocking) computation
* @param <T> value type of the Future
* @return a new {@code FutureImpl} instance
*/
static <T> FutureImpl<T> async(Executor executor, Task<? extends T> task) {
// In a single-threaded context this Future may already have been completed during initialization.
return new FutureImpl<>(executor, Option.none(), Queue.empty(), Queue.empty(), (complete, updateThread) ->
executor.execute(() -> {
updateThread.run();
try {
task.run(complete::with);
} catch (Throwable x) {
complete.with(Try.failure(x));
}
})
);
}
代码示例来源:origin: vavr-io/vavr
default <U> Future<U> flatMapTry(CheckedFunction1<? super T, ? extends Future<? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return run(executor(), complete ->
onComplete(result -> result.mapTry(mapper)
.onSuccess(future -> future.onComplete(complete::with))
.onFailure(x -> complete.with(Try.failure(x)))
)
);
}
代码示例来源:origin: vavr-io/vavr
/**
* Reduces many {@code Try}s into a single {@code Try} by transforming an
* {@code Iterable<Try<? extends T>>} into a {@code Try<Seq<T>>}. If any of
* the {@code Try}s are {@link Try.Failure}, then this returns a {@link Try.Failure}.
*
* @param values An {@link Iterable} of {@code Try}s
* @param <T> type of the Trys
* @return A {@code Try} of a {@link Seq} of results
* @throws NullPointerException if {@code values} is null
*/
static <T> Try<Seq<T>> sequence(Iterable<? extends Try<? extends T>> values) {
Objects.requireNonNull(values, "values is null");
Vector<T> vector = Vector.empty();
for (Try<? extends T> value : values) {
if (value.isFailure()) {
return Try.failure(value.getCause());
}
vector = vector.append(value.get());
}
return Try.success(vector);
}
代码示例来源:origin: vavr-io/vavr
/**
* A projection that inverses the result of this Future.
* <p>
* If this Future succeeds, the failed projection returns a failure containing a {@code NoSuchElementException}.
* <p>
* If this Future fails, the failed projection returns a success containing the exception.
*
* @return A new Future which contains an exception at a point of time.
*/
default Future<Throwable> failed() {
return run(executor(), complete ->
onComplete(result -> {
if (result.isFailure()) {
complete.with(Try.success(result.getCause()));
} else {
complete.with(Try.failure(new NoSuchElementException("Future.failed completed without a throwable")));
}
})
);
}
代码示例来源:origin: vavr-io/vavr
/**
* Handles a failure of this Future by returning the result of another Future.
* <p>
* Example:
* <pre><code>
* // = "oh!"
* Future.of(() -> { throw new Error("oh!"); }).recoverWith(x -> Future.of(x::getMessage));
* </code></pre>
*
* @param f A function which takes the exception of a failure and returns a new future.
* @return A new Future.
* @throws NullPointerException if {@code f} is null
*/
default Future<T> recoverWith(Function<? super Throwable, ? extends Future<? extends T>> f) {
Objects.requireNonNull(f, "f is null");
return run(executor(), complete ->
onComplete(t -> {
if (t.isFailure()) {
Try.run(() -> f.apply(t.getCause()).onComplete(complete::with))
.onFailure(x -> complete.with(Try.failure(x)));
} else {
complete.with(t);
}
})
);
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testGetTryFailureShouldReturnAllRows() {
List<Something> result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
.bind("name", Try.failure(new Throwable()))
.mapToBean(Something.class)
.list();
assertThat(result).hasSize(2);
}
代码示例来源:origin: vavr-io/vavr
/**
* Maps the cause to a new exception if this is a {@code Failure} or returns this instance if this is a {@code Success}.
* <p>
* If none of the given cases matches the cause, the same {@code Failure} is returned.
*
* @param cases A not necessarily exhaustive sequence of cases that will be matched against a cause.
* @return A new {@code Try} if this is a {@code Failure}, otherwise this.
*/
@GwtIncompatible
@SuppressWarnings({ "unchecked", "varargs" })
default Try<T> mapFailure(Match.Case<? extends Throwable, ? extends Throwable>... cases) {
if (isSuccess()) {
return this;
} else {
final Option<Throwable> x = Match(getCause()).option(cases);
return x.isEmpty() ? this : failure(x.get());
}
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testGetSuccessTryArgumentShouldNotBeEmpty() {
Optional<Argument> arg = unit.build(new GenericType<Try<Integer>>() {}.getType(),
Try.failure(new TestSpecificException()), null);
assertThat(arg).isNotEmpty();
}
代码示例来源:origin: jdbi/jdbi
@Test
public void testGetFailedTryArgumentShouldNotBeEmpty() {
Optional<Argument> arg = unit.build(new GenericType<Try<Integer>>() {}.getType(),
Try.failure(new TestSpecificException()), null);
assertThat(arg).isNotEmpty();
}
内容来源于网络,如有侵权,请联系作者删除!