All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pivovarit.collectors.CompletionStrategy Maven / Gradle / Ivy

The newest version!
package com.pivovarit.collectors;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

sealed interface CompletionStrategy extends Function>, Stream> permits CompletionStrategy.Unordered, CompletionStrategy.Ordered {

    Unordered UNORDERED = new Unordered<>();
    Ordered ORDERED = new Ordered<>();

    static  CompletionStrategy unordered() {
        return (CompletionStrategy) UNORDERED;
    }

    static  CompletionStrategy ordered() {
        return (CompletionStrategy) ORDERED;
    }

    final class Unordered implements CompletionStrategy {
        @Override
        public Stream apply(List> futures) {
            return StreamSupport.stream(new CompletionOrderSpliterator<>(futures), false);
        }
    }

    final class Ordered implements CompletionStrategy {
        @Override
        public Stream apply(List> futures) {
            return futures.stream().map(CompletableFuture::join);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy