1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml.nodes; 15 16 import java.util.List; 17 import org.yaml.snakeyaml.DumperOptions; 18 import org.yaml.snakeyaml.error.Mark; 19 20 /** 21 * Represents a map. 22 * <p> 23 * A map is a collection of unsorted key-value pairs. 24 * </p> 25 */ 26 public class MappingNode extends CollectionNode<NodeTuple> { 27 28 private List<NodeTuple> value; 29 private boolean merged = false; 30 MappingNode(Tag tag, boolean resolved, List<NodeTuple> value, Mark startMark, Mark endMark, DumperOptions.FlowStyle flowStyle)31 public MappingNode(Tag tag, boolean resolved, List<NodeTuple> value, Mark startMark, Mark endMark, 32 DumperOptions.FlowStyle flowStyle) { 33 super(tag, startMark, endMark, flowStyle); 34 if (value == null) { 35 throw new NullPointerException("value in a Node is required."); 36 } 37 this.value = value; 38 this.resolved = resolved; 39 } 40 MappingNode(Tag tag, List<NodeTuple> value, DumperOptions.FlowStyle flowStyle)41 public MappingNode(Tag tag, List<NodeTuple> value, DumperOptions.FlowStyle flowStyle) { 42 this(tag, true, value, null, null, flowStyle); 43 } 44 45 /* 46 * Existed in older versions but replaced with {@link DumperOptions.FlowStyle}-based constructor. 47 * Restored in v1.22 for backwards compatibility. 48 * 49 * @deprecated Since restored in v1.22. Use {@link MappingNode#MappingNode(Tag, boolean, List, 50 * Mark, Mark, org.yaml.snakeyaml.DumperOptions.FlowStyle) }. 51 */ 52 @Deprecated MappingNode(Tag tag, boolean resolved, List<NodeTuple> value, Mark startMark, Mark endMark, Boolean flowStyle)53 public MappingNode(Tag tag, boolean resolved, List<NodeTuple> value, Mark startMark, Mark endMark, 54 Boolean flowStyle) { 55 this(tag, resolved, value, startMark, endMark, DumperOptions.FlowStyle.fromBoolean(flowStyle)); 56 } 57 58 /* 59 * Existed in older versions but replaced with {@link DumperOptions.FlowStyle}-based constructor. 60 * Restored in v1.22 for backwards compatibility. 61 * 62 * @deprecated Since restored in v1.22. Use {@link MappingNode#MappingNode(Tag, List, 63 * org.yaml.snakeyaml.DumperOptions.FlowStyle) }. 64 */ 65 @Deprecated MappingNode(Tag tag, List<NodeTuple> value, Boolean flowStyle)66 public MappingNode(Tag tag, List<NodeTuple> value, Boolean flowStyle) { 67 this(tag, value, DumperOptions.FlowStyle.fromBoolean(flowStyle)); 68 69 } 70 71 @Override getNodeId()72 public NodeId getNodeId() { 73 return NodeId.mapping; 74 } 75 76 /** 77 * Returns the entries of this map. 78 * 79 * @return List of entries. 80 */ getValue()81 public List<NodeTuple> getValue() { 82 return value; 83 } 84 setValue(List<NodeTuple> mergedValue)85 public void setValue(List<NodeTuple> mergedValue) { 86 value = mergedValue; 87 } 88 setOnlyKeyType(Class<? extends Object> keyType)89 public void setOnlyKeyType(Class<? extends Object> keyType) { 90 for (NodeTuple nodes : value) { 91 nodes.getKeyNode().setType(keyType); 92 } 93 } 94 setTypes(Class<? extends Object> keyType, Class<? extends Object> valueType)95 public void setTypes(Class<? extends Object> keyType, Class<? extends Object> valueType) { 96 for (NodeTuple nodes : value) { 97 nodes.getValueNode().setType(valueType); 98 nodes.getKeyNode().setType(keyType); 99 } 100 } 101 102 @Override toString()103 public String toString() { 104 String values; 105 StringBuilder buf = new StringBuilder(); 106 for (NodeTuple node : getValue()) { 107 buf.append("{ key="); 108 buf.append(node.getKeyNode()); 109 buf.append("; value="); 110 if (node.getValueNode() instanceof CollectionNode) { 111 // to avoid overflow in case of recursive structures 112 buf.append(System.identityHashCode(node.getValueNode())); 113 } else { 114 buf.append(node); 115 } 116 buf.append(" }"); 117 } 118 values = buf.toString(); 119 return "<" + this.getClass().getName() + " (tag=" + getTag() + ", values=" + values + ")>"; 120 } 121 122 /** 123 * @param merged - true if map contains merge node 124 */ setMerged(boolean merged)125 public void setMerged(boolean merged) { 126 this.merged = merged; 127 } 128 129 /** 130 * @return true if map contains merge node 131 */ isMerged()132 public boolean isMerged() { 133 return merged; 134 } 135 } 136