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