001: package net.sf.saxon.om;
002:
003: import net.sf.saxon.expr.LastPositionFinder;
004: import net.sf.saxon.expr.ReversibleIterator;
005:
006: /**
007: * ReverseArrayIterator is used to enumerate items held in an array in reverse order.
008: * @author Michael H. Kay
009: */
010:
011: public final class ReverseArrayIterator implements AxisIterator,
012: ReversibleIterator, LookaheadIterator, LastPositionFinder {
013:
014: Item[] items;
015: int index = 0;
016: int start;
017: int end; // item after the last to be output
018: Item current = null;
019:
020: /**
021: * Create an iterator a slice of an array
022: * @param items The array of items
023: * @param start The first item in the array to be be used (this will be the last
024: * one in the resulting iteration). Zero-based.
025: * @param end The item after the last one in the array to be used (this will be the
026: * first one to be returned by the iterator). Zero-based.
027: */
028:
029: public ReverseArrayIterator(Item[] items, int start, int end) {
030: this .items = items;
031: this .end = end;
032: this .start = start;
033: index = end - 1;
034: }
035:
036: /**
037: * Determine whether there are more items to come. Note that this operation
038: * is stateless and it is not necessary (or usual) to call it before calling
039: * next(). It is used only when there is an explicit need to tell if we
040: * are at the last element.
041: *
042: * @return true if there are more items in the sequence
043: */
044:
045: public boolean hasNext() {
046: return index >= start;
047: }
048:
049: public Item next() {
050: if (index >= start) {
051: current = items[index--];
052: return current;
053: } else {
054: current = null;
055: return null;
056: }
057: }
058:
059: public Item current() {
060: return current;
061: }
062:
063: public int position() {
064: if (index < start - 1) {
065: return -1; // position() returns -1 after next() returns null
066: }
067: return end - 1 - index;
068: }
069:
070: public int getLastPosition() {
071: return end - start;
072: }
073:
074: public SequenceIterator getAnother() {
075: return new ReverseArrayIterator(items, start, end);
076: }
077:
078: /**
079: * Get properties of this iterator, as a bit-significant integer.
080: *
081: * @return the properties of this iterator. This will be some combination of
082: * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
083: * and {@link LOOKAHEAD}. It is always
084: * acceptable to return the value zero, indicating that there are no known special properties.
085: * It is acceptable for the properties of the iterator to change depending on its state.
086: */
087:
088: public int getProperties() {
089: return LAST_POSITION_FINDER;
090: }
091:
092: /**
093: * Get an iterator that processes the same items in reverse order.
094: * Since this iterator is processing the items backwards, this method
095: * returns an ArrayIterator that processes them forwards.
096: * @return a new ArrayIterator
097: */
098:
099: public SequenceIterator getReverseIterator() {
100: return new ArrayIterator(items, start, end);
101: }
102: }
103:
104: //
105: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
106: // you may not use this file except in compliance with the License. You may obtain a copy of the
107: // License at http://www.mozilla.org/MPL/
108: //
109: // Software distributed under the License is distributed on an "AS IS" basis,
110: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
111: // See the License for the specific language governing rights and limitations under the License.
112: //
113: // The Original Code is: all this file.
114: //
115: // The Initial Developer of the Original Code is Michael H. Kay.
116: //
117: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
118: //
119: // Contributor(s): none.
120: //
|