Skip to content

Commit

Permalink
Seal CompletionStrategy interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit committed Sep 20, 2023
1 parent be95e4f commit f47dd75
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/com/pivovarit/collectors/CompletionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

interface CompletionStrategy<T> extends Function<List<CompletableFuture<T>>, Stream<T>> {
sealed interface CompletionStrategy<T>
extends Function<List<CompletableFuture<T>>, Stream<T>>
permits CompletionStrategy.Unordered, CompletionStrategy.Ordered {

static <R> CompletionStrategy<R> unordered() {
return futures -> StreamSupport.stream(new CompletionOrderSpliterator<>(futures), false);
return new Unordered<>();
}

static <R> CompletionStrategy<R> ordered() {
return futures -> futures.stream().map(CompletableFuture::join);
return new Ordered<>();
}

final class Unordered<R> implements CompletionStrategy<R> {

@Override
public Stream<R> apply(List<CompletableFuture<R>> futures) {
return StreamSupport.stream(new CompletionOrderSpliterator<>(futures), false);
}
}

final class Ordered<R> implements CompletionStrategy<R> {

@Override
public Stream<R> apply(List<CompletableFuture<R>> futures) {
return futures.stream().map(CompletableFuture::join);
}
}
}

0 comments on commit f47dd75

Please sign in to comment.