1 package net.sf.saxon.om; 2 3 import net.sf.saxon.expr.LastPositionFinder; 4 import net.sf.saxon.expr.ReversibleIterator; 5 import net.sf.saxon.value.SequenceExtent; 6 import net.sf.saxon.value.Value; 7 8 14 15 16 public final class ArrayIterator implements AxisIterator, 17 ReversibleIterator, 18 LastPositionFinder, 19 LookaheadIterator, 20 GroundedIterator { 21 22 private Item[] items; 23 private int index; private int start; private int end; private Item current = null; 28 29 35 36 public ArrayIterator(Item[] nodes) { 37 this.items = nodes; 38 this.start = 0; 39 this.end = nodes.length; 40 index = 0; 41 } 42 43 54 55 public ArrayIterator(Item[] nodes, int start, int end) { 56 this.items = nodes; 57 this.end = end; 58 this.start = start; 59 index = start; 60 } 61 62 72 73 public SequenceIterator makeSliceIterator(int min, int max) { 74 Item[] items = getArray(); 75 int currentStart = getStartPosition(); 76 int currentEnd = getEndPosition(); 77 if (min < 1) { 78 min = 1; 79 } 80 int newStart = currentStart + (min-1); 81 if (newStart < currentStart) { 82 newStart = currentStart; 83 } 84 int newEnd = (max == Integer.MAX_VALUE ? currentEnd : newStart + (max - min + 1)); 85 if (newEnd > currentEnd) { 86 newEnd = currentEnd; 87 } 88 if (newEnd <= newStart) { 89 return EmptyIterator.getInstance(); 90 } 91 return new ArrayIterator(items, newStart, newEnd); 92 } 93 94 99 public boolean hasNext() { 100 return index < end; 101 } 102 103 108 public Item next() { 109 if (index >= end) { 110 index = end+1; 111 current = null; 112 return null; 113 } 114 current = items[index++]; 115 return current; 116 } 117 118 123 public Item current() { 124 return current; 125 } 126 127 132 public int position() { 133 if (index > end) { 134 return -1; 135 } 136 return index - start; 137 } 138 139 145 public int getLastPosition() { 146 return end - start; 147 } 148 149 154 public SequenceIterator getAnother() { 155 return new ArrayIterator(items, start, end); 156 } 157 158 163 public SequenceIterator getReverseIterator() { 164 return new ReverseArrayIterator(items, start, end); 165 } 166 167 176 177 179 184 185 public Item[] getArray() { 186 return items; 187 } 188 189 194 195 public int getStartPosition() { 196 return start; 197 } 198 199 205 206 public int getEndPosition() { 207 return end; 208 } 209 210 216 217 public Value materialize() { 218 if (start==0 && end == items.length) { 219 return new SequenceExtent(items); 220 } else { 221 SequenceExtent e = new SequenceExtent(items); 222 return new SequenceExtent(e, start, end-start); 223 } 224 } 225 226 235 236 public int getProperties() { 237 return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD; 238 } 239 240 } 241 242 243 | Popular Tags |