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