DataContainerNodeBuilder should take a Collection
[yangtools.git] / yang / 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.Collections;
14 import java.util.LinkedList;
15 import java.util.List;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
26
27 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
28     private List<UnkeyedListEntryNode> value;
29     private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
30     private boolean dirty;
31
32     protected ImmutableUnkeyedListNodeBuilder() {
33         this.value = new LinkedList<>();
34         this.dirty = false;
35     }
36
37     protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
38         this.nodeIdentifier = node.getIdentifier();
39         this.value = new LinkedList<>();
40         Iterables.addAll(value, node.getValue());
41         this.dirty = true;
42     }
43
44     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create() {
45         return new ImmutableUnkeyedListNodeBuilder();
46     }
47
48     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final int sizeHint) {
49         return new ImmutableUnkeyedListNodeBuilder();
50     }
51
52     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(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 YangInstanceIdentifier.PathArgument key) {
77         checkDirty();
78         throw new UnsupportedOperationException("Children does not have identifiers.");
79     }
80
81     @Override
82     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final Collection<UnkeyedListEntryNode> value) {
83         // TODO replace or putAll ?
84         for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
85             withChild(UnkeyedListEntryNode);
86         }
87
88         return this;
89     }
90
91     @Override
92     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
93             final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
94         this.nodeIdentifier = nodeIdentifier;
95         return this;
96     }
97
98     @Override
99     public UnkeyedListNode build() {
100         dirty = true;
101         if (value.isEmpty()) {
102             return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
103         } else {
104             return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
105         }
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> removeChild(
115             final PathArgument key) {
116         return withoutChild(key);
117     }
118
119     protected static final class EmptyImmutableUnkeyedListNode extends AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>> implements Immutable, UnkeyedListNode {
120         protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
121             super(nodeIdentifier);
122         }
123
124         @Override
125         public Iterable<UnkeyedListEntryNode> getValue() {
126             return Collections.emptyList();
127         }
128
129         @Override
130         public UnkeyedListEntryNode getChild(final int position) {
131             throw new IndexOutOfBoundsException();
132         }
133
134         @Override
135         public int getSize() {
136             return 0;
137         }
138
139         @Override
140         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
141             return Collections.EMPTY_LIST.equals(other.getValue());
142         }
143
144         @Override
145         protected int valueHashCode() {
146             return Collections.EMPTY_LIST.hashCode();
147         }
148     }
149
150     protected static final class ImmutableUnkeyedListNode extends
151             AbstractImmutableNormalizedValueNode<YangInstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>>
152             implements Immutable, UnkeyedListNode {
153
154         private final ImmutableList<UnkeyedListEntryNode> children;
155
156         ImmutableUnkeyedListNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
157                 final ImmutableList<UnkeyedListEntryNode> children) {
158             super(nodeIdentifier, children);
159             this.children = children;
160         }
161
162         @Override
163         protected int valueHashCode() {
164             return children.hashCode();
165         }
166
167         @Override
168         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
169             return children.equals(((ImmutableUnkeyedListNode) other).children);
170         }
171
172         @Override
173         public UnkeyedListEntryNode getChild(final int position) {
174             return children.get(position);
175         }
176
177         @Override
178         public int getSize() {
179             return children.size();
180         }
181     }
182 }