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