KickJava   Java API By Example, From Geeks To Geeks.

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


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.SingletonNode;
6 import net.sf.saxon.value.Value;
7 import net.sf.saxon.trans.XPathException;
8
9
10 /**
11 * SingletonIterator: an iterator over a sequence of zero or one values
12 */

13
14 public class SingletonIterator implements AxisIterator,
15         ReversibleIterator, LastPositionFinder, GroundedIterator, LookaheadIterator {
16
17     private Item value;
18     private int position = 0;
19
20     /**
21      * Private constructor: external classes should use the factory method
22      * @param value the item to iterate over
23      */

24
25     private SingletonIterator(Item value) {
26         this.value = value;
27     }
28
29    /**
30     * Factory method.
31     * @param item the item to iterate over
32     * @return a SingletonIterator over the supplied item, or an EmptyIterator
33     * if the supplied item is null.
34     */

35
36     public static AxisIterator makeIterator(Item item) {
37        if (item==null) {
38            return EmptyIterator.getInstance();
39        } else {
40            return new SingletonIterator(item);
41        }
42    }
43
44     /**
45      * Determine whether there are more items to come. Note that this operation
46      * is stateless and it is not necessary (or usual) to call it before calling
47      * next(). It is used only when there is an explicit need to tell if we
48      * are at the last element.
49      *
50      * @return true if there are more items
51      */

52
53     public boolean hasNext() {
54         return position == 0;
55     }
56
57     public Item next() {
58         if (position == 0) {
59             position = 1;
60             return value;
61         } else if (position == 1) {
62             position = -1;
63             return null;
64         } else {
65             return null;
66         }
67     }
68
69     public Item current() {
70         if (position == 1) {
71             return value;
72         } else {
73             return null;
74         }
75     }
76
77     /**
78      * Return the current position in the sequence.
79      * @return 0 before the first call on next(); 1 before the second call on next(); -1 after the second
80      * call on next().
81      */

82     public int position() {
83        return position;
84     }
85
86     public int getLastPosition() {
87         return 1;
88     }
89
90     public SequenceIterator getAnother() {
91         return new SingletonIterator(value);
92     }
93
94     public SequenceIterator getReverseIterator() {
95         return new SingletonIterator(value);
96     }
97
98     public Item getValue() {
99         return value;
100     }
101
102     /**
103      * Return a SequenceExtent containing all the items in the sequence returned by this
104      * SequenceIterator
105      *
106      * @return the corresponding SequenceExtent if it exists, or null if it doesn't; in this case
107      * the caller must construct a new SequenceExtent by calling new SequenceExtent(iter.getAnother())
108      */

109
110     public Value materialize() {
111         if (value instanceof Value) {
112             try {
113                 value = (Item)((Value)value).reduce();
114             } catch (XPathException err) {}
115             return (Value)value;
116         } else {
117             return new SingletonNode((NodeInfo)value);
118         }
119     }
120
121     /**
122      * Get properties of this iterator, as a bit-significant integer.
123      *
124      * @return the properties of this iterator. This will be some combination of
125      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
126      * and {@link LOOKAHEAD}. It is always
127      * acceptable to return the value zero, indicating that there are no known special properties.
128      * It is acceptable for the properties of the iterator to change depending on its state.
129      */

130
131     public int getProperties() {
132         return GROUNDED | LAST_POSITION_FINDER | LOOKAHEAD;
133     }
134
135 }
136
137 //
138
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
139
// you may not use this file except in compliance with the License. You may obtain a copy of the
140
// License at http://www.mozilla.org/MPL/
141
//
142
// Software distributed under the License is distributed on an "AS IS" basis,
143
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
144
// See the License for the specific language governing rights and limitations under the License.
145
//
146
// The Original Code is: all this file.
147
//
148
// The Initial Developer of the Original Code is Michael H. Kay.
149
//
150
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
151
//
152
// Contributor(s): none.
153
//
154
Popular Tags