Debian Codegraph @

net/sf/saxon/om/Item.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.om;
9
10 import net.sf.saxon.tree.jiter.MonoIterator;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.tree.iter.SingletonIterator;
13 import net.sf.saxon.tree.iter.UnfailingIterator;
14 import net.sf.saxon.value.EmptySequence;
15
16 import java.util.Iterator;
17
18 /**
19 * An Item is an object that can occur as a member of a sequence.
20 * It corresponds directly to the concept of an item in the XPath 2.0 data model.
21 * There are four kinds of Item: atomic values, nodes, function items, and external objects.
22 * <p>This interface is part of the public Saxon API. As such (starting from Saxon 8.4),
23 * methods that form part of the stable API are labelled with a JavaDoc "since" tag
24 * to identify the Saxon release at which they were introduced.</p>
25 * <p>Note: there is no method getItemType(). This is to avoid having to implement it
26 * on every implementation of NodeInfo. Instead, use the static method Type.getItemType(Item).</p>
27 *
28 * @param <T> (inherited from Sequence): the type of items that can exist in the sequence.
29 * This is a bit of a curiosity because the only item that can exist
30 * in a singleton sequence is the item itself. This leads to some strange abuses
31 * of the Java generics mechanisms.
32 *
33 */
34
35 public interface Item<T extends Item<?>> extends GroundedValue<T> {
36
37 /**
38 * Get the genre of this item (to distinguish the top-level categories of item,
39 * such as nodes, atomic values, and functions)
40 * @return the genre
41 */
42
43 Genre getGenre();
44
45 /**
46 * Get the first item in the sequence. Differs from the superclass {@link Sequence} in that
47 * * no exception is thrown.
48 *
49 * @return the first item in the sequence if there is one, or null if the sequence
50 * is empty
51 */
52
53 @SuppressWarnings("unchecked")
54 default T head() {
55 return (T) this;
56 }
57
58 /**
59 * Get the value of the item as a string. For nodes, this is the string value of the
60 * node as defined in the XPath 2.0 data model, except that all nodes are treated as being
61 * untyped: it is not an error to get the string value of a node with a complex type.
62 * For atomic values, the method returns the result of casting the atomic value to a string.
63 * <p>If the calling code can handle any CharSequence, the method {@link #getStringValueCS} should
64 * be used. If the caller requires a string, this method is preferred.</p>
65 *
66 * @return the string value of the item
67 * @throws UnsupportedOperationException if the item is a function item (an unchecked exception
68 * is used here to avoid introducing exception handling to a large number of paths where it is not
69 * needed)
70 * @see #getStringValueCS
71 * @since 8.4
72 */
73
74 String getStringValue();
75
76 /**
77 * Get the string value of the item as a CharSequence. This is in some cases more efficient than
78 * the version of the method that returns a String. The method satisfies the rule that
79 * <code>X.getStringValueCS().toString()</code> returns a string that is equal to
80 * <code>X.getStringValue()</code>.
81 * <p>Note that two CharSequence values of different types should not be compared using equals(), and
82 * for the same reason they should not be used as a key in a hash table.</p>
83 * <p>If the calling code can handle any CharSequence, this method should
84 * be used. If the caller requires a string, the {@link #getStringValue} method is preferred.</p>
85 *
86 * @return the string value of the item
87 * @throws UnsupportedOperationException if the item is a function item (an unchecked exception
88 * is used here to avoid introducing exception handling to a large number of paths where it is not
89 * needed)
90 * @see #getStringValue
91 * @since 8.4
92 */
93
94 CharSequence getStringValueCS();
95
96 /**
97 * Atomize the item.
98 * @return the result of atomization
99 * @throws net.sf.saxon.trans.XPathException if atomization is not allowed
100 * for this kind of item
101 */
102
103 AtomicSequence atomize() throws XPathException;
104
105 /**
106 * Provide a short string showing the contents of the item, suitable
107 * for use in error messages
108 * @return a depiction of the item suitable for use in error messages
109 */
110
111 default String toShortString() {
112 return toString();
113 }
114
115 /**
116 * Get the contents of this value in the form of a Java {@link java.util.Iterator},
117 * so that the sequence value can be used in a for-each expression
118 *
119 * @return an Iterator delivering a sequence of items containing this single item only
120 */
121
122 default Iterator<T> iterator() {
123 return new MonoIterator<>(head());
124 }
125
126 /**
127 * Get the n'th item in the value, counting from 0
128 *
129 * @param n the index of the required item, with 0 representing the first item in the sequence
130 * @return the n'th item if it exists, or null otherwise
131 */
132 default T itemAt(int n) {
133 return n == 0 ? head() : null;
134 }
135
136 /**
137 * Get a subsequence of the value
138 *
139 * @param start the index of the first item to be included in the result, counting from zero.
140 * A negative value is taken as zero. If the value is beyond the end of the sequence, an empty
141 * sequence is returned
142 * @param length the number of items to be included in the result. Specify Integer.MAX_VALUE to
143 * get the subsequence up to the end of the base sequence. If the value is negative, an empty sequence
144 * is returned. If the value goes off the end of the sequence, the result returns items up to the end
145 * of the sequence
146 * @return the required subsequence. If min is
147 */
148 default GroundedValue<T> subsequence(int start, int length) {
149 return start <= 0 && (start + length) > 0 ? this : EmptySequence.getInstance();
150 }
151
152 /**
153 * Get the size of the value (the number of items)
154 *
155 * @return the number of items in the sequence. Note that for a single item, including a map or array,
156 * the result is always 1 (one).
157 */
158 default int getLength() {
159 return 1;
160 }
161
162 /**
163 * Get an iterator over all the items in the sequence
164 *
165 * @return an iterator over all the items
166 */
167 default UnfailingIterator<T> iterate() {
168 return SingletonIterator.makeIterator(head());
169 }
170
171 /**
172 * Reduce the sequence to its simplest form. If the value is an empty sequence, the result will be
173 * EmptySequence.getInstance(). If the value is a single atomic value, the result will be an instance
174 * of AtomicValue. If the value is a single item of any other kind, the result will be an instance
175 * of Item. Otherwise, the result will typically be unchanged.
176 *
177 * @return the simplified sequence
178 */
179 default GroundedValue<T> reduce() {
180 return this;
181 }
182
183 static <T extends Item<?>> GroundedValue<T> toGroundedValue(Item<T> item) {
184 return item.reduce();
185 }
186
187 /**
188 * Ask whether this is a node in a streamed document
189 *
190 * @return true if the node is in a document being processed using streaming
191 */
192
193 default boolean isStreamed() {
194 return false;
195 }
196
197
198 }
199
200