f3dd9d0c9b1fb73a004e60f5af19263b8aa53195
[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.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
22
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Maps;
25
26 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
27
28     protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value = Maps.newLinkedHashMap();
29     protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
30
31     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
32         return new ImmutableLeafSetNodeBuilder<>();
33     }
34
35     @Override
36     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
37         this.value.put(child.getIdentifier(), child);
38         return this;
39     }
40
41     @Override
42     public LeafSetNode<T> build() {
43         return new ImmutableLeafSetNode<>(nodeIdentifier, value);
44     }
45
46     @Override
47     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
48             final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
49         this.nodeIdentifier = nodeIdentifier;
50         return this;
51     }
52
53     @Override
54     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final 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(final T value) {
64         return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(
65                 new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value));
66     }
67
68     private final static class ImmutableLeafSetNode<T> extends
69             AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
70             Immutable, LeafSetNode<T> {
71
72         private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
73
74         ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
75                 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
76             super(nodeIdentifier, children.values());
77             this.mappedChildren = children;
78         }
79
80         @Override
81         public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
82             return Optional.fromNullable(mappedChildren.get(child));
83         }
84
85         @Override
86         public String toString() {
87             final StringBuffer sb = new StringBuffer("ImmutableLeafSetNode{");
88             sb.append("nodeIdentifier=").append(nodeIdentifier);
89             sb.append(", children=").append(value);
90             sb.append('}');
91             return sb.toString();
92         }
93     }
94
95     @Override
96     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
97             final LeafSetEntryNode<T> child) {
98         return withChild(child);
99     }
100
101 }