View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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  
17  package org.yaml.snakeyaml.nodes;
18  
19  import java.util.List;
20  
21  import org.yaml.snakeyaml.error.Mark;
22  
23  /**
24   * Represents a map.
25   * <p>
26   * A map is a collection of unsorted key-value pairs.
27   * </p>
28   */
29  public class MappingNode extends CollectionNode {
30      private List<NodeTuple> value;
31      private boolean merged = false;
32  
33      public MappingNode(Tag tag, boolean resolved, List<NodeTuple> value, Mark startMark,
34              Mark endMark, Boolean flowStyle) {
35          super(tag, startMark, endMark, flowStyle);
36          if (value == null) {
37              throw new NullPointerException("value in a Node is required.");
38          }
39          this.value = value;
40          this.resolved = resolved;
41      }
42  
43      public MappingNode(Tag tag, List<NodeTuple> value, Boolean flowStyle) {
44          this(tag, true, value, null, null, flowStyle);
45      }
46  
47      @Override
48      public NodeId getNodeId() {
49          return NodeId.mapping;
50      }
51  
52      /**
53       * Returns the entries of this map.
54       * 
55       * @return List of entries.
56       */
57      public List<NodeTuple> getValue() {
58          return value;
59      }
60  
61      public void setValue(List<NodeTuple> merge) {
62          value = merge;
63      }
64  
65      public void setOnlyKeyType(Class<? extends Object> keyType) {
66          for (NodeTuple nodes : value) {
67              nodes.getKeyNode().setType(keyType);
68          }
69      }
70  
71      public void setTypes(Class<? extends Object> keyType, Class<? extends Object> valueType) {
72          for (NodeTuple nodes : value) {
73              nodes.getValueNode().setType(valueType);
74              nodes.getKeyNode().setType(keyType);
75          }
76      }
77  
78      @Override
79      public String toString() {
80          String values;
81          StringBuilder buf = new StringBuilder();
82          for (NodeTuple node : getValue()) {
83              buf.append("{ key=");
84              buf.append(node.getKeyNode());
85              buf.append("; value=");
86              if (node.getValueNode() instanceof CollectionNode) {
87                  // to avoid overflow in case of recursive structures
88                  buf.append(System.identityHashCode(node.getValueNode()));
89              } else {
90                  buf.append(node.toString());
91              }
92              buf.append(" }");
93          }
94          values = buf.toString();
95          return "<" + this.getClass().getName() + " (tag=" + getTag() + ", values=" + values + ")>";
96      }
97  
98      /**
99       * @param merged
100      *            - true if map contains merge node
101      */
102     public void setMerged(boolean merged) {
103         this.merged = merged;
104     }
105 
106     /**
107      * @return true if map contains merge node
108      */
109     public boolean isMerged() {
110         return merged;
111     }
112 }