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