| 1 | /* | |
| 2 | * Copyright 2014-2026 Grzegorz Piwowarek, https://4comprehension.com/ | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package com.pivovarit.collectors; | |
| 17 | ||
| 18 | import java.util.List; | |
| 19 | import java.util.Spliterator; | |
| 20 | import java.util.concurrent.BlockingQueue; | |
| 21 | import java.util.concurrent.CompletableFuture; | |
| 22 | import java.util.concurrent.CompletionException; | |
| 23 | import java.util.concurrent.LinkedBlockingQueue; | |
| 24 | import java.util.concurrent.TimeUnit; | |
| 25 | import java.util.concurrent.TimeoutException; | |
| 26 | import java.util.function.Consumer; | |
| 27 | ||
| 28 | /** | |
| 29 | * @author Grzegorz Piwowarek | |
| 30 | */ | |
| 31 | final class CompletionOrderSpliterator<T> implements Spliterator<T> { | |
| 32 | ||
| 33 | private final BlockingQueue<CompletableFuture<T>> completed = new LinkedBlockingQueue<>(); | |
| 34 | private final Deadline deadline; | |
| 35 | private int remaining; | |
| 36 | ||
| 37 | CompletionOrderSpliterator(List<CompletableFuture<T>> futures) { | |
| 38 | this(futures, null); | |
| 39 | } | |
| 40 | ||
| 41 | CompletionOrderSpliterator(List<CompletableFuture<T>> futures, Deadline deadline) { | |
| 42 | this.remaining = futures.size(); | |
| 43 | this.deadline = deadline; | |
| 44 |
1
1. <init> : removed call to java/util/List::forEach → TIMED_OUT |
futures.forEach(f -> f.whenComplete((__, ___) -> completed.add(f))); |
| 45 | } | |
| 46 | ||
| 47 | @Override | |
| 48 | public boolean tryAdvance(Consumer<? super T> action) { | |
| 49 |
2
1. tryAdvance : negated conditional → TIMED_OUT 2. tryAdvance : changed conditional boundary → TIMED_OUT |
if (remaining <= 0) { |
| 50 |
1
1. tryAdvance : replaced boolean return with true for com/pivovarit/collectors/CompletionOrderSpliterator::tryAdvance → KILLED |
return false; |
| 51 | } | |
| 52 |
1
1. tryAdvance : removed call to java/util/function/Consumer::accept → TIMED_OUT |
action.accept(nextCompleted().join()); |
| 53 |
1
1. tryAdvance : replaced boolean return with false for com/pivovarit/collectors/CompletionOrderSpliterator::tryAdvance → KILLED |
return true; |
| 54 | } | |
| 55 | ||
| 56 | private CompletableFuture<T> nextCompleted() { | |
| 57 | try { | |
| 58 |
1
1. nextCompleted : negated conditional → KILLED |
CompletableFuture<T> next = deadline == null |
| 59 | ? completed.take() | |
| 60 | : completed.poll(deadline.remainingNanos(), TimeUnit.NANOSECONDS); | |
| 61 | ||
| 62 |
1
1. nextCompleted : negated conditional → KILLED |
if (next == null) { |
| 63 | throw new CompletionException(new TimeoutException("Timeout while streaming results")); | |
| 64 | } | |
| 65 |
1
1. nextCompleted : Replaced integer subtraction with addition → TIMED_OUT |
remaining--; |
| 66 |
1
1. nextCompleted : replaced return value with null for com/pivovarit/collectors/CompletionOrderSpliterator::nextCompleted → KILLED |
return next; |
| 67 | } catch (InterruptedException e) { | |
| 68 |
1
1. nextCompleted : removed call to java/lang/Thread::interrupt → KILLED |
Thread.currentThread().interrupt(); |
| 69 | ||
| 70 | throw new CompletionException(e); | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | @Override | |
| 75 | public Spliterator<T> trySplit() { | |
| 76 | return null; | |
| 77 | } | |
| 78 | ||
| 79 | @Override | |
| 80 | public long estimateSize() { | |
| 81 |
1
1. estimateSize : replaced long return with 0 for com/pivovarit/collectors/CompletionOrderSpliterator::estimateSize → NO_COVERAGE |
return remaining; |
| 82 | } | |
| 83 | ||
| 84 | @Override | |
| 85 | public int characteristics() { | |
| 86 |
1
1. characteristics : replaced int return with 0 for com/pivovarit/collectors/CompletionOrderSpliterator::characteristics → SURVIVED |
return IMMUTABLE; |
| 87 | } | |
| 88 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 49 |
1.1 2.2 |
|
| 50 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 58 |
1.1 |
|
| 62 |
1.1 |
|
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 68 |
1.1 |
|
| 81 |
1.1 |
|
| 86 |
1.1 |