KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.om;
2
3 import net.sf.saxon.expr.LastPositionFinder;
4 import net.sf.saxon.expr.ReversibleIterator;
5
6
7 /**
8   * ReverseArrayIterator is used to enumerate items held in an array in reverse order.
9   * @author Michael H. Kay
10   */

11
12
13 public final class ReverseArrayIterator implements AxisIterator,
14                                                    ReversibleIterator,
15                                                    LookaheadIterator,
16                                                    LastPositionFinder {
17
18     Item[] items;
19     int index = 0;
20     int start;
21     int end; // item after the last to be output
22
Item current = null;
23
24     /**
25      * Create an iterator a slice of an array
26      * @param items The array of items
27      * @param start The first item in the array to be be used (this will be the last
28      * one in the resulting iteration). Zero-based.
29      * @param end The item after the last one in the array to be used (this will be the
30      * first one to be returned by the iterator). Zero-based.
31     */

32
33     public ReverseArrayIterator(Item[] items, int start, int end) {
34         this.items = items;
35         this.end = end;
36         this.start = start;
37         index = end - 1;
38     }
39
40     /**
41      * Determine whether there are more items to come. Note that this operation
42      * is stateless and it is not necessary (or usual) to call it before calling
43      * next(). It is used only when there is an explicit need to tell if we
44      * are at the last element.
45      *
46      * @return true if there are more items in the sequence
47      */

48
49     public boolean hasNext() {
50         return index >= start;
51     }
52
53     public Item next() {
54         if (index >= start) {
55             current = items[index--];
56             return current;
57         } else {
58             current = null;
59             return null;
60         }
61     }
62
63     public Item current() {
64         return current;
65     }
66
67     public int position() {
68         if (index < start-1) {
69             return -1; // position() returns -1 after next() returns null
70
}
71         return end - 1 - index;
72     }
73
74     public int getLastPosition() {
75         return end - start;
76     }
77
78     public SequenceIterator getAnother() {
79         return new ReverseArrayIterator(items, start, end);
80     }
81
82     /**
83      * Get properties of this iterator, as a bit-significant integer.
84      *
85      * @return the properties of this iterator. This will be some combination of
86      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
87      * and {@link LOOKAHEAD}. It is always
88      * acceptable to return the value zero, indicating that there are no known special properties.
89      * It is acceptable for the properties of the iterator to change depending on its state.
90      */

91
92     public int getProperties() {
93         return LAST_POSITION_FINDER;
94     }
95
96     /**
97      * Get an iterator that processes the same items in reverse order.
98      * Since this iterator is processing the items backwards, this method
99      * returns an ArrayIterator that processes them forwards.
100      * @return a new ArrayIterator
101      */

102
103     public SequenceIterator getReverseIterator() {
104         return new ArrayIterator(items, start, end);
105     }
106 }
107
108
109 //
110
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
111
// you may not use this file except in compliance with the License. You may obtain a copy of the
112
// License at http://www.mozilla.org/MPL/
113
//
114
// Software distributed under the License is distributed on an "AS IS" basis,
115
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
116
// See the License for the specific language governing rights and limitations under the License.
117
//
118
// The Original Code is: all this file.
119
//
120
// The Initial Developer of the Original Code is Michael H. Kay.
121
//
122
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
123
//
124
// Contributor(s): none.
125
//
126
Popular Tags