本文整理了Java中io.vavr.control.Try.get()
方法的一些代码示例,展示了Try.get()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.get()
方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:get
[英]Gets the result of this Try if this is a Success or throws if this is a Failure.
IMPORTANT! If this is a Failure, the underlying cause of type Throwable is thrown.
The thrown exception is exactly the same as the result of #getCause().
[中]如果成功,则获取此尝试的结果;如果失败,则抛出。
重要的如果这是一个失败,则抛出类型Throwable的根本原因。
抛出的异常与#getCause()的结果完全相同。
代码示例来源:origin: vavr-io/vavr
default T getOrElseGet(Function<? super Throwable, ? extends T> other) {
Objects.requireNonNull(other, "other is null");
if (isFailure()) {
return other.apply(getCause());
} else {
return get();
}
}
代码示例来源:origin: vavr-io/vavr
/**
* Returns the underlying value if present, otherwise returns the result of {@code Try.of(supplier).get()}.
*
* @param supplier An alternative value supplier.
* @return A value of type {@code T}.
* @throws NullPointerException if supplier is null
*/
default T getOrElseTry(CheckedFunction0<? extends T> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
return isEmpty() ? Try.of(supplier).get() : get();
}
代码示例来源:origin: vavr-io/vavr
/**
* Gets the value if the computation result is a {@code Success} or throws if it was a {@code Failure}.
* Waits for the result if necessary by blocking the current thread.
* <p>
* <strong>IMPORTANT! If the computation result is a {@link Try.Failure}, the underlying {@code cause} of type {@link Throwable} is thrown.</strong>
*
* @return The value of this {@code Future}.
*/
@Override
default T get() {
return await().getValue().get().get();
}
代码示例来源:origin: vavr-io/vavr
default <X extends Throwable> T getOrElseThrow(Function<? super Throwable, X> exceptionProvider) throws X {
Objects.requireNonNull(exceptionProvider, "exceptionProvider is null");
if (isFailure()) {
throw exceptionProvider.apply(getCause());
} else {
return get();
}
}
代码示例来源:origin: vavr-io/vavr
@Override
default Iterator<T> iterator() {
return isSuccess() ? Iterator.of(get()) : Iterator.empty();
}
代码示例来源:origin: vavr-io/vavr
/**
* Converts this {@code Try} to an {@link Either}.
*
* @return A new {@code Either}
*/
default Either<Throwable, T> toEither() {
if (isFailure()) {
return Either.left(getCause());
} else {
return Either.right(get());
}
}
代码示例来源:origin: resilience4j/resilience4j
private V computeAndPut(K cacheKey, CheckedFunction0<V> supplier) {
return Try.of(supplier)
.andThen(value -> putValueIntoCache(cacheKey, value))
.get();
}
代码示例来源:origin: vavr-io/vavr
/**
* Creates a {@code Validation} of an {@code Try}.
*
* @param t A {@code Try}
* @param <T> type of the valid value
* @return A {@code Valid(t.get())} if t is a Success, otherwise {@code Invalid(t.getCause())}.
* @throws NullPointerException if {@code t} is null
*/
static <T> Validation<Throwable, T> fromTry(Try<? extends T> t) {
Objects.requireNonNull(t, "t is null");
return t.isSuccess() ? valid(t.get()) : invalid(t.getCause());
}
代码示例来源:origin: resilience4j/resilience4j
@Override
public Response<T> execute() throws IOException {
CheckedFunction0<Response<T>> restrictedSupplier = RateLimiter.decorateCheckedSupplier(rateLimiter, call::execute);
final Try<Response<T>> response = Try.of(restrictedSupplier);
return response.isSuccess() ? response.get() : handleFailure(response);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateConsumerAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
// When
Consumer<String> consumer = Bulkhead.decorateConsumer(bulkhead, (value) -> {throw new RuntimeException("BAM!");});
Try<Void> result = Try.run(() -> consumer.accept("Tom"));
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateCheckedRunnableAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
// When
CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
Try<Void> result = Try.run(checkedRunnable);
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateRunnableAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
// When
Runnable runnable = Bulkhead.decorateRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
Try<Void> result = Try.run(runnable::run);
//Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateCheckedConsumerAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
// When
CheckedConsumer<String> checkedConsumer = Bulkhead.decorateCheckedConsumer(bulkhead, (value) -> {
throw new RuntimeException("BAM!");
});
Try<Void> result = Try.run(() -> checkedConsumer.accept("Tom"));
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateFunctionAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
BDDMockito.given(helloWorldService.returnHelloWorldWithName("Tom")).willThrow(new RuntimeException("BAM!"));
// When
Function<String, String> function = Bulkhead.decorateFunction(bulkhead, helloWorldService::returnHelloWorldWithName);
Try<String> result = Try.of(() -> function.apply("Tom"));
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateSupplierAndReturnWithException() throws Throwable {
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
// And measure the call with a Timer
Supplier<String> supplier = Timer.decorateSupplier(timer, helloWorldService::returnHelloWorld);
Try<String> result = Try.of(supplier::get);
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(timer.getMetrics().getNumberOfTotalCalls()).isEqualTo(1);
assertThat(timer.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0);
assertThat(timer.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
// Then the helloWorldService should be invoked 1 time
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateCheckedFunctionAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
BDDMockito.given(helloWorldService.returnHelloWorldWithNameWithException("Tom")).willThrow(new RuntimeException("BAM!"));
// When
CheckedFunction1<String, String> function = Bulkhead.decorateCheckedFunction(bulkhead, helloWorldService::returnHelloWorldWithNameWithException);
Try<String> result = Try.of(() -> function.apply("Tom"));
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateSupplierAndReturnWithException() {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
// When
Supplier<String> supplier = Bulkhead.decorateSupplier(bulkhead, helloWorldService::returnHelloWorld);
Try<String> result = Try.of(supplier::get);
//Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateCallableAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
// When
Callable<String> callable = Bulkhead.decorateCallable(bulkhead, helloWorldService::returnHelloWorldWithException);
Try<String> result = Try.of(callable::call);
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException();
}
代码示例来源:origin: resilience4j/resilience4j
@Test
public void shouldDecorateCheckedSupplierAndReturnWithException() throws Throwable {
// Given
Bulkhead bulkhead = Bulkhead.of("test", config);
BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
// When
CheckedFunction0<String> checkedSupplier = Bulkhead.decorateCheckedSupplier(bulkhead, helloWorldService::returnHelloWorldWithException);
Try<String> result = Try.of(checkedSupplier);
// Then
assertThat(result.isFailure()).isTrue();
assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException();
}
代码示例来源:origin: resilience4j/resilience4j
@SuppressWarnings("unchecked")
@Test
public void testDecorateCheckedSupplierWithCache() {
javax.cache.Cache<String, String> cache = mock(javax.cache.Cache.class);
// Given the cache contains the key
given(cache.containsKey("testKey")).willReturn(true);
// Return the value from cache
given(cache.get("testKey")).willReturn("Hello from cache");
CheckedFunction1<String, String> cachedFunction = Decorators.ofCheckedSupplier(() -> "Hello world")
.withCache(Cache.of(cache))
.decorate();
String value = Try.of(() -> cachedFunction.apply("testKey")).get();
assertThat(value).isEqualTo("Hello from cache");
}
内容来源于网络,如有侵权,请联系作者删除!