ad4fb5a0b6daf3157d069ff250c71818da7e5307
[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.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.util.MapAdaptor;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26
27 import com.google.common.base.Optional;
28 import com.google.common.collect.Iterables;
29
30 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
31
32     private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
33     private InstanceIdentifier.NodeIdentifier nodeIdentifier;
34
35     protected ImmutableLeafSetNodeBuilder() {
36         value = new HashMap<>();
37     }
38
39     protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
40         nodeIdentifier = node.getIdentifier();
41         value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
42     }
43
44     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
45         return new ImmutableLeafSetNodeBuilder<>();
46     }
47
48     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
49         if (!(node instanceof ImmutableLeafSetNode<?>)) {
50             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
51         }
52
53         return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
54     }
55
56     @Override
57     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
58         this.value.put(child.getIdentifier(), child);
59         return this;
60     }
61
62     @Override
63     public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
64         this.value.remove(key);
65         return this;
66     }
67
68     @Override
69     public LeafSetNode<T> build() {
70         return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
71     }
72
73     @Override
74     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
75             final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
76         this.nodeIdentifier = nodeIdentifier;
77         return this;
78     }
79
80     @Override
81     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
82         for (final LeafSetEntryNode<T> leafSetEntry : value) {
83             withChild(leafSetEntry);
84         }
85         return this;
86     }
87
88
89     @Override
90     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
91         final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
92         b.withNodeIdentifier(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
93         b.withValue(value);
94         b.withAttributes(attributes);
95         return withChild(b.build());
96     }
97
98     @Override
99     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
100         return withChildValue(value, Collections.<QName,String>emptyMap());
101     }
102
103     protected final static class ImmutableLeafSetNode<T> extends
104             AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
105             Immutable, LeafSetNode<T> {
106
107         private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
108
109         ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
110                 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
111             super(nodeIdentifier, Iterables.unmodifiableIterable(children.values()));
112             this.children = children;
113         }
114
115         @Override
116         public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
117             return Optional.fromNullable(children.get(child));
118         }
119
120         @Override
121         protected int valueHashCode() {
122             return children.hashCode();
123         }
124
125         @Override
126         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
127             return children.equals(((ImmutableLeafSetNode<?>) other).children);
128         }
129     }
130
131     @Override
132     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
133             final LeafSetEntryNode<T> child) {
134         return withChild(child);
135     }
136
137     @Override
138     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
139             final PathArgument key) {
140         return withoutChild(key);
141     }
142
143 }