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