7a267d968ae5a338bb3c531c201d43035827d80a
[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.opendaylight.yangtools.util.MapAdaptor;
16 import org.opendaylight.yangtools.util.UnmodifiableCollection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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
29     private final Map<NodeIdentifierWithPredicates, MapEntryNode> value;
30     private NodeIdentifier nodeIdentifier;
31
32     protected ImmutableMapNodeBuilder() {
33         this.value = new HashMap<>(DEFAULT_CAPACITY);
34     }
35
36     protected ImmutableMapNodeBuilder(final int sizeHint) {
37         if (sizeHint >= 0) {
38             this.value = Maps.newHashMapWithExpectedSize(sizeHint);
39         } else {
40             this.value = new HashMap<>(DEFAULT_CAPACITY);
41         }
42     }
43
44     protected ImmutableMapNodeBuilder(final ImmutableMapNode node) {
45         this.nodeIdentifier = node.getIdentifier();
46         this.value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
47     }
48
49     public static CollectionNodeBuilder<MapEntryNode, MapNode> create() {
50         return new ImmutableMapNodeBuilder();
51     }
52
53     public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final int sizeHint) {
54         return new ImmutableMapNodeBuilder(sizeHint);
55     }
56
57     public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final MapNode node) {
58         if (!(node instanceof ImmutableMapNode)) {
59             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
60         }
61
62         return new ImmutableMapNodeBuilder((ImmutableMapNode) node);
63     }
64
65     @Override
66     public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
67         this.value.put(child.getIdentifier(), child);
68         return this;
69     }
70
71     @Override
72     public CollectionNodeBuilder<MapEntryNode, MapNode> withoutChild(final PathArgument key) {
73         this.value.remove(key);
74         return this;
75     }
76
77     @Override
78     public CollectionNodeBuilder<MapEntryNode, MapNode> 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 CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
89         this.nodeIdentifier = withNodeIdentifier;
90         return this;
91     }
92
93     @Override
94     public MapNode build() {
95         return new ImmutableMapNode(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
96     }
97
98     @Override
99     public CollectionNodeBuilder<MapEntryNode, MapNode> addChild(
100             final MapEntryNode child) {
101         return withChild(child);
102     }
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         protected int valueHashCode() {
134             return children.hashCode();
135         }
136
137         @Override
138         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
139             return children.equals(((ImmutableMapNode) other).children);
140         }
141     }
142 }