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 / ImmutableOrderedLeafSetNodeBuilder.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.Iterables;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15 import java.util.Optional;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.util.UnmodifiableCollection;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
28
29 public class ImmutableOrderedLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
30
31     private Map<NodeWithValue, LeafSetEntryNode<T>> value;
32     private NodeIdentifier nodeIdentifier;
33     private boolean dirty;
34
35     protected ImmutableOrderedLeafSetNodeBuilder() {
36         value = new LinkedHashMap<>();
37         dirty = false;
38     }
39
40     protected ImmutableOrderedLeafSetNodeBuilder(final ImmutableOrderedLeafSetNode<T> node) {
41         nodeIdentifier = node.getIdentifier();
42         value = node.getChildren();
43         dirty = true;
44     }
45
46     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
47         return new ImmutableOrderedLeafSetNodeBuilder<>();
48     }
49
50     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
51         if (!(node instanceof ImmutableOrderedLeafSetNode<?>)) {
52             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
53         }
54
55         return new ImmutableOrderedLeafSetNodeBuilder<>((ImmutableOrderedLeafSetNode<T>) node);
56     }
57
58     private void checkDirty() {
59         if (dirty) {
60             value = new LinkedHashMap<>(value);
61             dirty = false;
62         }
63     }
64
65     @Override
66     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
67         checkDirty();
68         this.value.put(child.getIdentifier(), child);
69         return this;
70     }
71
72     @Override
73     public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
74         checkDirty();
75         this.value.remove(key);
76         return this;
77     }
78
79     @Override
80     public OrderedLeafSetNode<T> build() {
81         dirty = true;
82         return new ImmutableOrderedLeafSetNode<>(nodeIdentifier, value);
83     }
84
85     @Override
86     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
87         this.nodeIdentifier = withNodeIdentifier;
88         return this;
89     }
90
91     @Override
92     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
93         checkDirty();
94         for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
95             withChild(leafSetEntry);
96         }
97         return this;
98     }
99
100     @Override
101     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue,
102             final Map<QName, String> attributes) {
103         final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
104         b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue));
105         b.withValue(childValue);
106         b.withAttributes(attributes);
107         return withChild(b.build());
108     }
109
110     @Override
111     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
112         return withChildValue(childValue, Collections.emptyMap());
113     }
114
115     protected static final class ImmutableOrderedLeafSetNode<T> extends
116             AbstractImmutableNormalizedNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
117             Immutable, OrderedLeafSetNode<T> {
118
119         private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
120
121         ImmutableOrderedLeafSetNode(final NodeIdentifier nodeIdentifier,
122                 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
123             super(nodeIdentifier);
124             this.children = children;
125         }
126
127         @Override
128         public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
129             return Optional.ofNullable(children.get(child));
130         }
131
132         @Override
133         public LeafSetEntryNode<T> getChild(final int position) {
134             return Iterables.get(children.values(), position);
135         }
136
137         @Override
138         protected int valueHashCode() {
139             return children.hashCode();
140         }
141
142         private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
143             return Collections.unmodifiableMap(children);
144         }
145
146         @Override
147         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
148             return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
149         }
150
151         @Override
152         public int getSize() {
153             return children.size();
154         }
155
156         @Override
157         public Collection<LeafSetEntryNode<T>> getValue() {
158             return UnmodifiableCollection.create(children.values());
159         }
160     }
161
162     @Override
163     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
164             final LeafSetEntryNode<T> child) {
165         return withChild(child);
166     }
167
168     @Override
169     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
170             removeChild(final PathArgument key) {
171         return withoutChild(key);
172     }
173 }