Debian Codegraph @

net/sf/saxon/ma/map/DictionaryMap.java

raw symbols SCIP
1 package net.sf.saxon.ma.map;
2
3 import net.sf.saxon.om.GroundedValue;
4 import net.sf.saxon.om.SequenceTool;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.tree.iter.AtomicIterator;
7 import net.sf.saxon.type.*;
8 import net.sf.saxon.value.AtomicValue;
9 import net.sf.saxon.value.Cardinality;
10 import net.sf.saxon.value.SequenceType;
11 import net.sf.saxon.value.StringValue;
12
13 import java.util.*;
14
15 /**
16 * A simple implementation of MapItem where the strings are keys, and modification is unlikely.
17 * This implementation is used in a number of cases where it can be determined that it is suitable,
18 * for example when parsing JSON input, or when creating a fixed map to use in an options argument.
19 */
20
21 public class DictionaryMap implements MapItem {
22
23 private HashMap<String, GroundedValue<?>> hashMap;
24
25 /**
26 * Create an empty dictionary, to which entries can be added using {@link #initialPut(String, GroundedValue)},
27 * provided this is done before the map is exposed to the outside world.
28 */
29
30 public DictionaryMap() {
31 hashMap = new HashMap<>();
32 }
33
34 /**
35 * During initial construction of the map, add a key-value pair
36 */
37
38 public void initialPut(String key, GroundedValue<?> value) {
39 hashMap.put(key, value);
40 }
41
42 /**
43 * Get an entry from the Map
44 *
45 * @param key the value of the key
46 * @return the value associated with the given key, or null if the key is not present in the map.
47 */
48 @Override
49 public GroundedValue<?> get(AtomicValue key) {
50 if (key instanceof StringValue) {
51 return hashMap.get(key.getStringValue());
52 } else {
53 return null;
54 }
55 }
56
57 /**
58 * Get the size of the map
59 *
60 * @return the number of keys/entries present in this map
61 */
62 @Override
63 public int size() {
64 return hashMap.size();
65 }
66
67 /**
68 * Ask whether the map is empty
69 *
70 * @return true if and only if the size of the map is zero
71 */
72 @Override
73 public boolean isEmpty() {
74 return hashMap.isEmpty();
75 }
76
77 /**
78 * Get the set of all key values in the map.
79 *
80 * @return a set containing all the key values present in the map, in unpredictable order
81 */
82 @Override
83 public AtomicIterator keys() {
84 Iterator<String> base = hashMap.keySet().iterator();
85 return () -> base.hasNext() ? new StringValue(base.next()) : null;
86 }
87
88 /**
89 * Get the set of all key-value pairs in the map
90 *
91 * @return an iterable containing all the key-value pairs
92 */
93 @Override
94 public Iterable<KeyValuePair> keyValuePairs() {
95 List<KeyValuePair> pairs = new ArrayList<>();
96 hashMap.forEach((k, v) -> pairs.add(new KeyValuePair(new StringValue(k), v)));
97 return pairs;
98 }
99
100 /**
101 * Create a new map containing the existing entries in the map plus an additional entry,
102 * without modifying the original. If there is already an entry with the specified key,
103 * this entry is replaced by the new entry.
104 *
105 * @param key the key of the new entry
106 * @param value the value associated with the new entry
107 * @return the new map containing the additional entry
108 */
109 @Override
110 public MapItem addEntry(AtomicValue key, GroundedValue<?> value) {
111 return toHashTrieMap().addEntry(key, value);
112 }
113
114 /**
115 * Remove an entry from the map
116 *
117 * @param key the key of the entry to be removed
118 * @return a new map in which the requested entry has been removed; or this map
119 * unchanged if the specified key was not present
120 */
121 @Override
122 public MapItem remove(AtomicValue key) {
123 return toHashTrieMap().remove(key);
124 }
125
126 /**
127 * Ask whether the map conforms to a given map type
128 *
129 * @param keyType the required keyType
130 * @param valueType the required valueType
131 * @param th the type hierarchy cache for the configuration
132 * @return true if the map conforms to the required type
133 */
134 @Override
135 public boolean conforms(AtomicType keyType, SequenceType valueType, TypeHierarchy th) {
136 if (isEmpty()) {
137 return true;
138 }
139 if (!(keyType == BuiltInAtomicType.STRING || keyType == BuiltInAtomicType.ANY_ATOMIC)) {
140 return false;
141 }
142 if (valueType.equals(SequenceType.ANY_SEQUENCE)) {
143 return true;
144 }
145 for (GroundedValue<?> val : hashMap.values()) {
146 try {
147 if (!valueType.matches(val, th)) {
148 return false;
149 }
150 } catch (XPathException e) {
151 throw new AssertionError(e); // cannot happen when value is grounded
152 }
153 }
154 return true;
155 }
156
157 /**
158 * Get the type of the map. This method is used largely for diagnostics, to report
159 * the type of a map when it differs from the required type.
160 *
161 * @param th the type hierarchy cache
162 * @return the type of this map
163 */
164 @Override
165 public ItemType getItemType(TypeHierarchy th) {
166 ItemType valueType = null;
167 int valueCard = 0;
168 // we need to test the entries individually
169 for (Map.Entry<String, GroundedValue<?>> entry : hashMap.entrySet()) {
170 GroundedValue<?> val = entry.getValue();
171 if (valueType == null) {
172 valueType = SequenceTool.getItemType(val, th);
173 valueCard = SequenceTool.getCardinality(val);
174 } else {
175 valueType = Type.getCommonSuperType(valueType, SequenceTool.getItemType(val, th), th);
176 valueCard = Cardinality.union(valueCard, SequenceTool.getCardinality(val));
177 }
178 }
179 if (valueType == null) {
180 // empty map
181 return MapType.EMPTY_MAP_TYPE;
182 } else {
183 return new MapType(BuiltInAtomicType.STRING, SequenceType.makeSequenceType(valueType, valueCard));
184 }
185 }
186
187 /**
188 * Get the lowest common item type of the keys in the map
189 *
190 * @return the most specific type to which all the keys belong. If the map is
191 * empty, return UType.VOID
192 */
193 @Override
194 public UType getKeyUType() {
195 return hashMap.isEmpty() ? UType.VOID : UType.STRING;
196 }
197
198 /**
199 * Convert to a HashTrieMap
200 */
201
202 private HashTrieMap toHashTrieMap() {
203 //System.err.println("Dictionary rewrite!!!!");
204 HashTrieMap target = new HashTrieMap();
205 hashMap.forEach((k, v) -> target.initialPut(new StringValue(k), v));
206 return target;
207 }
208 }
209