274b781d3e66fded64bdf5485dada1f47561c20f
[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 java.util.LinkedList;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
23
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.Iterables;
26
27 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
28
29     private List<UnkeyedListEntryNode> value;
30     private InstanceIdentifier.NodeIdentifier nodeIdentifier;
31     private boolean dirty = false;
32
33     protected ImmutableUnkeyedListNodeBuilder() {
34         this.value = new LinkedList<>();
35         this.dirty = false;
36     }
37
38     protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
39         this.nodeIdentifier = node.getIdentifier();
40         this.value = new LinkedList<>();
41         Iterables.addAll(value, node.getValue());
42         this.dirty = true;
43     }
44
45     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create() {
46         return new ImmutableUnkeyedListNodeBuilder();
47     }
48
49     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final UnkeyedListNode node) {
50         if (!(node instanceof ImmutableUnkeyedListNode)) {
51             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
52         }
53
54         return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
55     }
56
57     private void checkDirty() {
58         if (dirty) {
59             value = new LinkedList<>(value);
60             dirty = false;
61         }
62     }
63
64     @Override
65     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
66         checkDirty();
67         this.value.add(child);
68         return this;
69     }
70
71     @Override
72     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
73             final InstanceIdentifier.PathArgument key) {
74         checkDirty();
75         throw new UnsupportedOperationException("Children does not have identifiers.");
76     }
77
78     @Override
79     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final List<UnkeyedListEntryNode> value) {
80         // TODO replace or putAll ?
81         for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
82             withChild(UnkeyedListEntryNode);
83         }
84
85         return this;
86     }
87
88     @Override
89     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
90             final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
91         this.nodeIdentifier = nodeIdentifier;
92         return this;
93     }
94
95     @Override
96     public UnkeyedListNode build() {
97         dirty = true;
98         return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
99     }
100
101     @Override
102     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
103         return withChild(child);
104     }
105
106     @Override
107     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode> removeChild(
108             final PathArgument key) {
109         return withoutChild(key);
110     }
111
112     protected static final class ImmutableUnkeyedListNode extends
113             AbstractImmutableNormalizedValueNode<InstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>>
114             implements Immutable, UnkeyedListNode {
115
116         private final ImmutableList<UnkeyedListEntryNode> children;
117
118         ImmutableUnkeyedListNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
119                 final ImmutableList<UnkeyedListEntryNode> children) {
120             super(nodeIdentifier, children);
121             this.children = children;
122         }
123
124         @Override
125         protected int valueHashCode() {
126             return children.hashCode();
127         }
128
129         @Override
130         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
131             return children.equals(((ImmutableUnkeyedListNode) other).children);
132         }
133
134         @Override
135         public UnkeyedListEntryNode getChild(final int position) {
136             return children.get(position);
137         }
138
139         @Override
140         public int getSize() {
141             return children.size();
142         }
143     }
144 }