Rework NormalizedNode type hierarchy
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableUserLeafSetNodeBuilder.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 com.google.common.collect.Iterables;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.LinkedHashMap;
15 import java.util.Map;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.util.UnmodifiableCollection;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
25
26 public class ImmutableUserLeafSetNodeBuilder<T> implements ListNodeBuilder<T, UserLeafSetNode<T>> {
27     private Map<NodeWithValue, LeafSetEntryNode<T>> value;
28     private NodeIdentifier nodeIdentifier;
29     private boolean dirty;
30
31     protected ImmutableUserLeafSetNodeBuilder() {
32         value = new LinkedHashMap<>();
33         dirty = false;
34     }
35
36     protected ImmutableUserLeafSetNodeBuilder(final ImmutableUserLeafSetNode<T> node) {
37         nodeIdentifier = node.getIdentifier();
38         value = node.getChildren();
39         dirty = true;
40     }
41
42     public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create() {
43         return new ImmutableUserLeafSetNodeBuilder<>();
44     }
45
46     public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(
47             final UserLeafSetNode<T> node) {
48         if (!(node instanceof ImmutableUserLeafSetNode<?>)) {
49             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
50         }
51
52         return new ImmutableUserLeafSetNodeBuilder<>((ImmutableUserLeafSetNode<T>) node);
53     }
54
55     private void checkDirty() {
56         if (dirty) {
57             value = new LinkedHashMap<>(value);
58             dirty = false;
59         }
60     }
61
62     @Override
63     public ImmutableUserLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
64         checkDirty();
65         this.value.put(child.getIdentifier(), child);
66         return this;
67     }
68
69     @Override
70     public ImmutableUserLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
71         checkDirty();
72         this.value.remove(key);
73         return this;
74     }
75
76     @Override
77     public UserLeafSetNode<T> build() {
78         dirty = true;
79         return new ImmutableUserLeafSetNode<>(nodeIdentifier, value);
80     }
81
82     @Override
83     public ImmutableUserLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
84         this.nodeIdentifier = withNodeIdentifier;
85         return this;
86     }
87
88     @Override
89     public ImmutableUserLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
90         checkDirty();
91         for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
92             withChild(leafSetEntry);
93         }
94         return this;
95     }
96
97     @Override
98     public ImmutableUserLeafSetNodeBuilder<T> withChildValue(final T childValue) {
99         return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
100             .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
101             .withValue(childValue).build());
102     }
103
104     protected static final class ImmutableUserLeafSetNode<T>
105             extends AbstractImmutableNormalizedNode<NodeIdentifier, UserLeafSetNode<?>>
106             implements UserLeafSetNode<T> {
107         private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
108
109         ImmutableUserLeafSetNode(final NodeIdentifier nodeIdentifier,
110                 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
111             super(nodeIdentifier);
112             this.children = children;
113         }
114
115         @Override
116         public LeafSetEntryNode<T> childByArg(final NodeWithValue child) {
117             return children.get(child);
118         }
119
120         @Override
121         public LeafSetEntryNode<T> getChild(final int position) {
122             return Iterables.get(children.values(), position);
123         }
124
125         @Override
126         public int size() {
127             return children.size();
128         }
129
130         @Override
131         public Collection<LeafSetEntryNode<T>> body() {
132             return UnmodifiableCollection.create(children.values());
133         }
134
135         @Override
136         protected Class<UserLeafSetNode<?>> implementedType() {
137             return (Class) UserLeafSetNode.class;
138         }
139
140         @Override
141         protected int valueHashCode() {
142             return children.hashCode();
143         }
144
145         @Override
146         protected boolean valueEquals(final UserLeafSetNode<?> other) {
147             return children.equals(((ImmutableUserLeafSetNode<?>) other).children);
148         }
149
150         @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
151             justification = "https://github.com/spotbugs/spotbugs/issues/811")
152         private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
153             return Collections.unmodifiableMap(children);
154         }
155     }
156
157     @Override
158     public ImmutableUserLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
159         return withChild(child);
160     }
161
162     @Override
163     public ImmutableUserLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
164         return withoutChild(key);
165     }
166 }