Abstract infrastructure for normalized nodes translation.
[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.ImmutableList;
25 import com.google.common.collect.Maps;
26
27 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
28
29     protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value = Maps.newLinkedHashMap();
30     protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
31
32     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
33         return new ImmutableLeafSetNodeBuilder<>();
34     }
35
36     @Override
37     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
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(
49             final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
50         this.nodeIdentifier = nodeIdentifier;
51         return this;
52     }
53
54     @Override
55     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
56         for (LeafSetEntryNode<T> leafSetEntry : value) {
57             withChild(leafSetEntry);
58         }
59
60         return this;
61     }
62
63     @Override
64     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
65         return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(
66                 new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value));
67     }
68
69     private final static class ImmutableLeafSetNode<T> extends
70             AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
71             Immutable, LeafSetNode<T> {
72
73         private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
74
75         ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
76                 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
77             super(nodeIdentifier, ImmutableList.copyOf(children.values()));
78             this.mappedChildren = children;
79         }
80
81         @Override
82         public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
83             return Optional.fromNullable(mappedChildren.get(child));
84         }
85
86     }
87
88     @Override
89     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
90             final LeafSetEntryNode<T> child) {
91         return withChild(child);
92     }
93
94 }