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