Debian Codegraph @

net/sf/saxon/tree/iter/UnfailingIterator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.tree.iter;
9
10 import net.sf.saxon.om.GroundedValue;
11 import net.sf.saxon.om.Item;
12 import net.sf.saxon.om.SequenceIterator;
13 import net.sf.saxon.value.SequenceExtent;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.function.Consumer;
19 import java.util.function.Predicate;
20
21
22 /**
23 * A SequenceIterator is used to iterate over a sequence. An UnfailingIterator
24 * is a SequenceIterator that throws no checked exceptions.
25 */
26
27 public interface UnfailingIterator<T extends Item<?>> extends SequenceIterator<T> {
28
29 /**
30 * Get the next item in the sequence.
31 *
32 * @return the next Item. If there are no more items, return null.
33 */
34
35 T next();
36
37 //@Override
38 default void forEach(Consumer<T> consumer) {
39 T item;
40 while ((item = next()) != null) {
41 consumer.accept(item);
42 }
43 }
44
45 default Optional<T> firstWith(Predicate<? super T> condition) {
46 T item;
47 while ((item = next()) != null) {
48 if (condition.test(item)) {
49 close();
50 return Optional.of(item);
51 }
52 }
53 return Optional.empty();
54 }
55
56 default List<T> toList() {
57 List<T> list = new ArrayList<>();
58 forEach(list::add);
59 return list;
60 }
61
62 default GroundedValue<T> toGroundedValue() {
63 return SequenceExtent.makeSequenceExtent(toList());
64 }
65 }
66