Populate data/ hierarchy
[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         this.value = new LinkedList<>();
32         this.dirty = false;
33     }
34
35     protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
36         this.nodeIdentifier = node.getIdentifier();
37         // FIXME: clean this up, notably reuse unmodified lists
38         this.value = new LinkedList<>();
39         Iterables.addAll(value, node.body());
40         this.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)) {
54             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
55         }
56
57         return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
58     }
59
60     private void checkDirty() {
61         if (dirty) {
62             value = new LinkedList<>(value);
63             dirty = false;
64         }
65     }
66
67     @Override
68     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
69         checkDirty();
70         this.value.add(child);
71         return this;
72     }
73
74     @Override
75     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
76             final PathArgument key) {
77         checkDirty();
78         throw new UnsupportedOperationException("Children does not have identifiers.");
79     }
80
81     @Override
82     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(
83             final Collection<UnkeyedListEntryNode> withValue) {
84         // TODO replace or putAll ?
85         for (final UnkeyedListEntryNode node : withValue) {
86             withChild(node);
87         }
88
89         return this;
90     }
91
92     @Override
93     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
94             final NodeIdentifier withNodeIdentifier) {
95         this.nodeIdentifier = withNodeIdentifier;
96         return this;
97     }
98
99     @Override
100     public UnkeyedListNode build() {
101         dirty = true;
102         if (value.isEmpty()) {
103             return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
104         }
105         return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
106     }
107
108     @Override
109     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
110         return withChild(child);
111     }
112
113     @Override
114     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode>
115             removeChild(final PathArgument key) {
116         return withoutChild(key);
117     }
118
119     protected static final class EmptyImmutableUnkeyedListNode
120             extends AbstractNormalizedNode<NodeIdentifier, UnkeyedListNode> implements UnkeyedListNode {
121         protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
122             super(nodeIdentifier);
123         }
124
125         @Override
126         public ImmutableList<UnkeyedListEntryNode> body() {
127             return ImmutableList.of();
128         }
129
130         @Override
131         public UnkeyedListEntryNode childAt(final int position) {
132             throw new IndexOutOfBoundsException();
133         }
134
135         @Override
136         public int size() {
137             return 0;
138         }
139
140         @Override
141         protected Class<UnkeyedListNode> implementedType() {
142             return UnkeyedListNode.class;
143         }
144
145         @Override
146         protected int valueHashCode() {
147             return 1;
148         }
149
150         @Override
151         protected boolean valueEquals(final UnkeyedListNode other) {
152             return other.isEmpty();
153         }
154     }
155
156     protected static final class ImmutableUnkeyedListNode
157             extends AbstractImmutableNormalizedValueNode<NodeIdentifier, UnkeyedListNode,
158                 Collection<@NonNull UnkeyedListEntryNode>>
159             implements UnkeyedListNode {
160
161         private final ImmutableList<UnkeyedListEntryNode> children;
162
163         ImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier,
164                 final ImmutableList<UnkeyedListEntryNode> children) {
165             super(nodeIdentifier, children);
166             this.children = children;
167         }
168
169         @Override
170         public UnkeyedListEntryNode childAt(final int position) {
171             return children.get(position);
172         }
173
174         @Override
175         public int size() {
176             return children.size();
177         }
178
179         @Override
180         protected Class<UnkeyedListNode> implementedType() {
181             return UnkeyedListNode.class;
182         }
183
184         @Override
185         protected int valueHashCode() {
186             return children.hashCode();
187         }
188
189         @Override
190         protected boolean valueEquals(final UnkeyedListNode other) {
191             final Collection<UnkeyedListEntryNode> otherChildren;
192             if (other instanceof ImmutableUnkeyedListNode) {
193                 otherChildren = ((ImmutableUnkeyedListNode) other).children;
194             } else {
195                 otherChildren = other.body();
196             }
197             return otherChildren instanceof List ? children.equals(otherChildren)
198                 : Iterables.elementsEqual(children, otherChildren);
199         }
200     }
201 }