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