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