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

amaembo / streamex / #677

02 Nov 2024 08:50AM UTC coverage: 99.673%. Remained the same
#677

push

amaembo
Optimize imports

5786 of 5805 relevant lines covered (99.67%)

1.0 hits per line

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

97.73
/src/main/java/one/util/streamex/HeadTailSpliterator.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.Spliterators;
20
import java.util.Spliterators.AbstractSpliterator;
21
import java.util.function.BiFunction;
22
import java.util.function.Consumer;
23
import java.util.function.Supplier;
24
import java.util.stream.Stream;
25

26
import static one.util.streamex.Internals.Box;
27
import static one.util.streamex.Internals.TailSpliterator;
28

29
/**
30
 * @author Tagir Valeev
31
 */
32
/*package*/ final class HeadTailSpliterator<T, U> extends AbstractSpliterator<U> implements TailSpliterator<U> {
33
    private Spliterator<T> source;
34
    private BiFunction<? super T, ? super StreamEx<T>, ? extends Stream<U>> mapper;
35
    private Supplier<? extends Stream<U>> emptyMapper;
36
    private Spliterator<U> target;
37
    StreamContext context;
38
    
39
    HeadTailSpliterator(Spliterator<T> source, BiFunction<? super T, ? super StreamEx<T>, ? extends Stream<U>> mapper,
40
            Supplier<? extends Stream<U>> emptyMapper) {
41
        super(Long.MAX_VALUE, ORDERED);
1✔
42
        this.source = source;
1✔
43
        this.mapper = mapper;
1✔
44
        this.emptyMapper = emptyMapper;
1✔
45
    }
1✔
46
    
47
    @Override
48
    public boolean tryAdvance(Consumer<? super U> action) {
49
        if (!init())
1✔
50
            return false;
1✔
51
        target = TailSpliterator.tryAdvanceWithTail(target, action);
1✔
52
        if (target == null) {
1✔
53
            context = null;
1✔
54
            return false;
1✔
55
        }
56
        return true;
1✔
57
    }
58

59
    @Override
60
    public Spliterator<U> tryAdvanceOrTail(Consumer<? super U> action) {
61
        if (!init())
1✔
62
            return null;
×
63
        Spliterator<U> tail = target;
1✔
64
        target = null;
1✔
65
        context = null;
1✔
66
        return tail;
1✔
67
    }
68

69
    @Override
70
    public void forEachRemaining(Consumer<? super U> action) {
71
        if (!init())
1✔
72
            return;
1✔
73
        TailSpliterator.forEachWithTail(target, action);
1✔
74
        target = null;
1✔
75
        context = null;
1✔
76
    }
1✔
77

78
    @Override
79
    public Spliterator<U> forEachOrTail(Consumer<? super U> action) {
80
        return tryAdvanceOrTail(action);
1✔
81
    }
82

83
    private boolean init() {
84
        if (context == null)
1✔
85
            return false;
1✔
86
        if (target == null) {
1✔
87
            Box<T> first = new Box<>();
1✔
88
            source = TailSpliterator.tryAdvanceWithTail(source, first);
1✔
89
            Stream<U> stream = source == null ? emptyMapper.get() : mapper.apply(first.a, StreamEx.of(source));
1✔
90
            source = null;
1✔
91
            mapper = null;
1✔
92
            emptyMapper = null;
1✔
93
            if (stream == null) {
1✔
94
                target = Spliterators.emptySpliterator();
1✔
95
            } else {
96
                StreamContext ctx = StreamContext.of(stream);
1✔
97
                if (ctx.closeHandler != null)
1✔
98
                    context.onClose(ctx.closeHandler);
1✔
99
                target = stream.spliterator();
1✔
100
            }
101
        }
102
        return true;
1✔
103
    }
104
    
105
    @Override
106
    public long estimateSize() {
107
        if (context == null)
1✔
108
            return 0;
1✔
109
        return (target == null ? source : target).estimateSize();
1✔
110
    }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc