Debian Codegraph @

net/sf/saxon/tree/iter/ListIterator.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.expr.LastPositionFinder;
11 import net.sf.saxon.om.GroundedValue;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.NodeInfo;
14 import net.sf.saxon.om.SequenceIterator;
15 import net.sf.saxon.value.AtomicValue;
16 import net.sf.saxon.value.SequenceExtent;
17
18 import java.util.List;
19
20 /**
21 * Class ListIterator, iterates over a sequence of items held in a Java List
22 */
23
24 public class ListIterator<T extends Item<?>>
25 implements UnfailingIterator<T>, LastPositionFinder, LookaheadIterator<T>, GroundedIterator<T>, ReversibleIterator<T> {
26
27 private int index = 0;
28 protected List<T> list;
29
30 /**
31 * Create a ListIterator over a given List
32 *
33 * @param list the list: all objects in the list must be instances of {@link Item}
34 */
35
36 public ListIterator(List<T> list) {
37 index = 0;
38 this.list = list;
39 }
40
41 public boolean hasNext() {
42 return index < list.size();
43 }
44
45 /*@Nullable*/
46 public T next() {
47 if (index >= list.size()) {
48 return null;
49 }
50 return list.get(index++);
51 }
52
53 public int getLength() {
54 return list.size();
55 }
56
57 /**
58 * Get properties of this iterator, as a bit-significant integer.
59 *
60 * @return the properties of this iterator. This will be some combination of
61 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
62 * and {@link #LOOKAHEAD}. It is always
63 * acceptable to return the value zero, indicating that there are no known special properties.
64 * It is acceptable for the properties of the iterator to change depending on its state.
65 */
66
67 public int getProperties() {
68 return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD;
69 }
70
71 /**
72 * Return a Sequence containing all the items in the sequence returned by this
73 * SequenceIterator
74 *
75 * @return the corresponding GroundedValue
76 */
77
78 /*@Nullable*/
79 public GroundedValue<T> materialize() {
80 return SequenceExtent.makeSequenceExtent(list);
81 }
82
83 @Override
84 public GroundedValue<T> getResidue() {
85 List<T> l2 = list;
86 if (index != 0) {
87 l2 = l2.subList(index, l2.size());
88 }
89 return SequenceExtent.makeSequenceExtent(l2);
90 }
91
92 public SequenceIterator<T> getReverseIterator() {
93 return new ReverseListIterator<>(list);
94 }
95
96 public static class Atomic extends ListIterator<AtomicValue> implements AtomicIterator {
97 public Atomic (List<AtomicValue> list) {
98 super(list);
99 }
100 }
101
102 public static class OfNodes extends ListIterator<NodeInfo> implements AxisIterator {
103 public OfNodes(List<NodeInfo> list) {
104 super(list);
105 }
106
107 public NodeInfo next() {
108 return super.next();
109 }
110
111 }
112 }
113