| 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.om.GroundedValue; |
| 11 | import net.sf.saxon.om.SequenceTool; |
| 12 | import net.sf.saxon.trans.XPathException; |
| 13 | import net.sf.saxon.tree.iter.AtomicIterator; |
| 14 | import net.sf.saxon.tree.iter.SingleAtomicIterator; |
| 15 | import net.sf.saxon.tree.jiter.MonoIterator; |
| 16 | import net.sf.saxon.type.AtomicType; |
| 17 | import net.sf.saxon.type.ItemType; |
| 18 | import net.sf.saxon.type.TypeHierarchy; |
| 19 | import net.sf.saxon.type.UType; |
| 20 | import net.sf.saxon.value.AtomicValue; |
| 21 | import net.sf.saxon.value.SequenceType; |
| 22 | |
| 23 | /** |
| 24 | * A key and a corresponding value to be held in a Map. A key-value pair also acts as a singleton |
| 25 | * map in its own right. |
| 26 | */ |
| 27 | |
| 28 | public class SingleEntryMap implements MapItem { |
| 29 | public AtomicValue key; |
| 30 | public GroundedValue<?> value; |
| 31 | |
| 32 | public SingleEntryMap(AtomicValue key, GroundedValue<?> value) { |
| 33 | this.key = key; |
| 34 | this.value = value; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get an entry from the Map |
| 39 | * |
| 40 | * @param key the value of the key |
| 41 | * @return the value associated with the given key, or null if the key is not present in the map. |
| 42 | */ |
| 43 | @Override |
| 44 | public GroundedValue<?> get(AtomicValue key) { |
| 45 | return this.key.asMapKey().equals(key.asMapKey()) ? value : null; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the size of the map |
| 50 | * |
| 51 | * @return the number of keys/entries present in this map |
| 52 | */ |
| 53 | @Override |
| 54 | public int size() { |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Ask whether the map is empty |
| 60 | * |
| 61 | * @return true if and only if the size of the map is zero |
| 62 | */ |
| 63 | @Override |
| 64 | public boolean isEmpty() { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the set of all key values in the map. |
| 70 | * |
| 71 | * @return a set containing all the key values present in the map, in unpredictable order |
| 72 | */ |
| 73 | @Override |
| 74 | public AtomicIterator keys() { |
| 75 | return new SingleAtomicIterator(key); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the set of all key-value pairs in the map |
| 80 | * |
| 81 | * @return an iterable containing all the key-value pairs |
| 82 | */ |
| 83 | @Override |
| 84 | public Iterable<KeyValuePair> keyValuePairs() { |
| 85 | return () -> new MonoIterator<>(new KeyValuePair(key, value)); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Create a new map containing the existing entries in the map plus an additional entry, |
| 90 | * without modifying the original. If there is already an entry with the specified key, |
| 91 | * this entry is replaced by the new entry. |
| 92 | * |
| 93 | * @param key the key of the new entry |
| 94 | * @param value the value associated with the new entry |
| 95 | * @return the new map containing the additional entry |
| 96 | */ |
| 97 | @Override |
| 98 | public MapItem addEntry(AtomicValue key, GroundedValue<?> value) { |
| 99 | return toHashTrieMap().addEntry(key, value); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Remove an entry from the map |
| 104 | * |
| 105 | * @param key the key of the entry to be removed |
| 106 | * @return a new map in which the requested entry has been removed; or this map |
| 107 | * unchanged if the specified key was not present |
| 108 | */ |
| 109 | @Override |
| 110 | public MapItem remove(AtomicValue key) { |
| 111 | return (get(key) == null) ? this : new HashTrieMap(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Ask whether the map conforms to a given map type |
| 116 | * |
| 117 | * @param keyType the required keyType |
| 118 | * @param valueType the required valueType |
| 119 | * @param th the type hierarchy cache for the configuration |
| 120 | * @return true if the map conforms to the required type |
| 121 | */ |
| 122 | @Override |
| 123 | public boolean conforms(AtomicType keyType, SequenceType valueType, TypeHierarchy th) { |
| 124 | try { |
| 125 | return keyType.matches(key, th) && valueType.matches(value, th); |
| 126 | } catch (XPathException e) { |
| 127 | throw new AssertionError(e); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the type of the map. This method is used largely for diagnostics, to report |
| 134 | * the type of a map when it differs from the required type. |
| 135 | * |
| 136 | * @param th the type hierarchy cache |
| 137 | * @return the type of this map |
| 138 | */ |
| 139 | @Override |
| 140 | public ItemType getItemType(TypeHierarchy th) { |
| 141 | return new MapType(key.getItemType(), SequenceType.makeSequenceType( |
| 142 | SequenceTool.getItemType(value, th), |
| 143 | SequenceTool.getCardinality(value))); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the lowest common item type of the keys in the map |
| 148 | * |
| 149 | * @return the most specific type to which all the keys belong. If the map is |
| 150 | * empty, return UType.VOID |
| 151 | */ |
| 152 | @Override |
| 153 | public UType getKeyUType() { |
| 154 | return key.getUType(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Convert to a HashTrieMap |
| 159 | */ |
| 160 | |
| 161 | private HashTrieMap toHashTrieMap() { |
| 162 | HashTrieMap target = new HashTrieMap(); |
| 163 | target.initialPut(key, value); |
| 164 | return target; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Copyright (c) 2010-2018 Saxonica Limited. |