Cleanup checkstyle warnings and turn enforcement on in yang-data-impl
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeBuilder.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.base.Optional;
11 import com.google.common.collect.Maps;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Map;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.util.MapAdaptor;
18 import org.opendaylight.yangtools.util.UnmodifiableCollection;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
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 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
29
30 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
31     private static final int DEFAULT_CAPACITY = 4;
32
33     private final Map<NodeWithValue, LeafSetEntryNode<T>> value;
34     private NodeIdentifier nodeIdentifier;
35
36     protected ImmutableLeafSetNodeBuilder() {
37         value = new HashMap<>(DEFAULT_CAPACITY);
38     }
39
40     protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
41         if (sizeHint >= 0) {
42             value = Maps.newHashMapWithExpectedSize(sizeHint);
43         } else {
44             value = new HashMap<>(DEFAULT_CAPACITY);
45         }
46     }
47
48     protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
49         nodeIdentifier = node.getIdentifier();
50         value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
51     }
52
53     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
54         return new ImmutableLeafSetNodeBuilder<>();
55     }
56
57     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
58         return new ImmutableLeafSetNodeBuilder<>(sizeHint);
59     }
60
61     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
62         if (!(node instanceof ImmutableLeafSetNode<?>)) {
63             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
64         }
65
66         return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
67     }
68
69     @Override
70     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
71         this.value.put(child.getIdentifier(), child);
72         return this;
73     }
74
75     @Override
76     public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
77         this.value.remove(key);
78         return this;
79     }
80
81     @Override
82     public LeafSetNode<T> build() {
83         return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
84     }
85
86     @Override
87     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier nodeIdentifier) {
88         this.nodeIdentifier = nodeIdentifier;
89         return this;
90     }
91
92     @Override
93     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
94         for (final LeafSetEntryNode<T> leafSetEntry : value) {
95             withChild(leafSetEntry);
96         }
97         return this;
98     }
99
100
101     @Override
102     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
103         final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
104         b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), value));
105         b.withValue(value);
106         b.withAttributes(attributes);
107         return withChild(b.build());
108     }
109
110     @Override
111     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
112         return withChildValue(value, Collections.emptyMap());
113     }
114
115     protected static final class ImmutableLeafSetNode<T> extends
116             AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
117             Immutable, LeafSetNode<T> {
118
119         private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
120
121         ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
122                 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
123             super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
124             this.children = children;
125         }
126
127         @Override
128         public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
129             return Optional.fromNullable(children.get(child));
130         }
131
132         @Override
133         protected int valueHashCode() {
134             return children.hashCode();
135         }
136
137         @Override
138         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
139             return children.equals(((ImmutableLeafSetNode<?>) other).children);
140         }
141     }
142
143     @Override
144     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
145             final LeafSetEntryNode<T> child) {
146         return withChild(child);
147     }
148
149     @Override
150     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
151             removeChild(final PathArgument key) {
152         return withoutChild(key);
153     }
154
155 }