Add NormalizedNodeContainer.size()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapNodeBuilder.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.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.util.MapAdaptor;
17 import org.opendaylight.yangtools.util.UnmodifiableCollection;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26
27 public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, MapNode> {
28     private static final int DEFAULT_CAPACITY = 4;
29
30     private final Map<NodeIdentifierWithPredicates, MapEntryNode> value;
31     private NodeIdentifier nodeIdentifier;
32
33     protected ImmutableMapNodeBuilder() {
34         this.value = new HashMap<>(DEFAULT_CAPACITY);
35     }
36
37     protected ImmutableMapNodeBuilder(final int sizeHint) {
38         if (sizeHint >= 0) {
39             this.value = Maps.newHashMapWithExpectedSize(sizeHint);
40         } else {
41             this.value = new HashMap<>(DEFAULT_CAPACITY);
42         }
43     }
44
45     protected ImmutableMapNodeBuilder(final ImmutableMapNode node) {
46         this.nodeIdentifier = node.getIdentifier();
47         this.value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
48     }
49
50     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create() {
51         return new ImmutableMapNodeBuilder();
52     }
53
54     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create(final int sizeHint) {
55         return new ImmutableMapNodeBuilder(sizeHint);
56     }
57
58     public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final MapNode node) {
59         if (!(node instanceof ImmutableMapNode)) {
60             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
61         }
62
63         return new ImmutableMapNodeBuilder((ImmutableMapNode) node);
64     }
65
66     @Override
67     public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
68         this.value.put(child.getIdentifier(), child);
69         return this;
70     }
71
72     @Override
73     public CollectionNodeBuilder<MapEntryNode, MapNode> withoutChild(final PathArgument key) {
74         this.value.remove(key);
75         return this;
76     }
77
78     @Override
79     public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(final Collection<MapEntryNode> withValue) {
80         // TODO replace or putAll ?
81         for (final MapEntryNode mapEntryNode : withValue) {
82             withChild(mapEntryNode);
83         }
84
85         return this;
86     }
87
88     @Override
89     public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
90         this.nodeIdentifier = withNodeIdentifier;
91         return this;
92     }
93
94     @Override
95     public MapNode build() {
96         return new ImmutableMapNode(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
97     }
98
99     @Override
100     public CollectionNodeBuilder<MapEntryNode, MapNode> addChild(
101             final MapEntryNode child) {
102         return withChild(child);
103     }
104
105     @Override
106     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, MapNode> removeChild(
107             final PathArgument key) {
108         return withoutChild(key);
109     }
110
111     protected static final class ImmutableMapNode
112             extends AbstractImmutableNormalizedNode<NodeIdentifier, Collection<MapEntryNode>> implements MapNode {
113
114         private final Map<NodeIdentifierWithPredicates, MapEntryNode> children;
115
116         ImmutableMapNode(final NodeIdentifier nodeIdentifier,
117                          final Map<NodeIdentifierWithPredicates, MapEntryNode> children) {
118             super(nodeIdentifier);
119             this.children = children;
120         }
121
122         @Override
123         public Optional<MapEntryNode> getChild(final NodeIdentifierWithPredicates child) {
124             return Optional.ofNullable(children.get(child));
125         }
126
127         @Override
128         public Collection<MapEntryNode> getValue() {
129             return UnmodifiableCollection.create(children.values());
130         }
131
132         @Override
133         public int size() {
134             return children.size();
135         }
136
137         @Override
138         protected int valueHashCode() {
139             return children.hashCode();
140         }
141
142         @Override
143         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
144             return children.equals(((ImmutableMapNode) other).children);
145         }
146     }
147 }