BUG-1275: teach NormalizedNode builders about size hints
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / AbstractImmutableDataContainerNodeBuilder.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 java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
21
22 abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceIdentifier.PathArgument, R extends DataContainerNode<I>> implements DataContainerNodeBuilder<I, R> {
23
24     private Map<YangInstanceIdentifier.PathArgument, DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value;
25     private I nodeIdentifier;
26
27     /*
28      * Tracks whether the builder is dirty, e.g. whether the value map has been used
29      * to construct a child. If it has, we detect this condition before any further
30      * modification and create a new value map with same contents. This way we do not
31      * force a map copy if the builder is not reused.
32      */
33     private boolean dirty;
34
35     protected AbstractImmutableDataContainerNodeBuilder() {
36         this.value = new HashMap<>();
37         this.dirty = false;
38     }
39
40     protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
41         this.value = new HashMap<>(sizeHint);
42         this.dirty = false;
43     }
44
45     protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
46         this.nodeIdentifier = node.getIdentifier();
47         this.value = node.getChildren();
48         this.dirty = true;
49     }
50
51     protected final I getNodeIdentifier() {
52         return nodeIdentifier;
53     }
54
55     protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
56         return value.get(child);
57     }
58
59     protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
60         dirty = true;
61         return value;
62     }
63
64     private void checkDirty() {
65         if (dirty) {
66             value = new HashMap<>(value);
67             dirty = false;
68         }
69     }
70
71     @Override
72     public DataContainerNodeBuilder<I, R> withValue(final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value) {
73         // TODO Replace or putAll ?
74         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : value) {
75             withChild(dataContainerChild);
76         }
77         return this;
78     }
79
80     @Override
81     public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
82         checkDirty();
83         this.value.put(child.getIdentifier(), child);
84         return this;
85     }
86
87     @Override
88     public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
89         checkDirty();
90         this.value.remove(key);
91         return this;
92     }
93
94     @Override
95     public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
96         this.nodeIdentifier = nodeIdentifier;
97         return this;
98     }
99
100     @Override
101     public DataContainerNodeBuilder<I, R> addChild(
102             final DataContainerChild<? extends PathArgument, ?> child) {
103         return withChild(child);
104     }
105
106     @Override
107     public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
108         return withoutChild(key);
109     }
110 }