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