Allow shared JSONNNSWriter use
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableOrderedMapNodeBuilder.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.Iterables;
12 import java.util.Collection;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15 import org.opendaylight.yangtools.concepts.Immutable;
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.api.schema.OrderedMapNode;
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 ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, OrderedMapNode> {
28     private static final int DEFAULT_CAPACITY = 4;
29     private Map<NodeIdentifierWithPredicates, MapEntryNode> value;
30     private NodeIdentifier nodeIdentifier;
31     private boolean dirty;
32
33     protected ImmutableOrderedMapNodeBuilder() {
34         this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
35         this.dirty = false;
36     }
37
38     protected ImmutableOrderedMapNodeBuilder(final int sizeHint) {
39         if (sizeHint >= 0) {
40             this.value = new LinkedHashMap<>(sizeHint + sizeHint / 3);
41         } else {
42             this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
43         }
44         this.dirty = false;
45     }
46
47     protected ImmutableOrderedMapNodeBuilder(final ImmutableOrderedMapNode node) {
48         this.nodeIdentifier = node.getIdentifier();
49         this.value = node.children;
50         this.dirty = true;
51     }
52
53     public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create() {
54         return new ImmutableOrderedMapNodeBuilder();
55     }
56
57     public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final int sizeHint) {
58         return new ImmutableOrderedMapNodeBuilder(sizeHint);
59     }
60
61     public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final MapNode node) {
62         if (!(node instanceof ImmutableOrderedMapNode)) {
63             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
64         }
65
66         return new ImmutableOrderedMapNodeBuilder((ImmutableOrderedMapNode) node);
67     }
68
69     private void checkDirty() {
70         if (dirty) {
71             value = new LinkedHashMap<>(value);
72             dirty = false;
73         }
74     }
75
76     @Override
77     public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withChild(final MapEntryNode child) {
78         checkDirty();
79         this.value.put(child.getIdentifier(), child);
80         return this;
81     }
82
83     @Override
84     public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withoutChild(final PathArgument key) {
85         checkDirty();
86         this.value.remove(key);
87         return this;
88     }
89
90     @Override
91     public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withValue(final Collection<MapEntryNode> value) {
92         // TODO replace or putAll ?
93         for (final MapEntryNode mapEntryNode : value) {
94             withChild(mapEntryNode);
95         }
96
97         return this;
98     }
99
100     @Override
101     public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withNodeIdentifier(final NodeIdentifier nodeIdentifier) {
102         this.nodeIdentifier = nodeIdentifier;
103         return this;
104     }
105
106     @Override
107     public OrderedMapNode build() {
108         dirty = true;
109         return new ImmutableOrderedMapNode(nodeIdentifier, value);
110     }
111
112     @Override
113     public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> addChild(
114             final MapEntryNode child) {
115         return withChild(child);
116     }
117
118
119     @Override
120     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, OrderedMapNode> removeChild(
121             final PathArgument key) {
122         return withoutChild(key);
123     }
124
125     protected static final class ImmutableOrderedMapNode extends AbstractImmutableNormalizedNode<NodeIdentifier, Collection<MapEntryNode>> implements Immutable, OrderedMapNode {
126
127         private final Map<NodeIdentifierWithPredicates, MapEntryNode> children;
128
129         ImmutableOrderedMapNode(final NodeIdentifier nodeIdentifier,
130                          final Map<NodeIdentifierWithPredicates, MapEntryNode> children) {
131             super(nodeIdentifier);
132             this.children = children;
133         }
134
135         @Override
136         public Optional<MapEntryNode> getChild(final NodeIdentifierWithPredicates child) {
137             return Optional.fromNullable(children.get(child));
138         }
139
140         @Override
141         protected int valueHashCode() {
142             return children.hashCode();
143         }
144
145         @Override
146         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
147             return children.equals(((ImmutableOrderedMapNode) other).children);
148         }
149
150         @Override
151         public MapEntryNode getChild(final int position) {
152             return Iterables.get(children.values(), position);
153         }
154
155         @Override
156         public int getSize() {
157             return children.size();
158         }
159
160         @Override
161         public Collection<MapEntryNode> getValue() {
162             return UnmodifiableCollection.create(children.values());
163         }
164     }
165 }