7b658054fba0048b67e6582c287c536a4adbca11
[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.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
25
26 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
27     private List<UnkeyedListEntryNode> value;
28     private NodeIdentifier nodeIdentifier;
29     private boolean dirty;
30
31     protected ImmutableUnkeyedListNodeBuilder() {
32         this.value = new LinkedList<>();
33         this.dirty = false;
34     }
35
36     protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
37         this.nodeIdentifier = node.getIdentifier();
38         this.value = new LinkedList<>();
39         Iterables.addAll(value, node.getValue());
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(final UnkeyedListNode node) {
52         if (!(node instanceof ImmutableUnkeyedListNode)) {
53             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
54         }
55
56         return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
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         this.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(final Collection<UnkeyedListEntryNode> value) {
82         // TODO replace or putAll ?
83         for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
84             withChild(UnkeyedListEntryNode);
85         }
86
87         return this;
88     }
89
90     @Override
91     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
92             final NodeIdentifier nodeIdentifier) {
93         this.nodeIdentifier = nodeIdentifier;
94         return this;
95     }
96
97     @Override
98     public UnkeyedListNode build() {
99         dirty = true;
100         if (value.isEmpty()) {
101             return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
102         } else {
103             return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
104         }
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> removeChild(
114             final PathArgument key) {
115         return withoutChild(key);
116     }
117
118     protected static final class EmptyImmutableUnkeyedListNode extends AbstractImmutableNormalizedNode<NodeIdentifier, Collection<UnkeyedListEntryNode>> implements Immutable, UnkeyedListNode {
119         protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
120             super(nodeIdentifier);
121         }
122
123         @Override
124         public Collection<UnkeyedListEntryNode> getValue() {
125             return Collections.emptySet();
126         }
127
128         @Override
129         public UnkeyedListEntryNode getChild(final int position) {
130             throw new IndexOutOfBoundsException();
131         }
132
133         @Override
134         public int getSize() {
135             return 0;
136         }
137
138         @Override
139         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
140             return Collections.EMPTY_LIST.equals(other.getValue());
141         }
142
143         @Override
144         protected int valueHashCode() {
145             return Collections.EMPTY_LIST.hashCode();
146         }
147     }
148
149     protected static final class ImmutableUnkeyedListNode extends
150             AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<UnkeyedListEntryNode>>
151             implements Immutable, UnkeyedListNode {
152
153         private final ImmutableList<UnkeyedListEntryNode> children;
154
155         ImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier,
156                 final ImmutableList<UnkeyedListEntryNode> children) {
157             super(nodeIdentifier, children);
158             this.children = children;
159         }
160
161         @Override
162         protected int valueHashCode() {
163             return children.hashCode();
164         }
165
166         @Override
167         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
168             return children.equals(((ImmutableUnkeyedListNode) other).children);
169         }
170
171         @Override
172         public UnkeyedListEntryNode getChild(final int position) {
173             return children.get(position);
174         }
175
176         @Override
177         public int getSize() {
178             return children.size();
179         }
180     }
181 }