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

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

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

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

  1. default T getOrElseGet(Function<? super Throwable, ? extends T> other) {
  2. Objects.requireNonNull(other, "other is null");
  3. if (isFailure()) {
  4. return other.apply(getCause());
  5. } else {
  6. return get();
  7. }
  8. }

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

  1. /**
  2. * Returns the underlying value if present, otherwise returns the result of {@code Try.of(supplier).get()}.
  3. *
  4. * @param supplier An alternative value supplier.
  5. * @return A value of type {@code T}.
  6. * @throws NullPointerException if supplier is null
  7. */
  8. default T getOrElseTry(CheckedFunction0<? extends T> supplier) {
  9. Objects.requireNonNull(supplier, "supplier is null");
  10. return isEmpty() ? Try.of(supplier).get() : get();
  11. }

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

  1. /**
  2. * Gets the value if the computation result is a {@code Success} or throws if it was a {@code Failure}.
  3. * Waits for the result if necessary by blocking the current thread.
  4. * <p>
  5. * <strong>IMPORTANT! If the computation result is a {@link Try.Failure}, the underlying {@code cause} of type {@link Throwable} is thrown.</strong>
  6. *
  7. * @return The value of this {@code Future}.
  8. */
  9. @Override
  10. default T get() {
  11. return await().getValue().get().get();
  12. }

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

  1. default <X extends Throwable> T getOrElseThrow(Function<? super Throwable, X> exceptionProvider) throws X {
  2. Objects.requireNonNull(exceptionProvider, "exceptionProvider is null");
  3. if (isFailure()) {
  4. throw exceptionProvider.apply(getCause());
  5. } else {
  6. return get();
  7. }
  8. }

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

  1. @Override
  2. default Iterator<T> iterator() {
  3. return isSuccess() ? Iterator.of(get()) : Iterator.empty();
  4. }

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

  1. /**
  2. * Converts this {@code Try} to an {@link Either}.
  3. *
  4. * @return A new {@code Either}
  5. */
  6. default Either<Throwable, T> toEither() {
  7. if (isFailure()) {
  8. return Either.left(getCause());
  9. } else {
  10. return Either.right(get());
  11. }
  12. }

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

  1. private V computeAndPut(K cacheKey, CheckedFunction0<V> supplier) {
  2. return Try.of(supplier)
  3. .andThen(value -> putValueIntoCache(cacheKey, value))
  4. .get();
  5. }

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

  1. /**
  2. * Creates a {@code Validation} of an {@code Try}.
  3. *
  4. * @param t A {@code Try}
  5. * @param <T> type of the valid value
  6. * @return A {@code Valid(t.get())} if t is a Success, otherwise {@code Invalid(t.getCause())}.
  7. * @throws NullPointerException if {@code t} is null
  8. */
  9. static <T> Validation<Throwable, T> fromTry(Try<? extends T> t) {
  10. Objects.requireNonNull(t, "t is null");
  11. return t.isSuccess() ? valid(t.get()) : invalid(t.getCause());
  12. }

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

  1. @Override
  2. public Response<T> execute() throws IOException {
  3. CheckedFunction0<Response<T>> restrictedSupplier = RateLimiter.decorateCheckedSupplier(rateLimiter, call::execute);
  4. final Try<Response<T>> response = Try.of(restrictedSupplier);
  5. return response.isSuccess() ? response.get() : handleFailure(response);
  6. }

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

  1. @Test
  2. public void shouldDecorateConsumerAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. Consumer<String> consumer = Bulkhead.decorateConsumer(bulkhead, (value) -> {throw new RuntimeException("BAM!");});
  7. Try<Void> result = Try.run(() -> consumer.accept("Tom"));
  8. // Then
  9. assertThat(result.isFailure()).isTrue();
  10. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  11. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  12. }

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

  1. @Test
  2. public void shouldDecorateCheckedRunnableAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. CheckedRunnable checkedRunnable = Bulkhead.decorateCheckedRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
  7. Try<Void> result = Try.run(checkedRunnable);
  8. // Then
  9. assertThat(result.isFailure()).isTrue();
  10. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  11. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  12. }

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

  1. @Test
  2. public void shouldDecorateRunnableAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. Runnable runnable = Bulkhead.decorateRunnable(bulkhead, () -> {throw new RuntimeException("BAM!");});
  7. Try<Void> result = Try.run(runnable::run);
  8. //Then
  9. assertThat(result.isFailure()).isTrue();
  10. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  11. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  12. }

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

  1. @Test
  2. public void shouldDecorateCheckedConsumerAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. // When
  6. CheckedConsumer<String> checkedConsumer = Bulkhead.decorateCheckedConsumer(bulkhead, (value) -> {
  7. throw new RuntimeException("BAM!");
  8. });
  9. Try<Void> result = Try.run(() -> checkedConsumer.accept("Tom"));
  10. // Then
  11. assertThat(result.isFailure()).isTrue();
  12. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  13. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  14. }

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

  1. @Test
  2. public void shouldDecorateFunctionAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithName("Tom")).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. Function<String, String> function = Bulkhead.decorateFunction(bulkhead, helloWorldService::returnHelloWorldWithName);
  8. Try<String> result = Try.of(() -> function.apply("Tom"));
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. }

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

  1. @Test
  2. public void shouldDecorateSupplierAndReturnWithException() throws Throwable {
  3. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  4. // And measure the call with a Timer
  5. Supplier<String> supplier = Timer.decorateSupplier(timer, helloWorldService::returnHelloWorld);
  6. Try<String> result = Try.of(supplier::get);
  7. assertThat(result.isFailure()).isTrue();
  8. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  9. assertThat(timer.getMetrics().getNumberOfTotalCalls()).isEqualTo(1);
  10. assertThat(timer.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(0);
  11. assertThat(timer.getMetrics().getNumberOfFailedCalls()).isEqualTo(1);
  12. // Then the helloWorldService should be invoked 1 time
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
  14. }

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

  1. @Test
  2. public void shouldDecorateCheckedFunctionAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithNameWithException("Tom")).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. CheckedFunction1<String, String> function = Bulkhead.decorateCheckedFunction(bulkhead, helloWorldService::returnHelloWorldWithNameWithException);
  8. Try<String> result = Try.of(() -> function.apply("Tom"));
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. }

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

  1. @Test
  2. public void shouldDecorateSupplierAndReturnWithException() {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. Supplier<String> supplier = Bulkhead.decorateSupplier(bulkhead, helloWorldService::returnHelloWorld);
  8. Try<String> result = Try.of(supplier::get);
  9. //Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
  14. }

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

  1. @Test
  2. public void shouldDecorateCallableAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. Callable<String> callable = Bulkhead.decorateCallable(bulkhead, helloWorldService::returnHelloWorldWithException);
  8. Try<String> result = Try.of(callable::call);
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException();
  14. }

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

  1. @Test
  2. public void shouldDecorateCheckedSupplierAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. CheckedFunction0<String> checkedSupplier = Bulkhead.decorateCheckedSupplier(bulkhead, helloWorldService::returnHelloWorldWithException);
  8. Try<String> result = Try.of(checkedSupplier);
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException();
  14. }

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

  1. @SuppressWarnings("unchecked")
  2. @Test
  3. public void testDecorateCheckedSupplierWithCache() {
  4. javax.cache.Cache<String, String> cache = mock(javax.cache.Cache.class);
  5. // Given the cache contains the key
  6. given(cache.containsKey("testKey")).willReturn(true);
  7. // Return the value from cache
  8. given(cache.get("testKey")).willReturn("Hello from cache");
  9. CheckedFunction1<String, String> cachedFunction = Decorators.ofCheckedSupplier(() -> "Hello world")
  10. .withCache(Cache.of(cache))
  11. .decorate();
  12. String value = Try.of(() -> cachedFunction.apply("testKey")).get();
  13. assertThat(value).isEqualTo("Hello from cache");
  14. }

相关文章