001: package net.sf.saxon.om;
002:
003: import net.sf.saxon.expr.LastPositionFinder;
004: import net.sf.saxon.value.SequenceExtent;
005: import net.sf.saxon.value.Value;
006:
007: import java.util.List;
008:
009: /**
010: * Class ListIterator, iterates over a sequence of items held in a Java ArrayList,
011: * or indeed in any other kind of List
012: */
013:
014: public final class ListIterator implements AxisIterator,
015: LastPositionFinder, LookaheadIterator, GroundedIterator {
016:
017: int index = 0;
018: int length;
019: Item current = null;
020: List list = null;
021:
022: /**
023: * Create a ListIterator over a given List
024: * @param list the list: all objects in the list must be instances of {@link Item}
025: */
026:
027: public ListIterator(List list) {
028: index = 0;
029: this .list = list;
030: this .length = list.size();
031: }
032:
033: /**
034: * Create a ListIterator over the leading part of a given List
035: * @param list the list: all objects in the list must be instances of {@link Item}
036: * @param length: the number of items to be included
037: */
038:
039: public ListIterator(List list, int length) {
040: index = 0;
041: this .list = list;
042: this .length = length;
043: }
044:
045: public boolean hasNext() {
046: return index < length;
047: }
048:
049: public Item next() {
050: if (index >= length) {
051: current = null;
052: index = -1;
053: length = -1;
054: return null;
055: }
056: current = (Item) list.get(index++);
057: return current;
058: }
059:
060: public Item current() {
061: return current;
062: }
063:
064: public int position() {
065: return index;
066: }
067:
068: public int getLastPosition() {
069: return length;
070: }
071:
072: public SequenceIterator getAnother() {
073: return new ListIterator(list);
074: }
075:
076: /**
077: * Get properties of this iterator, as a bit-significant integer.
078: *
079: * @return the properties of this iterator. This will be some combination of
080: * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
081: * and {@link LOOKAHEAD}. It is always
082: * acceptable to return the value zero, indicating that there are no known special properties.
083: * It is acceptable for the properties of the iterator to change depending on its state.
084: */
085:
086: public int getProperties() {
087: return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD;
088: }
089:
090: /**
091: * Return a SequenceValue containing all the items in the sequence returned by this
092: * SequenceIterator
093: *
094: * @return the corresponding SequenceValue
095: */
096:
097: public Value materialize() {
098: return new SequenceExtent(list);
099: }
100:
101: }
102:
103: //
104: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
105: // you may not use this file except in compliance with the License. You may obtain a copy of the
106: // License at http://www.mozilla.org/MPL/
107: //
108: // Software distributed under the License is distributed on an "AS IS" basis,
109: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
110: // See the License for the specific language governing rights and limitations under the License.
111: //
112: // The Original Code is: all this file.
113: //
114: // The Initial Developer of the Original Code is Michael H. Kay.
115: //
116: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
117: //
118: // Contributor(s): none.
119: //
|