aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/yaml/snakeyaml/nodes/MappingNode.java
blob: f0f7a84d2d77d40a61d5623fac41190170949dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package org.yaml.snakeyaml.nodes;

import java.util.List;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.error.Mark;

/**
 * Represents a map.
 * <p>
 * A map is a collection of unsorted key-value pairs.
 * </p>
 */
public class MappingNode extends CollectionNode<NodeTuple> {

  private List<NodeTuple> value;
  private boolean merged = false;

  public MappingNode(Tag tag, boolean resolved, List<NodeTuple> value, Mark startMark, Mark endMark,
      DumperOptions.FlowStyle flowStyle) {
    super(tag, startMark, endMark, flowStyle);
    if (value == null) {
      throw new NullPointerException("value in a Node is required.");
    }
    this.value = value;
    this.resolved = resolved;
  }

  public MappingNode(Tag tag, List<NodeTuple> value, DumperOptions.FlowStyle flowStyle) {
    this(tag, true, value, null, null, flowStyle);
  }

  /*
   * Existed in older versions but replaced with {@link DumperOptions.FlowStyle}-based constructor.
   * Restored in v1.22 for backwards compatibility.
   *
   * @deprecated Since restored in v1.22. Use {@link MappingNode#MappingNode(Tag, boolean, List,
   * Mark, Mark, org.yaml.snakeyaml.DumperOptions.FlowStyle) }.
   */
  @Deprecated
  public MappingNode(Tag tag, boolean resolved, List<NodeTuple> value, Mark startMark, Mark endMark,
      Boolean flowStyle) {
    this(tag, resolved, value, startMark, endMark, DumperOptions.FlowStyle.fromBoolean(flowStyle));
  }

  /*
   * Existed in older versions but replaced with {@link DumperOptions.FlowStyle}-based constructor.
   * Restored in v1.22 for backwards compatibility.
   *
   * @deprecated Since restored in v1.22. Use {@link MappingNode#MappingNode(Tag, List,
   * org.yaml.snakeyaml.DumperOptions.FlowStyle) }.
   */
  @Deprecated
  public MappingNode(Tag tag, List<NodeTuple> value, Boolean flowStyle) {
    this(tag, value, DumperOptions.FlowStyle.fromBoolean(flowStyle));

  }

  @Override
  public NodeId getNodeId() {
    return NodeId.mapping;
  }

  /**
   * Returns the entries of this map.
   *
   * @return List of entries.
   */
  public List<NodeTuple> getValue() {
    return value;
  }

  public void setValue(List<NodeTuple> mergedValue) {
    value = mergedValue;
  }

  public void setOnlyKeyType(Class<? extends Object> keyType) {
    for (NodeTuple nodes : value) {
      nodes.getKeyNode().setType(keyType);
    }
  }

  public void setTypes(Class<? extends Object> keyType, Class<? extends Object> valueType) {
    for (NodeTuple nodes : value) {
      nodes.getValueNode().setType(valueType);
      nodes.getKeyNode().setType(keyType);
    }
  }

  @Override
  public String toString() {
    String values;
    StringBuilder buf = new StringBuilder();
    for (NodeTuple node : getValue()) {
      buf.append("{ key=");
      buf.append(node.getKeyNode());
      buf.append("; value=");
      if (node.getValueNode() instanceof CollectionNode) {
        // to avoid overflow in case of recursive structures
        buf.append(System.identityHashCode(node.getValueNode()));
      } else {
        buf.append(node);
      }
      buf.append(" }");
    }
    values = buf.toString();
    return "<" + this.getClass().getName() + " (tag=" + getTag() + ", values=" + values + ")>";
  }

  /**
   * @param merged - true if map contains merge node
   */
  public void setMerged(boolean merged) {
    this.merged = merged;
  }

  /**
   * @return true if map contains merge node
   */
  public boolean isMerged() {
    return merged;
  }
}