A map expression, for map creation syntax, e.g. {foo: bar}.
| 18 | * A map expression, for map creation syntax, e.g. {@code {foo: bar}}. |
| 19 | */ |
| 20 | public class MapNode extends Node { |
| 21 | private final Map<String, Node> entries; |
| 22 | |
| 23 | /** |
| 24 | * Creates a map expression. |
| 25 | * |
| 26 | * @see Ast#map() |
| 27 | */ |
| 28 | public MapNode(Map<String, Node> entries) { |
| 29 | Assert.check(entries.getClass() != HashMap.class, |
| 30 | "HashMap given to MapNode, order will be impure!"); |
| 31 | this.entries = entries; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Gets the entries of this map expression. |
| 36 | */ |
| 37 | public Map<String, Node> getEntries() { |
| 38 | return entries; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Gets the value associated with the given key, or null if no value was associated. |
| 43 | */ |
| 44 | @Nullable |
| 45 | public Node get(String key) { |
| 46 | return this.entries.get(key); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Sets the value associated with the given key, returning the previous value if present. |
| 51 | */ |
| 52 | @Nullable |
| 53 | public Node put(String key, Node value) { |
| 54 | return this.entries.put(key, value); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void render(Renderer renderer, StringBuilder builder, int currentIndentationMultiplier) { |
| 59 | builder.append('{'); |
| 60 | List<Map.Entry<String, Node>> list = new LinkedList<>(); |
| 61 | entries.entrySet().forEach(list::add); |
| 62 | for (int i = 0; i < list.size(); i++) { |
| 63 | renderer.indent(builder, currentIndentationMultiplier); |
| 64 | String key = list.get(i).getKey(); |
| 65 | builder.append(RenderUtil.quotifyIdentifierIfNeeded(key, '"')); |
| 66 | builder.append(": "); |
| 67 | list.get(i).getValue().render(renderer, builder, currentIndentationMultiplier + 1); |
| 68 | if (i < entries.size() - 1 || renderer.hasTrailingCommas()) { |
| 69 | builder.append(", "); |
| 70 | } |
| 71 | } |
| 72 | if (list.size() > 0) { |
| 73 | renderer.indent(builder, currentIndentationMultiplier - 1); |
| 74 | } |
| 75 | builder.append('}'); |
| 76 | } |
| 77 |
nothing calls this directly
no outgoing calls
no test coverage detected