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