Merge "Fix for Bug 511 (yang)"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapNodeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
9
10 import java.util.List;
11 import java.util.Map;
12
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
17 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
18
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Maps;
21
22 public class ImmutableMapNodeBuilder
23         implements CollectionNodeBuilder<MapEntryNode, MapNode> {
24
25     protected Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value;
26     protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
27
28     public static CollectionNodeBuilder<MapEntryNode, MapNode> create() {
29         return new ImmutableMapNodeBuilder();
30     }
31
32     public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(MapEntryNode child) {
33         if(this.value == null) {
34             this.value = Maps.newLinkedHashMap();
35         }
36
37         this.value.put(child.getIdentifier(), child);
38         return this;
39     }
40
41     @Override
42     public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(List<MapEntryNode> value) {
43         // TODO replace or putAll ?
44         for (MapEntryNode mapEntryNode : value) {
45             withChild(mapEntryNode);
46         }
47
48         return this;
49     }
50
51     public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
52         this.nodeIdentifier = nodeIdentifier;
53         return this;
54     }
55
56     @Override
57     public MapNode build() {
58         return new ImmutableMapNode(nodeIdentifier, value);
59     }
60
61     static final class ImmutableMapNode extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements MapNode {
62
63         private final Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mappedChildren;
64
65         ImmutableMapNode(InstanceIdentifier.NodeIdentifier nodeIdentifier,
66                          Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children) {
67             super(nodeIdentifier, children.values());
68             this.mappedChildren = children;
69         }
70
71         @Override
72         public Optional<MapEntryNode> getChild(InstanceIdentifier.NodeIdentifierWithPredicates child) {
73             return Optional.fromNullable(mappedChildren.get(child));
74         }
75
76         @Override
77         public String toString() {
78             final StringBuffer sb = new StringBuffer("ImmutableMapNode{");
79             sb.append("nodeIdentifier=").append(nodeIdentifier);
80             sb.append(", children=").append(mappedChildren);
81             sb.append('}');
82             return sb.toString();
83         }
84     }
85 }