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