| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.ma.map.HashTrieMap; |
| 11 | import net.sf.saxon.ma.map.KeyValuePair; |
| 12 | import net.sf.saxon.ma.map.MapItem; |
| 13 | import net.sf.saxon.om.GroundedValue; |
| 14 | import net.sf.saxon.tree.jiter.MappingJavaIterator; |
| 15 | import net.sf.saxon.value.DoubleValue; |
| 16 | import net.sf.saxon.value.Int64Value; |
| 17 | import net.sf.saxon.value.StringValue; |
| 18 | |
| 19 | import java.util.*; |
| 20 | |
| 21 | /** |
| 22 | * A map in the XDM data model. A map is a list of zero or more entries, each of which |
| 23 | * is a pair comprising a key (which is an atomic value) and a value (which is an arbitrary value). |
| 24 | * The map itself is an XDM item. |
| 25 | * <p>An XdmMap is immutable.</p> |
| 26 | * <p>As originally issued in Saxon 9.8, this class implemented the {@link java.util.Map} interface. |
| 27 | * It no longer does so, because it was found that the methods {@link #size()}, {@link #put(XdmAtomicValue, XdmValue)}, |
| 28 | * and {@link #remove} did not adhere to the semantic contract of that interface. Instead, it is now |
| 29 | * possible to obtain a view of this object as an immutable Java {@link java.util.Map} by calling the |
| 30 | * method {@link #asImmutableMap()}. See bug 3824.</p> |
| 31 | * |
| 32 | * @since 9.8 |
| 33 | */ |
| 34 | |
| 35 | public class XdmMap extends XdmFunctionItem { |
| 36 | |
| 37 | /** |
| 38 | * Create an empty XdmMap |
| 39 | */ |
| 40 | |
| 41 | public XdmMap() { |
| 42 | setValue(new HashTrieMap()); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create an XdmMap whose underlying value is a MapItem |
| 47 | * @param map the MapItem to be encapsulated |
| 48 | */ |
| 49 | |
| 50 | public XdmMap(MapItem map) { |
| 51 | setValue(map); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create an XdmMap supplying the entries in the form of a Java Map, |
| 56 | * where the keys and values in the Java Map are XDM values |
| 57 | * |
| 58 | * @param map a Java map whose entries are the (key, value) pairs |
| 59 | * @since 9.8 |
| 60 | */ |
| 61 | |
| 62 | public XdmMap(Map<? extends XdmAtomicValue, ? extends XdmValue> map) { |
| 63 | HashTrieMap val = new HashTrieMap(); |
| 64 | for (Map.Entry<? extends XdmAtomicValue, ? extends XdmValue> entry : map.entrySet()) { |
| 65 | val.initialPut(entry.getKey().getUnderlyingValue(), entry.getValue().getUnderlyingValue()); |
| 66 | } |
| 67 | setValue(val); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the underlying implementation object representing the value. This method allows |
| 72 | * access to lower-level Saxon functionality, including classes and methods that offer |
| 73 | * no guarantee of stability across releases. |
| 74 | * |
| 75 | * @return the underlying implementation object representing the value |
| 76 | * @since 9.8 (previously inherited from XdmValue which returns a Sequence) |
| 77 | */ |
| 78 | @Override |
| 79 | public MapItem getUnderlyingValue() { |
| 80 | return (MapItem)super.getUnderlyingValue(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the number of entries in the map |
| 85 | * |
| 86 | * @return the number of entries in the map. (Note that the {@link #size()} method returns 1 (one), |
| 87 | * because an XDM map is an item.) |
| 88 | */ |
| 89 | |
| 90 | public int mapSize() { |
| 91 | return getUnderlyingValue().size(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Create a new map containing an additional (key, value) pair. |
| 96 | * If there is an existing entry with the same key, it is removed |
| 97 | * @return a new map containing the additional entry. The original map is unchanged. |
| 98 | */ |
| 99 | |
| 100 | public XdmMap put(XdmAtomicValue key, XdmValue value) { |
| 101 | XdmMap map2 = new XdmMap(); |
| 102 | map2.setValue(getUnderlyingValue().addEntry(key.getUnderlyingValue(), value.getUnderlyingValue())); |
| 103 | return map2; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Create a new map in which the entry for a given key has been removed. |
| 108 | * If there is no entry with the same key, the new map has the same content as the old (it may or may not |
| 109 | * be the same Java object) |
| 110 | * |
| 111 | * @return a map without the specified entry. The original map is unchanged. |
| 112 | */ |
| 113 | |
| 114 | public XdmMap remove(XdmAtomicValue key) { |
| 115 | XdmMap map2 = new XdmMap(); |
| 116 | map2.setValue(getUnderlyingValue().remove(key.getUnderlyingValue())); |
| 117 | return map2; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the keys present in the map in the form of an unordered set. |
| 122 | * @return an unordered set of the keys present in this map, in arbitrary order. |
| 123 | */ |
| 124 | public Set<XdmAtomicValue> keySet() { |
| 125 | return new AbstractSet<XdmAtomicValue>() { |
| 126 | @Override |
| 127 | public Iterator<XdmAtomicValue> iterator() { |
| 128 | return new MappingJavaIterator<>( |
| 129 | getUnderlyingValue().keyValuePairs().iterator(), |
| 130 | kvp -> (XdmAtomicValue) XdmValue.wrap(kvp.key)); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int size() { |
| 135 | return getUnderlyingValue().size(); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public boolean contains(Object o) { |
| 140 | // Intentionally throws ClassCastException if o is not an XdmAtomicValue |
| 141 | return getUnderlyingValue().get(((XdmAtomicValue)o).getUnderlyingValue()) != null; |
| 142 | } |
| 143 | }; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Return this map as an immutable instance of {@link java.util.Map} |
| 148 | * @return an immutable instance of {@link java.util.Map} backed by this map. |
| 149 | * Methods such as {@link #remove} and {@link #put} applied to the result will |
| 150 | * always throw {@code UnsupportedOperationException}. |
| 151 | */ |
| 152 | |
| 153 | public Map<XdmAtomicValue, XdmValue> asImmutableMap() { |
| 154 | // See bug 3824 |
| 155 | XdmMap base = this; |
| 156 | return new AbstractMap<XdmAtomicValue, XdmValue>() { |
| 157 | @Override |
| 158 | public Set<Entry<XdmAtomicValue, XdmValue>> entrySet() { |
| 159 | return base.entrySet(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public int size() { |
| 164 | return base.mapSize(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public boolean isEmpty() { |
| 169 | return base.isEmpty(); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public boolean containsKey(Object key) { |
| 174 | return key instanceof XdmAtomicValue && base.containsKey((XdmAtomicValue) key); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public XdmValue get(Object key) { |
| 179 | return key instanceof XdmAtomicValue ? base.get((XdmAtomicValue)key) : null; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public XdmValue put(XdmAtomicValue key, XdmValue value) { |
| 184 | throw new UnsupportedOperationException(); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public XdmValue remove(Object key) { |
| 189 | throw new UnsupportedOperationException(); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void putAll(Map<? extends XdmAtomicValue, ? extends XdmValue> m) { |
| 194 | throw new UnsupportedOperationException(); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void clear() { |
| 199 | throw new UnsupportedOperationException(); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public Set<XdmAtomicValue> keySet() { |
| 204 | return base.keySet(); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public Collection<XdmValue> values() { |
| 209 | return base.values(); |
| 210 | } |
| 211 | }; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Return a mutable Java Map containing the same entries as this map. |
| 216 | * |
| 217 | * @return a mutable Map from atomic values to (sequence) values, containing the |
| 218 | * same entries as this map |
| 219 | * @since 9.6. |
| 220 | */ |
| 221 | |
| 222 | public Map<XdmAtomicValue, XdmValue> asMap() { |
| 223 | return new HashMap<>(asImmutableMap()); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Removes all of the mappings from this map (optional operation). |
| 228 | * The map will be empty after this call returns. |
| 229 | * |
| 230 | * @throws UnsupportedOperationException if the <tt>clear</tt> operation |
| 231 | * is not supported by this map |
| 232 | */ |
| 233 | public void clear() { |
| 234 | throw new UnsupportedOperationException("XdmMap is immutable"); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns <tt>true</tt> if this map contains no key-value mappings. |
| 239 | * |
| 240 | * @return <tt>true</tt> if this map contains no key-value mappings |
| 241 | */ |
| 242 | public boolean isEmpty() { |
| 243 | return getUnderlyingValue().isEmpty(); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Returns <tt>true</tt> if this map contains a mapping for the specified |
| 248 | * key. More formally, returns <tt>true</tt> if and only if |
| 249 | * this map contains a mapping for a key <tt>k</tt> such that |
| 250 | * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be |
| 251 | * at most one such mapping.) |
| 252 | * |
| 253 | * @param key key whose presence in this map is to be tested |
| 254 | * @return <tt>true</tt> if this map contains a mapping for the specified key |
| 255 | * @since 9.8. Changed the method signature in 9.9.1.1 to match the implementation: see bug 3969. |
| 256 | */ |
| 257 | public boolean containsKey(XdmAtomicValue key) { |
| 258 | return getUnderlyingValue().get(key.getUnderlyingValue()) != null; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the value to which the specified key is mapped, |
| 263 | * or {@code null} if this map contains no mapping for the key. |
| 264 | * |
| 265 | * @param key the key whose associated value is to be returned. |
| 266 | * @return the value to which the specified key is mapped, or |
| 267 | * {@code null} if this map contains no mapping for the key |
| 268 | * @throws ClassCastException if the supplied key cannot be converted to an XdmAtomicValue |
| 269 | * @throws NullPointerException if the supplied key is null |
| 270 | */ |
| 271 | public XdmValue get(XdmAtomicValue key) { |
| 272 | if (key == null) { |
| 273 | throw new NullPointerException(); |
| 274 | } |
| 275 | GroundedValue<?> v = getUnderlyingValue().get(key.getUnderlyingValue()); |
| 276 | return v == null ? null : XdmValue.wrap(v); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Returns the value to which the specified string-valued key is mapped, |
| 281 | * or {@code null} if this map contains no mapping for the key. This is a convenience |
| 282 | * method to save the trouble of converting the String to an <code>XdmAtomicValue</code>. |
| 283 | * |
| 284 | * @param key the key whose associated value is to be returned. This is treated |
| 285 | * as an instance of <code>xs:string</code> (which will also match |
| 286 | * entries whose key is <code>xs:untypedAtomic</code> or <code>xs:anyURI</code>) |
| 287 | * @return the value to which the specified key is mapped, or |
| 288 | * {@code null} if this map contains no mapping for the key |
| 289 | * @throws NullPointerException if the supplied key is null |
| 290 | */ |
| 291 | public XdmValue get(String key) { |
| 292 | if (key == null) { |
| 293 | throw new NullPointerException(); |
| 294 | } |
| 295 | GroundedValue<?> v = getUnderlyingValue().get(new StringValue(key)); |
| 296 | return v == null ? null : XdmValue.wrap(v); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns the value to which the specified integer-valued key is mapped, |
| 301 | * or {@code null} if this map contains no mapping for the key. This is a convenience |
| 302 | * method to save the trouble of converting the integer to an <code>XdmAtomicValue</code>. |
| 303 | * |
| 304 | * @param key the key whose associated value is to be returned. This is treated |
| 305 | * as an instance of <code>xs:integer</code> (which will also match |
| 306 | * entries whose key belongs to another numeric type) |
| 307 | * @return the value to which the specified key is mapped, or |
| 308 | * {@code null} if this map contains no mapping for the key |
| 309 | * @throws NullPointerException if the supplied key is null |
| 310 | */ |
| 311 | public XdmValue get(long key) { |
| 312 | GroundedValue<?> v = getUnderlyingValue().get(new Int64Value(key)); |
| 313 | return v == null ? null : XdmValue.wrap(v); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Returns the value to which the specified double-valued key is mapped, |
| 318 | * or {@code null} if this map contains no mapping for the key. This is a convenience |
| 319 | * method to save the trouble of converting the double to an <code>XdmAtomicValue</code>. |
| 320 | * |
| 321 | * @param key the key whose associated value is to be returned. This is treated |
| 322 | * as an instance of <code>xs:double</code> (which will also match |
| 323 | * entries whose key belongs to another numeric type) |
| 324 | * @return the value to which the specified key is mapped, or |
| 325 | * {@code null} if this map contains no mapping for the key |
| 326 | * @throws NullPointerException if the supplied key is null |
| 327 | */ |
| 328 | |
| 329 | public XdmValue get(double key) { |
| 330 | GroundedValue<?> v = getUnderlyingValue().get(new DoubleValue(key)); |
| 331 | return v == null ? null : XdmValue.wrap(v); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Returns a {@link Collection} containing the values found in this map, |
| 336 | * that is, the value parts of the key-value pairs. |
| 337 | * |
| 338 | * @return a collection containing the values found in this map. The result |
| 339 | * may contain duplicates, and the ordering of the collection is unpredictable. |
| 340 | */ |
| 341 | public Collection<XdmValue> values() { |
| 342 | List<XdmValue> result = new ArrayList<>(); |
| 343 | for (KeyValuePair keyValuePair : getUnderlyingValue().keyValuePairs()) { |
| 344 | result.add(XdmValue.wrap(keyValuePair.value)); |
| 345 | } |
| 346 | return result; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Returns a {@link Set} of the key-value pairs contained in this map. |
| 351 | * |
| 352 | * @return a set of the mappings contained in this map |
| 353 | */ |
| 354 | public Set<Map.Entry<XdmAtomicValue, XdmValue>> entrySet() { |
| 355 | Set<Map.Entry<XdmAtomicValue, XdmValue>> result = new HashSet<>(); |
| 356 | for (KeyValuePair keyValuePair : getUnderlyingValue().keyValuePairs()) { |
| 357 | result.add(new XdmMapEntry(keyValuePair)); |
| 358 | } |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Static factory method to construct an XDM map by converting each entry |
| 364 | * in a supplied Java map. The keys in the Java map must be convertible |
| 365 | * to XDM atomic values using the {@link XdmAtomicValue#makeAtomicValue(Object)} |
| 366 | * method. The associated values must be convertible to XDM sequences |
| 367 | * using the {@link XdmValue#makeValue(Object)} method. |
| 368 | * @param input the supplied map |
| 369 | * @return the resulting XdmMap |
| 370 | * @throws IllegalArgumentException if any value in the input map cannot be converted |
| 371 | * to a corresponding XDM value. |
| 372 | */ |
| 373 | |
| 374 | public static XdmMap makeMap(Map input) throws IllegalArgumentException { |
| 375 | HashTrieMap result = new HashTrieMap(); |
| 376 | for (Object entry : input.entrySet()) { |
| 377 | Object key = ((Map.Entry)entry).getKey(); |
| 378 | Object value = ((Map.Entry)entry).getValue(); |
| 379 | XdmAtomicValue xKey = XdmAtomicValue.makeAtomicValue(key); |
| 380 | XdmValue xValue = XdmValue.makeValue(value); |
| 381 | result.initialPut(xKey.getUnderlyingValue(), xValue.getUnderlyingValue()); |
| 382 | } |
| 383 | return new XdmMap(result); |
| 384 | } |
| 385 | |
| 386 | private static class XdmMapEntry implements Map.Entry<XdmAtomicValue, XdmValue> { |
| 387 | |
| 388 | KeyValuePair pair; |
| 389 | |
| 390 | public XdmMapEntry(KeyValuePair pair) { |
| 391 | this.pair = pair; |
| 392 | } |
| 393 | /** |
| 394 | * Returns the key corresponding to this entry. |
| 395 | * |
| 396 | * @return the key corresponding to this entry |
| 397 | * @throws IllegalStateException implementations may, but are not |
| 398 | * required to, throw this exception if the entry has been |
| 399 | * removed from the backing map. |
| 400 | */ |
| 401 | @Override |
| 402 | public XdmAtomicValue getKey() { |
| 403 | return (XdmAtomicValue)XdmValue.wrap(pair.key); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Returns the value corresponding to this entry. If the mapping |
| 408 | * has been removed from the backing map (by the iterator's |
| 409 | * <tt>remove</tt> operation), the results of this call are undefined. |
| 410 | * |
| 411 | * @return the value corresponding to this entry |
| 412 | * @throws IllegalStateException implementations may, but are not |
| 413 | * required to, throw this exception if the entry has been |
| 414 | * removed from the backing map. |
| 415 | */ |
| 416 | @Override |
| 417 | public XdmValue getValue() { |
| 418 | return XdmValue.wrap(pair.value); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Replaces the value corresponding to this entry with the specified |
| 423 | * value (optional operation). (Writes through to the map.) The |
| 424 | * behavior of this call is undefined if the mapping has already been |
| 425 | * removed from the map (by the iterator's <tt>remove</tt> operation). |
| 426 | * |
| 427 | * @param value new value to be stored in this entry |
| 428 | * @return old value corresponding to the entry |
| 429 | * @throws UnsupportedOperationException if the <tt>put</tt> operation |
| 430 | * is not supported by the backing map |
| 431 | * @throws ClassCastException if the class of the specified value |
| 432 | * prevents it from being stored in the backing map |
| 433 | * @throws NullPointerException if the backing map does not permit |
| 434 | * null values, and the specified value is null |
| 435 | * @throws IllegalArgumentException if some property of this value |
| 436 | * prevents it from being stored in the backing map |
| 437 | * @throws IllegalStateException implementations may, but are not |
| 438 | * required to, throw this exception if the entry has been |
| 439 | * removed from the backing map. |
| 440 | */ |
| 441 | @Override |
| 442 | public XdmValue setValue(XdmValue value) { |
| 443 | throw new UnsupportedOperationException(); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 |