KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > IntegerRange


1 package net.sf.saxon.value;
2
3 import net.sf.saxon.om.SequenceIterator;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.expr.XPathContext;
7 import net.sf.saxon.expr.RangeExpression;
8 import net.sf.saxon.expr.StaticProperty;
9 import net.sf.saxon.expr.ExpressionTool;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.type.ItemType;
12 import net.sf.saxon.type.Type;
13
14 import java.io.PrintStream JavaDoc;
15
16 /**
17  * This class represents a sequence of consecutive ascending integers, for example 1 to 50.
18  * The integers must be within the range of a Java long.
19  */

20
21 public class IntegerRange extends Value {
22
23     public long start;
24     public long end;
25
26     public IntegerRange(long start, long end) {
27         this.start = start;
28         this.end = end;
29     }
30
31     public long getStart() {
32         return start;
33     }
34
35     public long getEnd() {
36         return end;
37     }
38
39     /**
40      * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
41      * This method indicates which of these methods is provided directly. The other methods will always be available
42      * indirectly, using an implementation that relies on one of the other methods.
43      */

44
45     public int getImplementationMethod() {
46         return ITERATE_METHOD;
47     }
48
49     /**
50      * Return an Iterator to iterate over the values of a sequence. The value of every
51      * expression can be regarded as a sequence, so this method is supported for all
52      * expressions. This default implementation handles iteration for expressions that
53      * return singleton values: for non-singleton expressions, the subclass must
54      * provide its own implementation.
55      *
56      * @param context supplies the context for evaluation
57      * @return a SequenceIterator that can be used to iterate over the result
58      * of the expression
59      * @throws net.sf.saxon.trans.XPathException
60      * if any dynamic error occurs evaluating the
61      * expression
62      */

63
64     public SequenceIterator iterate(XPathContext context) throws XPathException {
65         return new RangeExpression.RangeIterator(start, end);
66     }
67
68     /**
69      * Determine the data type of the items in the expression, if possible
70      *
71      * @return AnyItemType (not known)
72      */

73
74     public ItemType getItemType() {
75         return Type.INTEGER_TYPE;
76     }
77
78     /**
79      * Determine the cardinality
80      */

81
82     public int getCardinality() {
83         return StaticProperty.ALLOWS_MANY;
84     }
85
86     /**
87      * Get the n'th item in the sequence (starting from 0). This is defined for all
88      * Values, but its real benefits come for a sequence Value stored extensionally
89      * (or for a MemoClosure, once all the values have been read)
90      */

91
92     public Item itemAt(int n) throws XPathException {
93         if (n < 0 || n > (end-start)) {
94             return null;
95         }
96         return new IntegerValue(start + n);
97     }
98
99     /**
100      * Get the length of the sequence
101      */

102
103     public int getLength() throws XPathException {
104         return (int)(end - start + 1);
105     }
106
107     /**
108      * Diagnostic display of the expression
109      */

110
111     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
112         System.err.println(ExpressionTool.indent(level) + start + " to " + end);
113     }
114 }
115
116 //
117
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
118
// you may not use this file except in compliance with the License. You may obtain a copy of the
119
// License at http://www.mozilla.org/MPL/
120
//
121
// Software distributed under the License is distributed on an "AS IS" basis,
122
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
123
// See the License for the specific language governing rights and limitations under the License.
124
//
125
// The Original Code is: all this file.
126
//
127
// The Initial Developer of the Original Code is Michael H. Kay.
128
//
129
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
130
//
131
// Contributor(s): none.
132
//
133
Popular Tags