Refactoring

This commit is contained in:
Andrea Cavalli 2020-12-07 22:08:17 +01:00
parent e6f8fe1e2a
commit 279fad3431
2 changed files with 62 additions and 10 deletions

View File

@ -221,7 +221,14 @@ public class CompletableFutureUtils {
/**
* Creates a new empty collection of disaggregated future results future lists
*/
public static <T> Collection<CompletableFuture<FloatPriorityQueue<CompletableFuture<T>>>> createDisaggregatedResultsPq() {
public static <T> Collection<CompletableFuture<FloatPriorityQueue<CompletableFuture<T>>>> createDisaggregatedFutureResultsPq() {
return FloatPriorityQueue.synchronizedPq(10);
}
/**
* Creates a new empty collection of disaggregated results future lists
*/
public static <T> Collection<CompletableFuture<FloatPriorityQueue<T>>> createDisaggregatedResultsPq() {
return FloatPriorityQueue.synchronizedPq(10);
}
@ -246,7 +253,7 @@ public class CompletableFutureUtils {
addDisaggregatedPqCastForced(disaggregatedResults, result);
}
public static <T, U> void addDisaggregatedPqCastForced(
public static <T, U> void addDisaggregatedFuturePqCastForced(
Collection<CompletableFuture<FloatPriorityQueue<CompletableFuture<T>>>> disaggregatedResults,
CompletableFuture<FloatPriorityQueue<CompletableFuture<U>>> result) {
disaggregatedResults.add(result.thenApply((originalFloatPriorityQueue) -> {
@ -263,6 +270,19 @@ public class CompletableFutureUtils {
}));
}
public static <T, U> void addDisaggregatedPqCastForced(
Collection<CompletableFuture<FloatPriorityQueue<T>>> disaggregatedResults,
CompletableFuture<FloatPriorityQueue<U>> result) {
disaggregatedResults.add(result.thenApply((originalFloatPriorityQueue) -> {
FloatPriorityQueue<T> resultFloatPriorityQueue = new FloatPriorityQueue<>();
originalFloatPriorityQueue.forEachItem((originalFuture) -> {
//noinspection unchecked
resultFloatPriorityQueue.offer(ScoredValue.of(originalFuture.getScore(), (T) originalFuture.getValue()));
});
return resultFloatPriorityQueue;
}));
}
public static <T> Set<T> collectToSet(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
return futureList.join().parallelStream().map(CompletableFuture::join).collect(Collectors.toSet());
}
@ -279,16 +299,26 @@ public class CompletableFutureUtils {
return futureList.join().stream().map(CompletableFuture::join).limit(limit).collect(Collectors.toList());
}
public static <T> LinkedHashSet<T> collectToLinkedSet(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
public static <T> LinkedHashSet<T> collectToLinkedSetFuture(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
return futureList.join().stream().map(CompletableFuture::join).collect(Collectors.toCollection(LinkedHashSet::new));
}
public static <T> LinkedHashSet<T> collectToLinkedSet(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList,
public static <T> LinkedHashSet<T> collectToLinkedSetFuture(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList,
int limit) {
return futureList.join().stream().map(CompletableFuture::join).limit(limit)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
public static <T> LinkedHashSet<T> collectToLinkedSet(CompletableFuture<? extends Collection<T>> futureList) {
return new LinkedHashSet<>(futureList.join());
}
public static <T> LinkedHashSet<T> collectToLinkedSet(CompletableFuture<? extends Collection<T>> futureList,
int limit) {
return futureList.join().stream().limit(limit)
.collect(Collectors.toCollection(LinkedHashSet::new));
}
public static <T> FloatPriorityQueue<T> collectToPq(CompletableFuture<? extends FloatPriorityQueue<CompletableFuture<T>>> futureList) {
var internalPq = futureList.join().streamItems().map(t -> {
if (t.getValue() != null) {
@ -312,32 +342,54 @@ public class CompletableFutureUtils {
return new FloatPriorityQueue<>(internalPq);
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
public static <T> TreeSet<T> collectToTreeSetFuture(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
return futureList.join().stream().map(CompletableFuture::join).collect(Collectors.toCollection(TreeSet::new));
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList, int limit) {
public static <T> TreeSet<T> collectToTreeSetFuture(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList, int limit) {
return futureList.join().stream().map(CompletableFuture::join).limit(limit)
.collect(Collectors.toCollection(TreeSet::new));
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList, Comparator<T> comparator) {
public static <T> TreeSet<T> collectToTreeSetFuture(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList, Comparator<T> comparator) {
return futureList.join().stream().map(CompletableFuture::join).collect(Collectors.toCollection(() -> new TreeSet<>(comparator)));
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList, Comparator<T> comparator, int limit) {
public static <T> TreeSet<T> collectToTreeSetFuture(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList, Comparator<T> comparator, int limit) {
return futureList.join().stream().map(CompletableFuture::join).limit(limit)
.collect(Collectors.toCollection(() -> new TreeSet<>(comparator)));
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<T>> futureList) {
return new TreeSet<>(futureList.join());
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<T>> futureList, int limit) {
return futureList.join().stream().limit(limit)
.collect(Collectors.toCollection(TreeSet::new));
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<T>> futureList, Comparator<T> comparator) {
return futureList.join().stream().collect(Collectors.toCollection(() -> new TreeSet<>(comparator)));
}
public static <T> TreeSet<T> collectToTreeSet(CompletableFuture<? extends Collection<T>> futureList, Comparator<T> comparator, int limit) {
return futureList.join().stream().limit(limit)
.collect(Collectors.toCollection(() -> new TreeSet<>(comparator)));
}
public static <T> Optional<T> anyOrNull(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
return futureList.join().parallelStream().map(CompletableFuture::join).findAny();
}
public static <T> Optional<T> firstOrNull(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
public static <T> Optional<T> firstOrNullFuture(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList) {
return futureList.join().stream().map(CompletableFuture::join).findFirst();
}
public static <T> Optional<T> firstOrNull(CompletableFuture<? extends Collection<T>> futureList) {
return futureList.join().stream().findFirst();
}
public static <T> void forEachOrdered(CompletableFuture<? extends Collection<CompletableFuture<T>>> futureList,
Consumer<T> consumer) {
var futures = futureList.join();

View File

@ -30,7 +30,7 @@ public class SizedFutureSet<T> {
}
public LinkedHashSet<T> getDataOrdered() {
return CompletableFutureUtils.collectToLinkedSet(data);
return CompletableFutureUtils.collectToLinkedSetFuture(data);
}
public Set<T> getDataUnordered() {