Merge "Fix for Bug 495."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeBuilder.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.LeafSetEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
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 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22
23 public class ImmutableLeafSetNodeBuilder<T>
24         implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
25
26     protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
27     protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
28
29     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
30         return new ImmutableLeafSetNodeBuilder<>();
31     }
32
33     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(LeafSetEntryNode<T> child) {
34         if(this.value == null) {
35             this.value = Maps.newLinkedHashMap();
36         }
37
38         this.value.put(child.getIdentifier(), child);
39         return this;
40     }
41
42     @Override
43     public LeafSetNode<T> build() {
44         return new ImmutableLeafSetNode<>(nodeIdentifier, value);
45     }
46
47     @Override
48     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
49         this.nodeIdentifier = nodeIdentifier;
50         return this;
51     }
52
53     @Override
54     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(List<LeafSetEntryNode<T>> value) {
55         for (LeafSetEntryNode<T> leafSetEntry : value) {
56             withChild(leafSetEntry);
57         }
58
59         return this;
60     }
61
62     @Override
63     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(T value) {
64         return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value));
65     }
66
67     final class ImmutableLeafSetNode<T> extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements LeafSetNode<T> {
68
69         private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
70
71         ImmutableLeafSetNode(InstanceIdentifier.NodeIdentifier nodeIdentifier, Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
72             super(nodeIdentifier, children.values());
73             this.mappedChildren = children;
74         }
75
76         @Override
77         public Optional<LeafSetEntryNode<T>> getChild(InstanceIdentifier.NodeWithValue child) {
78             return Optional.fromNullable(mappedChildren.get(child));
79         }
80
81         @Override
82         public String toString() {
83             final StringBuffer sb = new StringBuffer("ImmutableLeafSetNode{");
84             sb.append("nodeIdentifier=").append(nodeIdentifier);
85             sb.append(", children=").append(value);
86             sb.append('}');
87             return sb.toString();
88         }
89     }
90
91 }