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