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

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

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

Try.success介绍

[英]Creates a Success that contains the given value. Shortcut for new Success<>(value).
[中]创建包含给定值的成功。新成功的捷径<>(价值)。

代码示例

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

  1. /**
  2. * Alias for {@link Try#success(Object)}
  3. *
  4. * @param <T> Type of the given {@code value}.
  5. * @param value A value.
  6. * @return A new {@link Try.Success}.
  7. */
  8. @SuppressWarnings("unchecked")
  9. public static <T> Try.Success<T> Success(T value) {
  10. return (Try.Success<T>) Try.success(value);
  11. }

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

  1. /**
  2. * Completes this {@code Promise} with the given {@code value}.
  3. *
  4. * @param value A value.
  5. * @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
  6. */
  7. default boolean trySuccess(T value) {
  8. return tryComplete(Try.success(value));
  9. }

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

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

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

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

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

  1. /**
  2. * Returns {@code this}, if this is a {@link Try.Success} or this is a {@code Failure} and the cause is not assignable
  3. * from {@code cause.getClass()}. Otherwise returns a {@link Try.Success} containing the given {@code value}.
  4. *
  5. * <pre>{@code
  6. * // = Success(13)
  7. * Try.of(() -> 27/2).recover(ArithmeticException.class, Integer.MAX_VALUE);
  8. *
  9. * // = Success(2147483647)
  10. * Try.of(() -> 1/0)
  11. * .recover(Error.class, -1);
  12. * .recover(ArithmeticException.class, Integer.MAX_VALUE);
  13. *
  14. * // = Failure(java.lang.ArithmeticException: / by zero)
  15. * Try.of(() -> 1/0).recover(Error.class, Integer.MAX_VALUE);
  16. * }</pre>
  17. *
  18. * @param <X> Exception type
  19. * @param exceptionType The specific exception type that should be handled
  20. * @param value A value that is used in case of a recovery
  21. * @return a {@code Try}
  22. * @throws NullPointerException if {@code exception} is null
  23. */
  24. @GwtIncompatible
  25. default <X extends Throwable> Try<T> recover(Class<X> exceptionType, T value) {
  26. Objects.requireNonNull(exceptionType, "exceptionType is null");
  27. return (isFailure() && exceptionType.isAssignableFrom(getCause().getClass()))
  28. ? Try.success(value)
  29. : this;
  30. }

代码示例来源: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. .onSuccess(value -> completed.set(complete.with(Try.success(Option.some(value)))))
  2. .onFailure(ignored -> {
  3. if (wasLast) {
  4. completed.set(complete.with(Try.success(Option.none())));

代码示例来源: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: resilience4j/resilience4j

  1. @Test
  2. public void decorateConsumer() throws Exception {
  3. Consumer<Integer> consumer = mock(Consumer.class);
  4. Consumer<Integer> decorated = RateLimiter.decorateConsumer(limit, consumer);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<Integer> decoratedConsumerResult = Try.success(1).andThen(decorated);
  8. then(decoratedConsumerResult.isFailure()).isTrue();
  9. then(decoratedConsumerResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(consumer, never()).accept(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondConsumerResult = Try.success(1).andThen(decorated);
  14. then(secondConsumerResult.isSuccess()).isTrue();
  15. verify(consumer, times(1)).accept(1);
  16. }

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

  1. @Test
  2. public void decorateFunction() throws Exception {
  3. Function<Integer, String> function = mock(Function.class);
  4. Function<Integer, String> decorated = RateLimiter.decorateFunction(limit, function);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<String> decoratedFunctionResult = Try.success(1).map(decorated);
  8. then(decoratedFunctionResult.isFailure()).isTrue();
  9. then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(function, never()).apply(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondFunctionResult = Try.success(1).map(decorated);
  14. then(secondFunctionResult.isSuccess()).isTrue();
  15. verify(function, times(1)).apply(1);
  16. }

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

  1. @Test
  2. public void decorateRunnable() throws Exception {
  3. Runnable runnable = mock(Runnable.class);
  4. Runnable decorated = RateLimiter.decorateRunnable(limit, runnable);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedRunnableResult = Try.success(decorated).andThen(Runnable::run);
  8. then(decoratedRunnableResult.isFailure()).isTrue();
  9. then(decoratedRunnableResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(runnable, never()).run();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondRunnableResult = Try.success(decorated).andThen(Runnable::run);
  14. then(secondRunnableResult.isSuccess()).isTrue();
  15. verify(runnable, times(1)).run();
  16. }

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

  1. @Test
  2. public void decorateSupplier() throws Exception {
  3. Supplier supplier = mock(Supplier.class);
  4. Supplier decorated = RateLimiter.decorateSupplier(limit, supplier);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedSupplierResult = Try.success(decorated).map(Supplier::get);
  8. then(decoratedSupplierResult.isFailure()).isTrue();
  9. then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(supplier, never()).get();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondSupplierResult = Try.success(decorated).map(Supplier::get);
  14. then(secondSupplierResult.isSuccess()).isTrue();
  15. verify(supplier, times(1)).get();
  16. }

代码示例来源: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: resilience4j/resilience4j

  1. @Test
  2. public void decorateCheckedFunction() throws Throwable {
  3. CheckedFunction1<Integer, String> function = mock(CheckedFunction1.class);
  4. CheckedFunction1<Integer, String> decorated = RateLimiter.decorateCheckedFunction(limit, function);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<String> decoratedFunctionResult = Try.success(1).mapTry(decorated);
  8. then(decoratedFunctionResult.isFailure()).isTrue();
  9. then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(function, never()).apply(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondFunctionResult = Try.success(1).mapTry(decorated);
  14. then(secondFunctionResult.isSuccess()).isTrue();
  15. verify(function, times(1)).apply(1);
  16. }

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

  1. @Test
  2. public void decorateFutureSupplier() throws Throwable {
  3. when(timeLimiter.getTimeLimiterConfig()).thenReturn(shortConfig);
  4. Future<Integer> future = EXECUTOR_SERVICE.submit(() -> {
  5. Thread.sleep(SLEEP_DURATION.toMillis());
  6. return 1;
  7. }
  8. );
  9. Supplier<Future<Integer>> supplier = () -> future;
  10. Callable<Integer> decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
  11. Try decoratedResult = Try.success(decorated).mapTry(Callable::call);
  12. then(decoratedResult.isFailure()).isTrue();
  13. then(decoratedResult.getCause()).isInstanceOf(TimeoutException.class);
  14. then(future.isCancelled()).isTrue();
  15. when(timeLimiter.getTimeLimiterConfig())
  16. .thenReturn(longConfig);
  17. Future<Integer> secondFuture = EXECUTOR_SERVICE.submit(() -> {
  18. Thread.sleep(SLEEP_DURATION.toMillis());
  19. return 1;
  20. }
  21. );
  22. supplier = () -> secondFuture;
  23. decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
  24. Try secondResult = Try.success(decorated).mapTry(Callable::call);
  25. then(secondResult.isSuccess()).isTrue();
  26. }

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

  1. @Test
  2. public void testGetTrySuccessShouldReturnCorrectRow() {
  3. Something result = dbRule.getSharedHandle().createQuery(SELECT_BY_NAME)
  4. .bind("name", Try.success("brian"))
  5. .mapToBean(Something.class)
  6. .findOnly();
  7. assertThat(result).isEqualTo(BRIANSOMETHING);
  8. }

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

  1. /**
  2. * Alias for {@link Try#success(Object)}
  3. *
  4. * @param <T> Type of the given {@code value}.
  5. * @param value A value.
  6. * @return A new {@link Try.Success}.
  7. */
  8. @SuppressWarnings("unchecked")
  9. public static <T> Try.Success<T> Success(T value) {
  10. return (Try.Success<T>) Try.success(value);
  11. }

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

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

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

  1. /**
  2. * Completes this {@code Promise} with the given {@code value}.
  3. *
  4. * @param value A value.
  5. * @return {@code false} if this {@code Promise} has already been completed, {@code true} otherwise.
  6. */
  7. default boolean trySuccess(T value) {
  8. return tryComplete(Try.success(value));
  9. }

代码示例来源:origin: martincooper/java-datatable

  1. /**
  2. * Returns the IDataColumn at the specified index.
  3. * Performs bounds check, returns results in a Try.
  4. *
  5. * @param index The index to return the IDataColumn.
  6. * @return Returns the IDataColumn.
  7. */
  8. public Try<IDataColumn> tryGet(int index) {
  9. return VectorExtensions.outOfBounds(this.columns, index)
  10. ? DataTableException.tryError("Column index out of bounds")
  11. : Try.success(get(index));
  12. }

相关文章