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