Java Code Examples for io.vavr.control.Try#failure()

The following examples show how to use io.vavr.control.Try#failure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringMarshallableProtocol.java    From ts-reaktive with MIT License 6 votes vote down vote up
public static <E,T> Reader<E,T> addLocationOnError(Reader<E,T> inner, Locator<E> locator) {
    return new Reader<E,T>() {
        @Override
        public Try<T> reset() {
            return inner.reset();
        }

        @Override
        public Try<T> apply(E event) {
            return addLocation(inner.apply(event), locator.getLocation(event));
        }
        
        private Try<T> addLocation(Try<T> t, String location) {
            if (t.isFailure() && t.failed().get() instanceof IllegalArgumentException) {
                return Try.failure(new IllegalArgumentException(t.failed().get().getMessage() + " at " + location, t.failed().get()));
            } else {
                return t;
            }
        }
    };
}
 
Example 2
Source File: CircuitBreaker.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a supplier which is decorated by a CircuitBreaker.
 *
 * @param circuitBreaker the CircuitBreaker
 * @param supplier       the original function
 * @param <T>            the type of results supplied by this supplier
 * @return a retryable function
 */
static <T> Supplier<Try<T>> decorateTrySupplier(CircuitBreaker circuitBreaker,
    Supplier<Try<T>> supplier) {
    return () -> {
        if (circuitBreaker.tryAcquirePermission()) {
            long start = System.nanoTime();
            Try<T> result = supplier.get();
            long durationInNanos = System.nanoTime() - start;
            if (result.isSuccess()) {
                circuitBreaker.onSuccess(durationInNanos, TimeUnit.NANOSECONDS);
                return result;
            } else {
                circuitBreaker
                    .onError(durationInNanos, TimeUnit.NANOSECONDS, result.getCause());
                return result;
            }
        } else {
            return Try.failure(
                CallNotPermittedException.createCallNotPermittedException(circuitBreaker));
        }
    };
}
 
Example 3
Source File: DataRow.java    From java-datatable with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of a particular row column as a specific type.
 * This method performs bounds checks and type checks. Any errors
 * will return a Try.failure.
 *
 * @param type The data type.
 * @param idx The index of the column.
 * @param <T> The value type.
 * @return Returns the value at the specified index.
 */
public <T> Try<T> tryGetAs(Class<T> type, Integer idx) {

    // Get the column as it's typed version.
    Try<DataColumn<T>> col = this.table.columns()
            .tryGet(idx)
            .flatMap(c -> c.asType(type));

    return col.isFailure()
            ? Try.failure(col.getCause())
            : Try.success(col.get().valueAt(this.rowIdx));
}
 
Example 4
Source File: DataRow.java    From java-datatable with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of a particular row column as a specific type.
 * This method performs bounds checks and type checks. Any errors
 * will return a Try.failure.
 *
 * @param type The data type.
 * @param colName The name of the column.
 * @param <T> The value type.
 * @return Returns the value at the specified index.
 */
public <T> Try<T> tryGetAs(Class<T> type, String colName) {

    // Get the column as it's typed version.
    Try<DataColumn<T>> col = this.table.columns()
            .tryGet(colName)
            .flatMap(c -> c.asType(type));

    return col.isFailure()
            ? Try.failure(col.getCause())
            : Try.success(col.get().valueAt(this.rowIdx));
}
 
Example 5
Source File: Bulkhead.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a supplier which is decorated by a bulkhead.
 *
 * @param bulkhead the bulkhead
 * @param supplier the original supplier
 * @param <T>      the type of results supplied by this supplier
 * @return a supplier which is decorated by a Bulkhead.
 */
static <T> Supplier<Try<T>> decorateTrySupplier(Bulkhead bulkhead, Supplier<Try<T>> supplier) {
    return () -> {
        if (bulkhead.tryAcquirePermission()) {
            try {
                return supplier.get();
            } finally {
                bulkhead.onComplete();
            }
        } else {
            return Try.failure(BulkheadFullException.createBulkheadFullException(bulkhead));
        }
    };
}
 
Example 6
Source File: RateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a supplier which is restricted by a RateLimiter.
 *
 * @param rateLimiter the RateLimiter
 * @param permits     number of permits that this call requires
 * @param supplier    the original supplier
 * @param <T>         the type of results supplied supplier
 * @return a supplier which is restricted by a RateLimiter.
 */
static <T> Supplier<Try<T>> decorateTrySupplier(RateLimiter rateLimiter, int permits,
    Supplier<Try<T>> supplier) {
    return () -> {
        try {
            waitForPermission(rateLimiter, permits);
            return supplier.get();
        } catch (RequestNotPermitted requestNotPermitted) {
            return Try.failure(requestNotPermitted);
        }
    };
}
 
Example 7
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public Try<String> doSomethingErroneous() {
	assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
	return Try.failure(new IllegalStateException());
}
 
Example 8
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public Try<String> doSomethingErroneousWithCheckedException() {
	assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
	return Try.failure(new Exception());
}
 
Example 9
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Transactional(rollbackFor = Exception.class)
public Try<String> doSomethingErroneousWithCheckedExceptionAndRollbackRule() {
	assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
	return Try.failure(new Exception());
}
 
Example 10
Source File: DataRow.java    From java-datatable with Apache License 2.0 4 votes vote down vote up
private Try<Object> columnToValue(Try<IDataColumn> column) {
    return column.isSuccess()
            ? Try.success(column.get().valueAt(this.rowIdx))
            : Try.failure(column.getCause());
}
 
Example 11
Source File: GenericExtensions.java    From java-datatable with Apache License 2.0 3 votes vote down vote up
/**
 * Used to perform a type check and cast on an object instance.
 * Returns a Try success or failure depending on if it was valid cast.
 *
 * @param <T> The type to return.
 * @param type The type to cast to.
 * @param obj The object to cast.
 * @return Returns a Success<T> or a Failure.
 */
@SuppressWarnings({"unchecked"})
static <T> Try<T> tryCast(Class<T> type, Object obj) {
    return type.isInstance(obj)
            ? Try.success((T)obj)
            : Try.failure(new DataTableException("Invalid cast."));
}
 
Example 12
Source File: Guard.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Asserts the argument is not null. Returns in a Try.
 *
 * @param <T> The argument type.
 * @param argument The argument to check.
 * @param name The name of the argument.
 * @return Returns a Try testing the argument being null.
 */
public static <T> Try<T> tryNotNull(T argument, String name) {
    return argument == null
            ? Try.failure(new IllegalArgumentException("Invalid value [NULL] for argument " + name))
            : Try.success(argument);
}
 
Example 13
Source File: DataTableException.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a DataTableException wrapped in a Try.
 *
 * @param errorMessage The error message.
 * @param <T> The Try type.
 * @return Returns a new DataTable Exception in a try.
 */
public static <T> Try<T> tryError(String errorMessage) {
    return Try.failure(new DataTableException(errorMessage));
}
 
Example 14
Source File: DataTableException.java    From java-datatable with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a DataTableException wrapped in a Try.
 *
 * @param errorMessage The error message.
 * @param throwable The inner exception.
 * @param <T> The Try type.
 * @return Returns a new DataTable Exception in a try.
 */
public static <T> Try<T> tryError(String errorMessage, Throwable throwable) {
    return Try.failure(new DataTableException(errorMessage, throwable));
}