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