View Javadoc

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