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