001: package net.sf.saxon.value;
002:
003: import net.sf.saxon.om.SequenceIterator;
004: import net.sf.saxon.om.Item;
005: import net.sf.saxon.om.NamePool;
006: import net.sf.saxon.expr.XPathContext;
007: import net.sf.saxon.expr.RangeExpression;
008: import net.sf.saxon.expr.StaticProperty;
009: import net.sf.saxon.expr.ExpressionTool;
010: import net.sf.saxon.trans.XPathException;
011: import net.sf.saxon.type.ItemType;
012: import net.sf.saxon.type.Type;
013: import net.sf.saxon.type.TypeHierarchy;
014:
015: import java.io.PrintStream;
016:
017: /**
018: * This class represents a sequence of consecutive ascending integers, for example 1 to 50.
019: * The integers must be within the range of a Java long.
020: */
021:
022: public class IntegerRange extends Value {
023:
024: public long start;
025: public long end;
026:
027: public IntegerRange(long start, long end) {
028: this .start = start;
029: this .end = end;
030: }
031:
032: public long getStart() {
033: return start;
034: }
035:
036: public long getEnd() {
037: return end;
038: }
039:
040: /**
041: * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
042: * This method indicates which of these methods is provided directly. The other methods will always be available
043: * indirectly, using an implementation that relies on one of the other methods.
044: */
045:
046: public int getImplementationMethod() {
047: return ITERATE_METHOD;
048: }
049:
050: /**
051: * Return an Iterator to iterate over the values of a sequence. The value of every
052: * expression can be regarded as a sequence, so this method is supported for all
053: * expressions. This default implementation handles iteration for expressions that
054: * return singleton values: for non-singleton expressions, the subclass must
055: * provide its own implementation.
056: *
057: * @param context supplies the context for evaluation
058: * @return a SequenceIterator that can be used to iterate over the result
059: * of the expression
060: * @throws net.sf.saxon.trans.XPathException
061: * if any dynamic error occurs evaluating the
062: * expression
063: */
064:
065: public SequenceIterator iterate(XPathContext context)
066: throws XPathException {
067: return new RangeExpression.RangeIterator(start, end);
068: }
069:
070: /**
071: * Determine the data type of the items in the expression, if possible
072: *
073: * @return AnyItemType (not known)
074: * @param th
075: */
076:
077: public ItemType getItemType(TypeHierarchy th) {
078: return Type.INTEGER_TYPE;
079: }
080:
081: /**
082: * Determine the cardinality
083: */
084:
085: public int getCardinality() {
086: return StaticProperty.ALLOWS_MANY;
087: }
088:
089: /**
090: * Get the n'th item in the sequence (starting from 0). This is defined for all
091: * Values, but its real benefits come for a sequence Value stored extensionally
092: * (or for a MemoClosure, once all the values have been read)
093: */
094:
095: public Item itemAt(int n) throws XPathException {
096: if (n < 0 || n > (end - start)) {
097: return null;
098: }
099: return new IntegerValue(start + n);
100: }
101:
102: /**
103: * Get the length of the sequence
104: */
105:
106: public int getLength() throws XPathException {
107: return (int) (end - start + 1);
108: }
109:
110: /**
111: * Diagnostic display of the expression
112: */
113:
114: public void display(int level, NamePool pool, PrintStream out) {
115: System.err.println(ExpressionTool.indent(level) + start
116: + " to " + end);
117: }
118: }
119:
120: //
121: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
122: // you may not use this file except in compliance with the License. You may obtain a copy of the
123: // License at http://www.mozilla.org/MPL/
124: //
125: // Software distributed under the License is distributed on an "AS IS" basis,
126: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
127: // See the License for the specific language governing rights and limitations under the License.
128: //
129: // The Original Code is: all this file.
130: //
131: // The Initial Developer of the Original Code is Michael H. Kay.
132: //
133: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
134: //
135: // Contributor(s): none.
136: //
|