• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

amaembo / streamex / #686

19 Jul 2026 09:26AM UTC coverage: 99.777% (-0.02%) from 99.794%
#686

push

amaembo
[#286] EntryStream.withoutKeys and withoutValues don't tolerate streams containing null keys and values respectively

4 of 4 new or added lines in 1 file covered. (100.0%)

4 existing lines in 3 files now uncovered.

5814 of 5827 relevant lines covered (99.78%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.67
/src/main/java/one/util/streamex/UnorderedCancellableSpliterator.java
1
/*
2
 * Copyright 2015, 2024 StreamEx contributors
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
 *     http://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 one.util.streamex;
17

18
import java.util.Spliterator;
19
import java.util.concurrent.ConcurrentLinkedQueue;
20
import java.util.concurrent.atomic.AtomicBoolean;
21
import java.util.concurrent.atomic.AtomicInteger;
22
import java.util.function.*;
23

24
import static one.util.streamex.Internals.CancelException;
25
import static one.util.streamex.Internals.CloneableSpliterator;
26

27
/**
28
 * @author Tagir Valeev
29
 */
30
/* package */class UnorderedCancellableSpliterator<T, A> extends CloneableSpliterator<A, UnorderedCancellableSpliterator<T, A>> {
31
    private volatile Spliterator<T> source;
32
    private final BiConsumer<A, ? super T> accumulator;
33
    private final Predicate<A> cancelPredicate;
34
    private final Supplier<A> supplier;
35
    private final ConcurrentLinkedQueue<A> partialResults = new ConcurrentLinkedQueue<>();
1✔
36
    private final AtomicBoolean cancelled = new AtomicBoolean(false);
1✔
37
    private final AtomicInteger nPeers = new AtomicInteger(1);
1✔
38
    private final BinaryOperator<A> combiner;
39
    private boolean flag;
40

41
    UnorderedCancellableSpliterator(Spliterator<T> source, Supplier<A> supplier, BiConsumer<A, ? super T> accumulator,
42
            BinaryOperator<A> combiner, Predicate<A> cancelPredicate) {
1✔
43
        this.source = source;
1✔
44
        this.supplier = supplier;
1✔
45
        this.accumulator = accumulator;
1✔
46
        this.combiner = combiner;
1✔
47
        this.cancelPredicate = cancelPredicate;
1✔
48
    }
1✔
49

50
    private boolean checkCancel(A acc) {
51
        if (cancelPredicate.test(acc)) {
1✔
52
            if (cancelled.compareAndSet(false, true)) {
1✔
53
                flag = true;
1✔
54
                return true;
1✔
55
            }
56
        }
57
        if (cancelled.get()) {
1✔
58
            flag = false;
1✔
59
            return true;
1✔
60
        }
61
        return false;
1✔
62
    }
63

64
    private boolean handleCancel(Consumer<? super A> action, A acc) {
65
        source = null;
1✔
66
        if (flag) {
1✔
67
            action.accept(acc);
1✔
68
            return true;
1✔
69
        }
70
        return false;
1✔
71
    }
72

73
    @Override
74
    public boolean tryAdvance(Consumer<? super A> action) {
75
        Spliterator<T> source = this.source;
1✔
76
        if (source == null || cancelled.get()) {
1✔
77
            this.source = null;
1✔
78
            return false;
1✔
79
        }
80
        A acc = supplier.get();
1✔
81
        if (checkCancel(acc))
1✔
82
            return handleCancel(action, acc);
1✔
83
        try {
84
            source.forEachRemaining(t -> {
1✔
85
                accumulator.accept(acc, t);
1✔
86
                if (checkCancel(acc))
1✔
87
                    throw new CancelException();
1✔
88
            });
1✔
89
        } catch (CancelException ex) {
1✔
90
            return handleCancel(action, acc);
1✔
91
        }
1✔
92
        A result = acc;
1✔
93
        while (true) {
94
            A acc2 = partialResults.poll();
1✔
95
            if (acc2 == null)
1✔
96
                break;
1✔
97
            result = combiner.apply(result, acc2);
1✔
98
            if (checkCancel(result))
1✔
99
                return handleCancel(action, result);
1✔
100
        }
1✔
101
        partialResults.offer(result);
1✔
102
        this.source = null;
1✔
103
        if (nPeers.decrementAndGet() == 0) {
1✔
104
            result = partialResults.poll();
1✔
105
            // non-cancelled finish
106
            while (true) {
107
                A acc2 = partialResults.poll();
1✔
108
                if (acc2 == null)
1✔
109
                    break;
1✔
110
                result = combiner.apply(result, acc2);
1✔
111
                if (cancelPredicate.test(result))
1✔
UNCOV
112
                    break;
×
113
            }
1✔
114
            this.source = null;
1✔
115
            action.accept(result);
1✔
116
            return true;
1✔
117
        }
118
        return false;
1✔
119
    }
120

121
    @Override
122
    public void forEachRemaining(Consumer<? super A> action) {
123
        tryAdvance(action);
1✔
124
    }
1✔
125

126
    @Override
127
    public Spliterator<A> trySplit() {
128
        if (source == null || cancelled.get()) {
1✔
129
            source = null;
1✔
130
            return null;
1✔
131
        }
132
        Spliterator<T> prefix = source.trySplit();
1✔
133
        if (prefix == null) {
1✔
134
            return null;
1✔
135
        }
136
        UnorderedCancellableSpliterator<T, A> result = doClone();
1✔
137
        result.source = prefix;
1✔
138
        nPeers.incrementAndGet();
1✔
139
        return result;
1✔
140
    }
141

142
    @Override
143
    public long estimateSize() {
144
        return source == null ? 0 : source.estimateSize();
1✔
145
    }
146

147
    @Override
148
    public int characteristics() {
149
        return source == null ? SIZED : 0;
1✔
150
    }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc