38fcd3395bbcd42fc21f6e9eaa09b87b9432468d
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableUnkeyedListNodeBuilder.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.ImmutableList;
11 import com.google.common.collect.Iterables;
12 import java.util.Collection;
13 import java.util.LinkedList;
14 import java.util.List;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
21 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
23 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
24
25 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
26     private List<UnkeyedListEntryNode> value;
27     private NodeIdentifier nodeIdentifier;
28     private boolean dirty;
29
30     protected ImmutableUnkeyedListNodeBuilder() {
31         value = new LinkedList<>();
32         dirty = false;
33     }
34
35     protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
36         nodeIdentifier = node.getIdentifier();
37         // FIXME: clean this up, notably reuse unmodified lists
38         value = new LinkedList<>();
39         Iterables.addAll(value, node.body());
40         dirty = true;
41     }
42
43     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create() {
44         return new ImmutableUnkeyedListNodeBuilder();
45     }
46
47     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final int sizeHint) {
48         return new ImmutableUnkeyedListNodeBuilder();
49     }
50
51     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(
52             final UnkeyedListNode node) {
53         if (!(node instanceof ImmutableUnkeyedListNode immutableNode)) {
54             throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
55         }
56         return new ImmutableUnkeyedListNodeBuilder(immutableNode);
57     }
58
59     private void checkDirty() {
60         if (dirty) {
61             value = new LinkedList<>(value);
62             dirty = false;
63         }
64     }
65
66     @Override
67     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
68         checkDirty();
69         value.add(child);
70         return this;
71     }
72
73     @Override
74     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
75             final PathArgument key) {
76         checkDirty();
77         throw new UnsupportedOperationException("Children does not have identifiers.");
78     }
79
80     @Override
81     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(
82             final Collection<UnkeyedListEntryNode> withValue) {
83         // TODO replace or putAll ?
84         for (final UnkeyedListEntryNode node : withValue) {
85             withChild(node);
86         }
87
88         return this;
89     }
90
91     @Override
92     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
93             final NodeIdentifier withNodeIdentifier) {
94         nodeIdentifier = withNodeIdentifier;
95         return this;
96     }
97
98     @Override
99     public UnkeyedListNode build() {
100         dirty = true;
101         if (value.isEmpty()) {
102             return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
103         }
104         return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
105     }
106
107     @Override
108     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
109         return withChild(child);
110     }
111
112     @Override
113     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode>
114             removeChild(final PathArgument key) {
115         return withoutChild(key);
116     }
117
118     protected static final class EmptyImmutableUnkeyedListNode
119             extends AbstractNormalizedNode<NodeIdentifier, UnkeyedListNode> implements UnkeyedListNode {
120         protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
121             super(nodeIdentifier);
122         }
123
124         @Override
125         public ImmutableList<UnkeyedListEntryNode> body() {
126             return ImmutableList.of();
127         }
128
129         @Override
130         public UnkeyedListEntryNode childAt(final int position) {
131             throw new IndexOutOfBoundsException();
132         }
133
134         @Override
135         public int size() {
136             return 0;
137         }
138
139         @Override
140         protected Class<UnkeyedListNode> implementedType() {
141             return UnkeyedListNode.class;
142         }
143
144         @Override
145         protected int valueHashCode() {
146             return 1;
147         }
148
149         @Override
150         protected boolean valueEquals(final UnkeyedListNode other) {
151             return other.isEmpty();
152         }
153     }
154
155     protected static final class ImmutableUnkeyedListNode
156             extends AbstractImmutableNormalizedValueNode<NodeIdentifier, UnkeyedListNode,
157                 Collection<@NonNull UnkeyedListEntryNode>>
158             implements UnkeyedListNode {
159
160         private final ImmutableList<UnkeyedListEntryNode> children;
161
162         ImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier,
163                 final ImmutableList<UnkeyedListEntryNode> children) {
164             super(nodeIdentifier, children);
165             this.children = children;
166         }
167
168         @Override
169         public UnkeyedListEntryNode childAt(final int position) {
170             return children.get(position);
171         }
172
173         @Override
174         public int size() {
175             return children.size();
176         }
177
178         @Override
179         protected Class<UnkeyedListNode> implementedType() {
180             return UnkeyedListNode.class;
181         }
182
183         @Override
184         protected int valueHashCode() {
185             return children.hashCode();
186         }
187
188         @Override
189         protected boolean valueEquals(final UnkeyedListNode other) {
190             final Collection<UnkeyedListEntryNode> otherChildren;
191             if (other instanceof ImmutableUnkeyedListNode immutableOther) {
192                 otherChildren = immutableOther.children;
193             } else {
194                 otherChildren = other.body();
195             }
196             return otherChildren instanceof List ? children.equals(otherChildren)
197                 : Iterables.elementsEqual(children, otherChildren);
198         }
199     }
200 }