Move NormalizedNode builders
[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 com.google.common.collect.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.util.ModifiableMapPhase;
17 import org.opendaylight.yangtools.util.UnmodifiableMapPhase;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
20 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.CloneableMap;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.LazyLeafOperations;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument, R extends DataContainerNode>
30         implements DataContainerNodeBuilder<I, R> {
31
32     private static final Logger LOG = LoggerFactory.getLogger(AbstractImmutableDataContainerNodeBuilder.class);
33     private static final int DEFAULT_CAPACITY = 4;
34
35     // This is a run-time constant, i.e. it is set at class initialization time. We expect JIT to notice this and
36     // perform DCE based on the value, so that the static newHashMap() methods end up not containing the branch at all.
37     private static final boolean USE_LINKEDHASHMAP;
38
39     static {
40         USE_LINKEDHASHMAP = Boolean.getBoolean(
41             "org.opendaylight.yangtools.yang.data.impl.schema.builder.retain-child-order");
42         if (USE_LINKEDHASHMAP) {
43             LOG.info("Initial immutable DataContainerNodes are retaining child insertion order");
44         }
45     }
46
47     private Map<PathArgument, Object> value;
48     private I nodeIdentifier;
49
50     /*
51      * Tracks whether the builder is dirty, e.g. whether the value map has been used
52      * to construct a child. If it has, we detect this condition before any further
53      * modification and create a new value map with same contents. This way we do not
54      * force a map copy if the builder is not reused.
55      */
56     private boolean dirty;
57
58     protected AbstractImmutableDataContainerNodeBuilder() {
59         this.value = newHashMap();
60         this.dirty = false;
61     }
62
63     protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
64         if (sizeHint >= 0) {
65             this.value = newHashMap(sizeHint);
66         } else {
67             this.value = newHashMap();
68         }
69         this.dirty = false;
70     }
71
72     protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I, R> node) {
73         this.nodeIdentifier = node.getIdentifier();
74
75         /*
76          * This quite awkward. What we actually want to be saying here is: give me
77          * a copy-on-write view of your children. The API involved in that could be
78          * a bit hairy, so we do the next best thing and rely on the fact that the
79          * returned object implements a specific interface, which leaks the functionality
80          * we need.
81          */
82         this.value = node.getChildren();
83         this.dirty = true;
84     }
85
86     protected final I getNodeIdentifier() {
87         return nodeIdentifier;
88     }
89
90     protected final @Nullable DataContainerChild getChild(final PathArgument child) {
91         return LazyLeafOperations.getChild(value, child);
92     }
93
94     protected final Map<PathArgument, Object> buildValue() {
95         if (value instanceof ModifiableMapPhase) {
96             return ((ModifiableMapPhase<PathArgument, Object>)value).toUnmodifiableMap();
97         }
98
99         dirty = true;
100         return value;
101     }
102
103     private void checkDirty() {
104         if (dirty) {
105             if (value instanceof UnmodifiableMapPhase) {
106                 value = ((UnmodifiableMapPhase<PathArgument, Object>) value).toModifiableMap();
107             } else if (value instanceof CloneableMap) {
108                 value = ((CloneableMap<PathArgument, Object>) value).createMutableClone();
109             } else {
110                 value = newHashMap(value);
111             }
112             dirty = false;
113         }
114     }
115
116     @Override
117     public DataContainerNodeBuilder<I, R> withValue(final Collection<DataContainerChild> withValue) {
118         // TODO Replace or putAll ?
119         for (final DataContainerChild dataContainerChild : withValue) {
120             withChild(dataContainerChild);
121         }
122         return this;
123     }
124
125     @Override
126     public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild child) {
127         checkDirty();
128         LazyLeafOperations.putChild(value, child);
129         return this;
130     }
131
132     @Override
133     public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
134         checkDirty();
135         this.value.remove(key);
136         return this;
137     }
138
139     @Override
140     public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I withNodeIdentifier) {
141         this.nodeIdentifier = withNodeIdentifier;
142         return this;
143     }
144
145     @Override
146     public DataContainerNodeBuilder<I, R> addChild(final DataContainerChild child) {
147         return withChild(child);
148     }
149
150     @Override
151     public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild, R> removeChild(final PathArgument key) {
152         return withoutChild(key);
153     }
154
155     // Static utility methods providing dispatch to proper HashMap implementation.
156     private static <K, V> HashMap<K, V> newHashMap() {
157         return USE_LINKEDHASHMAP ? new LinkedHashMap<>(DEFAULT_CAPACITY) : new HashMap<>(DEFAULT_CAPACITY);
158     }
159
160     private static <K, V> HashMap<K, V> newHashMap(final int size) {
161         return USE_LINKEDHASHMAP ? Maps.newLinkedHashMapWithExpectedSize(size) : Maps.newHashMapWithExpectedSize(size);
162     }
163
164     private static <K, V> HashMap<K, V> newHashMap(final Map<K, V> map) {
165         return USE_LINKEDHASHMAP ? new LinkedHashMap<>(map) : new HashMap<>(map);
166     }
167 }