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