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