Debian Codegraph @

net/sf/saxon/ma/map/RangeKey.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.ma.map;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.expr.sort.AtomicComparer;
12 import net.sf.saxon.expr.sort.AtomicMatchKey;
13 import net.sf.saxon.expr.sort.CodepointMatchKey;
14 import net.sf.saxon.functions.Count;
15 import net.sf.saxon.om.Function;
16 import net.sf.saxon.om.GroundedValue;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.om.Sequence;
19 import net.sf.saxon.trace.ExpressionPresenter;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.AtomicIterator;
22 import net.sf.saxon.type.AtomicType;
23 import net.sf.saxon.type.BuiltInAtomicType;
24 import net.sf.saxon.type.TypeHierarchy;
25 import net.sf.saxon.type.UType;
26 import net.sf.saxon.value.*;
27
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.TreeMap;
31
32 /**
33 * This class implements an XPath map item as a view of an XSLT key index. It is possible to specify a minimum
34 * and maximum key value to be included in the map, and keys are returned in sorted order.
35 * <p>At present range keys are only available for string-valued keys using the Unicode codepoint collating sequence.</p>
36 */
37
38 public class RangeKey implements MapItem {
39
40 private CodepointMatchKey min;
41 private CodepointMatchKey max;
42 private TreeMap<AtomicMatchKey, List<NodeInfo>> index;
43
44 public RangeKey(String min, String max, TreeMap<AtomicMatchKey, List<NodeInfo>> index) {
45 this.min = min == null ? null : new CodepointMatchKey(min);
46 this.max = max == null ? null : new CodepointMatchKey(max);
47 this.index = index;
48 }
49
50 /**
51 * Get an entry from the Map
52 * @param key the value of the key
53 * @return the value associated with the given key, or null if the key is not present in the map
54 */
55 public GroundedValue<?> get(AtomicValue key) {
56 CodepointMatchKey k = new CodepointMatchKey(key.getStringValue());
57 if ((min == null || min.compareTo(k) <= 0) &&
58 (max == null || max.compareTo(k) >= 0)) {
59 List<NodeInfo> nodes = index.get(k);
60 if (nodes == null) {
61 return EmptySequence.getInstance();
62 } else {
63 return SequenceExtent.makeSequenceExtent(nodes);
64 }
65 }
66 return EmptySequence.getInstance();
67 }
68
69 /**
70 * Get the size of the map
71 *
72 * @return the number of keys/entries present in this map
73 */
74 public int size() {
75 try {
76 return Count.count(keys());
77 } catch (XPathException err) {
78 return 0;
79 }
80 }
81
82 /**
83 * Ask whether the map is empty
84 *
85 * @return true if and only if the size of the map is zero
86 */
87 public boolean isEmpty() {
88 return keys().next() == null;
89 }
90
91 /**
92 * Get the set of all key values in the map.
93 *
94 * @return a set containing all the key values present in the map.
95 */
96 public AtomicIterator keys() {
97 return new RangeKeyIterator();
98 }
99
100 /**
101 * Get the set of all key-value pairs in the map
102 *
103 * @return an iterator over the key-value pairs
104 */
105 public Iterable<KeyValuePair> keyValuePairs() {
106 return () -> new Iterator<KeyValuePair>() {
107 AtomicIterator keys = keys();
108 AtomicValue next = keys.next();
109 public boolean hasNext() {
110 return next != null;
111 }
112
113 public KeyValuePair next() {
114 if (next == null) {
115 return null;
116 } else {
117 KeyValuePair kvp = new KeyValuePair(next, get(next));
118 next = keys.next();
119 return kvp;
120 }
121 }
122 };
123 }
124
125 /**
126 * Remove an entry from the map
127 *
128 * @param key the key of the entry to be removed
129 * @return a new map in which the requested entry has been removed; or this map
130 * unchanged if the specified key was not present
131 */
132 public MapItem remove(AtomicValue key) {
133 return HashTrieMap.copy(this).remove(key);
134 }
135
136 /**
137 * Get the lowest common item type of the keys in the map
138 *
139 * @return the most specific type to which all the keys belong. If the map is
140 * empty, return ErrorType.getInstance() (the type with no instances)
141 */
142 public UType getKeyUType() {
143 return UType.STRING;
144 }
145
146 /**
147 * Create a new map containing the existing entries in the map plus an additional entry,
148 * without modifying the original. If there is already an entry with the specified key,
149 * this entry is replaced by the new entry.
150 *
151 * @param key the key of the new entry
152 * @param value the value associated with the new entry
153 * @return the new map containing the additional entry
154 */
155 @Override
156 public MapItem addEntry(AtomicValue key, GroundedValue<?> value) {
157 return HashTrieMap.copy(this).addEntry(key, value);
158 }
159
160 /**
161 * Ask whether the map conforms to a given map type
162 *
163 * @param keyType the required keyType
164 * @param valueType the required valueType
165 * @param th the type hierarchy cache for the configuration
166 * @return true if the map conforms to the required type
167 */
168 @Override
169 public boolean conforms(AtomicType keyType, SequenceType valueType, TypeHierarchy th) {
170 AtomicIterator keyIter = keys();
171 AtomicValue key;
172 while ((key = keyIter.next()) != null) {
173 Sequence<?> value = get(key);
174 try {
175 if (!valueType.matches(value, th)) {
176 return false;
177 }
178 } catch (XPathException e) {
179 throw new AssertionError(e);
180 }
181 }
182 return true;
183 }
184
185 /**
186 * Get the type of the map. This method is used largely for diagnostics, to report
187 * the type of a map when it differs from the required type.
188 *
189 * @param th the type hierarchy (not used)
190 * @return the type of this map
191 */
192 @Override
193 public MapType getItemType(TypeHierarchy th) {
194 return new MapType(BuiltInAtomicType.STRING, SequenceType.NODE_SEQUENCE);
195 }
196
197 /**
198 * Get the item type of the function item
199 *
200 * @return the function item's type
201 */
202 public MapType getFunctionItemType() {
203 return new MapType(BuiltInAtomicType.STRING, SequenceType.NODE_SEQUENCE);
204 }
205
206 /**
207 * Get a description of this function for use in error messages. For named functions, the description
208 * is the function name (as a lexical QName). For others, it might be, for example, "inline function",
209 * or "partially-applied ends-with function".
210 *
211 * @return a description of the function for use in error messages
212 */
213 public String getDescription() {
214 return "range key";
215 }
216
217 /**
218 * Test whether this FunctionItem is deep-equal to another function item,
219 * under the rules of the deep-equal function
220 *
221 * @param other the other function item
222 * @param context the dynamic evaluation context
223 * @param comparer the object to perform the comparison
224 * @param flags options for how the comparison is performed
225 * @return true if the two function items are deep-equal
226 */
227 public boolean deepEquals(Function other, XPathContext context, AtomicComparer comparer, int flags) {
228 if (other instanceof RangeKey) {
229 RangeKey rk = (RangeKey) other;
230 return min.equals(rk.min) && max.equals(rk.max) && index.equals(rk.index);
231 } else {
232 return false;
233 }
234 }
235
236 /**
237 * Output information about this function item to the diagnostic explain() output
238 */
239 public void export(ExpressionPresenter out) throws XPathException {
240 out.startElement("range-key-map");
241 out.emitAttribute("size", size() + "");
242 out.endElement();
243 }
244
245 public boolean isTrustedResultType() {
246 return false;
247 }
248
249 public String toString() {
250 return MapItem.mapToString(this);
251 }
252
253 private class RangeKeyIterator implements AtomicIterator {
254
255 int pos = 0;
256 CodepointMatchKey curr = null;
257 CodepointMatchKey top;
258
259 public RangeKeyIterator() {
260 top = (CodepointMatchKey) (max == null ? index.lastKey() : index.floorKey(max));
261 }
262
263 /**
264 * Get the next item in the sequence. <BR>
265 *
266 * @return the next Item. If there are no more nodes, return null.
267 */
268 public StringValue next() {
269 if (pos <= 0) {
270 if (pos < 0) {
271 return null;
272 }
273 if (min == null) {
274 curr = (CodepointMatchKey) index.firstKey();
275 } else {
276 curr = (CodepointMatchKey) index.ceilingKey(min);
277 if(curr != null && max != null && curr.compareTo(max) > 0) {
278 curr = null;
279 }
280 }
281 } else if (curr.equals(top)) {
282 curr = null;
283 } else {
284 curr = (CodepointMatchKey) index.higherKey(curr);
285 }
286 if (curr == null) {
287 pos = -1;
288 return null;
289 } else {
290 pos++;
291 return new StringValue(curr.getValue());
292 }
293 }
294
295 }
296 }
297
298 // Copyright (c) 2012 Saxonica Limited.