Search in sources :

Example 1 with List

use of javaslang.collection.List in project javaslang by javaslang.

the class Future method find.

/**
     * Returns a {@code Future} that eventually succeeds with the first result of the given {@code Future}s which
     * matches the given {@code predicate}. If no result matches, the {@code Future} will contain {@link Option.None}.
     * <p>
     * The returned {@code Future} is backed by the given {@link ExecutorService}.
     *
     * @param executorService An executor service.
     * @param futures         An iterable of futures.
     * @param predicate       A predicate that tests successful future results.
     * @param <T>             Result type of the futures.
     * @return A Future of an {@link Option} of the first result of the given {@code futures} that satisfies the given {@code predicate}.
     * @throws NullPointerException if one of the arguments is null
     */
static <T> Future<Option<T>> find(ExecutorService executorService, Iterable<? extends Future<? extends T>> futures, Predicate<? super T> predicate) {
    Objects.requireNonNull(executorService, "executorService is null");
    Objects.requireNonNull(futures, "futures is null");
    Objects.requireNonNull(predicate, "predicate is null");
    final Promise<Option<T>> promise = Promise.make(executorService);
    final List<Future<? extends T>> list = List.ofAll(futures);
    if (list.isEmpty()) {
        promise.success(Option.none());
    } else {
        final AtomicInteger count = new AtomicInteger(list.length());
        list.forEach(future -> future.onComplete(result -> {
            synchronized (count) {
                if (!promise.isCompleted()) {
                    final boolean wasLast = count.decrementAndGet() == 0;
                    result.filter(predicate).onSuccess(value -> promise.trySuccess(Option.some(value))).onFailure(ignored -> {
                        if (wasLast) {
                            promise.trySuccess(Option.none());
                        }
                    });
                }
            }
        }));
    }
    return promise.future();
}
Also used : Option(javaslang.control.Option) List(javaslang.collection.List) CheckedSupplier(javaslang.control.Try.CheckedSupplier) Seq(javaslang.collection.Seq) CheckedPredicate(javaslang.control.Try.CheckedPredicate) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Value(javaslang.Value) CheckedRunnable(javaslang.control.Try.CheckedRunnable) Executors(java.util.concurrent.Executors) Tuple(javaslang.Tuple) Try(javaslang.control.Try) Objects(java.util.Objects) Iterator(javaslang.collection.Iterator) CheckedFunction(javaslang.control.Try.CheckedFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stream(javaslang.collection.Stream) Tuple2(javaslang.Tuple2) java.util.function(java.util.function) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) Option(javaslang.control.Option)

Example 2 with List

use of javaslang.collection.List in project javaslang by javaslang.

the class Euler43Test method tenDigitPandigitalsWithProperty.

private static Seq<Long> tenDigitPandigitalsWithProperty() {
    final CharSeq ALL_DIGITS = CharSeq.of("0123456789");
    final List<Integer> DIVISORS = List.of(2, 3, 5, 7, 11, 13, 17);
    return ALL_DIGITS.combinations(2).flatMap(CharSeq::permutations).flatMap(firstTwoDigits -> DIVISORS.foldLeft(List.of(firstTwoDigits), (accumulator, divisor) -> accumulator.flatMap(digitsSoFar -> ALL_DIGITS.removeAll(digitsSoFar).map(nextDigit -> digitsSoFar.append(nextDigit))).filter(digitsToTest -> digitsToTest.takeRight(3).parseInt() % divisor == 0))).map(tailDigitsWithProperty -> tailDigitsWithProperty.prepend(ALL_DIGITS.removeAll(tailDigitsWithProperty).head())).map(CharSeq::parseLong);
}
Also used : CharSeq(javaslang.collection.CharSeq) List(javaslang.collection.List) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Seq(javaslang.collection.Seq) Test(org.junit.Test) CharSeq(javaslang.collection.CharSeq)

Aggregations

List (javaslang.collection.List)2 Seq (javaslang.collection.Seq)2 NoSuchElementException (java.util.NoSuchElementException)1 Objects (java.util.Objects)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 java.util.function (java.util.function)1 Tuple (javaslang.Tuple)1 Tuple2 (javaslang.Tuple2)1 Value (javaslang.Value)1 CharSeq (javaslang.collection.CharSeq)1 Iterator (javaslang.collection.Iterator)1 Stream (javaslang.collection.Stream)1 Option (javaslang.control.Option)1 Try (javaslang.control.Try)1 CheckedFunction (javaslang.control.Try.CheckedFunction)1 CheckedPredicate (javaslang.control.Try.CheckedPredicate)1