KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > om > ArrayIterator


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 /**
9  * ArrayIterator is used to enumerate items held in an array.
10  * The items are always held in the correct sorted order for the sequence.
11  *
12  * @author Michael H. Kay
13  */

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; // position in array of current item, zero-based
24
// set equal to end+1 when all the items required have been read.
25
private int start; // position of first item to be returned, zero-based
26
private int end; // position of first item that is NOT returned, zero-based
27
private Item current = null;
28
29     /**
30      * Create an iterator over all the items in an array
31      *
32      * @param nodes the array (of any items, not necessarily nodes) to be
33      * processed by the iterator
34      */

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     /**
44      * Create an iterator over a range of an array. Note that the start position is zero-based
45      *
46      * @param nodes the array (of nodes or simple values) to be processed by
47      * the iterator
48      * @param start the position of the first item to be processed
49      * (numbering from zero). Must be between zero and nodes.length-1; if not,
50      * undefined exceptions are likely to occur.
51      * @param end position of first item that is NOT returned, zero-based. Must be
52      * beween 1 and nodes.length; if not, undefined exceptions are likely to occur.
53      */

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     /**
63      * Create a new ArrayIterator over the same items,
64      * with a different start point and end point
65      * @param min the start position (1-based) of the new ArrayIterator
66      * relative to the original
67      * @param max the end position (1-based) of the last item to be delivered
68      * by the new ArrayIterator, relative to the original. For example, min=2, max=3
69      * delivers the two items ($base[2], $base[3]). Set this to Integer.MAX_VALUE if
70      * there is no end limit.
71      */

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     /**
95      * Test whether there are any more items
96      *
97      * @return true if there are more items
98      */

99     public boolean hasNext() {
100         return index < end;
101     }
102
103     /**
104      * Get the next item in the array
105      *
106      * @return the next item in the array
107      */

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     /**
119      * Get the current item in the array
120      *
121      * @return the item returned by the most recent call of next()
122      */

123     public Item current() {
124         return current;
125     }
126
127     /**
128      * Get the position of the current item in the array
129      *
130      * @return the current position (starting at 1 for the first item)
131      */

132     public int position() {
133         if (index > end) {
134             return -1;
135         }
136         return index - start;
137     }
138
139     /**
140      * Get the number of items in the part of the array being processed
141      *
142      * @return the number of items; equivalently, the position of the last
143      * item
144      */

145     public int getLastPosition() {
146         return end - start;
147     }
148
149     /**
150      * Get another iterator over the same items
151      *
152      * @return a new ArrayIterator
153      */

154     public SequenceIterator getAnother() {
155         return new ArrayIterator(items, start, end);
156     }
157
158     /**
159      * Get an iterator that processes the same items in reverse order
160      *
161      * @return a new ArrayIterator
162      */

163     public SequenceIterator getReverseIterator() {
164         return new ReverseArrayIterator(items, start, end);
165     }
166
167     /**
168      * Indicate that any nodes returned in the sequence will be atomized. This
169      * means that if it wishes to do so, the implementation can return the typed
170      * values of the nodes rather than the nodes themselves. The implementation
171      * is free to ignore this hint.
172      * @param atomizing true if the caller of this iterator will atomize any
173      * nodes that are returned, and is therefore willing to accept the typed
174      * value of the nodes instead of the nodes themselves.
175      */

176
177     //public void setIsAtomizing(boolean atomizing) {}
178

179     /**
180      * Get the underlying array
181      *
182      * @return the underlying array being processed by the iterator
183      */

184
185     public Item[] getArray() {
186         return items;
187     }
188
189     /**
190      * Get the initial start position
191      *
192      * @return the start position of the iterator in the array (zero-based)
193      */

194
195     public int getStartPosition() {
196         return start;
197     }
198
199     /**
200      * Get the end position in the array
201      *
202      * @return the position in the array (zero-based) of the first item not included
203      * in the iteration
204      */

205
206     public int getEndPosition() {
207         return end;
208     }
209
210     /**
211      * Return a SequenceValue containing all the items in the sequence returned by this
212      * SequenceIterator
213      *
214      * @return the corresponding SequenceValue
215      */

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     /**
227      * Get properties of this iterator, as a bit-significant integer.
228      *
229      * @return the properties of this iterator. This will be some combination of
230      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
231      * and {@link LOOKAHEAD}. It is always
232      * acceptable to return the value zero, indicating that there are no known special properties.
233      * It is acceptable for the properties of the iterator to change depending on its state.
234      */

235
236     public int getProperties() {
237         return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD;
238     }
239
240 }
241
242
243 //
244
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
245
// you may not use this file except in compliance with the License. You may obtain a copy of the
246
// License at http://www.mozilla.org/MPL/
247
//
248
// Software distributed under the License is distributed on an "AS IS" basis,
249
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
250
// See the License for the specific language governing rights and limitations under the License.
251
//
252
// The Original Code is: all this file.
253
//
254
// The Initial Developer of the Original Code is Michael H. Kay.
255
//
256
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
257
//
258
// Contributor(s): none.
259
//
260
Popular Tags