Do not retain leaf nodes in containers by default
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / AbstractImmutableDataContainerNodeBuilder.java
index 21303e62dac4e9d718275b7851878f48af4997b6..dc8cf1e33c10d7e63b5bc8eb7ce473b18a6189d4 100644 (file)
@@ -7,22 +7,43 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
 
+import com.google.common.collect.Maps;
+import java.util.Collection;
 import java.util.HashMap;
-import java.util.List;
+import java.util.LinkedHashMap;
 import java.util.Map;
-
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.util.ModifiableMapPhase;
+import org.opendaylight.yangtools.util.UnmodifiableMapPhase;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.nodes.CloneableMap;
+import org.opendaylight.yangtools.yang.data.impl.schema.nodes.LazyLeafOperations;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceIdentifier.PathArgument, R extends DataContainerNode<I>>
+abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument, R extends DataContainerNode<I>>
         implements DataContainerNodeBuilder<I, R> {
 
-    private Map<YangInstanceIdentifier.PathArgument, DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value;
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractImmutableDataContainerNodeBuilder.class);
+    private static final int DEFAULT_CAPACITY = 4;
+
+    // This is a run-time constant, i.e. it is set at class initialization time. We expect JIT to notice this and
+    // perform DCE based on the value, so that the static newHashMap() methods end up not containing the branch at all.
+    private static final boolean USE_LINKEDHASHMAP;
+
+    static {
+        USE_LINKEDHASHMAP = Boolean.getBoolean(
+            "org.opendaylight.yangtools.yang.data.impl.schema.builder.retain-child-order");
+        if (USE_LINKEDHASHMAP) {
+            LOG.info("Initial immutable DataContainerNodes are retaining child insertion order");
+        }
+    }
+
+    private Map<PathArgument, Object> value;
     private I nodeIdentifier;
 
     /*
@@ -34,12 +55,29 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceI
     private boolean dirty;
 
     protected AbstractImmutableDataContainerNodeBuilder() {
-        this.value = new HashMap<>();
+        this.value = newHashMap();
+        this.dirty = false;
+    }
+
+    protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
+        if (sizeHint >= 0) {
+            this.value = newHashMap(sizeHint);
+        } else {
+            this.value = newHashMap();
+        }
         this.dirty = false;
     }
 
     protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
         this.nodeIdentifier = node.getIdentifier();
+
+        /*
+         * This quite awkward. What we actually want to be saying here is: give me
+         * a copy-on-write view of your children. The API involved in that could be
+         * a bit hairy, so we do the next best thing and rely on the fact that the
+         * returned object implements a specific interface, which leaks the functionality
+         * we need.
+         */
         this.value = node.getChildren();
         this.dirty = true;
     }
@@ -49,25 +87,36 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceI
     }
 
     protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
-        return value.get(child);
+        return LazyLeafOperations.getChild(value, child);
     }
 
-    protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
+    protected final Map<PathArgument, Object> buildValue() {
+        if (value instanceof ModifiableMapPhase) {
+            return ((ModifiableMapPhase<PathArgument, Object>)value).toUnmodifiableMap();
+        }
+
         dirty = true;
         return value;
     }
 
     private void checkDirty() {
         if (dirty) {
-            value = new HashMap<>(value);
+            if (value instanceof UnmodifiableMapPhase) {
+                value = ((UnmodifiableMapPhase<PathArgument, Object>) value).toModifiableMap();
+            } else if (value instanceof CloneableMap) {
+                value = ((CloneableMap<PathArgument, Object>) value).createMutableClone();
+            } else {
+                value = newHashMap(value);
+            }
             dirty = false;
         }
     }
 
     @Override
-    public DataContainerNodeBuilder<I, R> withValue(final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value) {
+    public DataContainerNodeBuilder<I, R> withValue(
+            final Collection<DataContainerChild<? extends PathArgument, ?>> withValue) {
         // TODO Replace or putAll ?
-        for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : value) {
+        for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : withValue) {
             withChild(dataContainerChild);
         }
         return this;
@@ -76,7 +125,7 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceI
     @Override
     public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
         checkDirty();
-        this.value.put(child.getIdentifier(), child);
+        LazyLeafOperations.putChild(value, child);
         return this;
     }
 
@@ -88,8 +137,8 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceI
     }
 
     @Override
-    public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
-        this.nodeIdentifier = nodeIdentifier;
+    public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I withNodeIdentifier) {
+        this.nodeIdentifier = withNodeIdentifier;
         return this;
     }
 
@@ -100,7 +149,21 @@ abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceI
     }
 
     @Override
-    public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
+    public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R>
+            removeChild(final PathArgument key) {
         return withoutChild(key);
     }
+
+    // Static utility methods providing dispatch to proper HashMap implementation.
+    private static <K, V> HashMap<K, V> newHashMap() {
+        return USE_LINKEDHASHMAP ? new LinkedHashMap<>(DEFAULT_CAPACITY) : new HashMap<>(DEFAULT_CAPACITY);
+    }
+
+    private static <K, V> HashMap<K, V> newHashMap(final int size) {
+        return USE_LINKEDHASHMAP ? Maps.newLinkedHashMapWithExpectedSize(size) : Maps.newHashMapWithExpectedSize(size);
+    }
+
+    private static <K, V> HashMap<K, V> newHashMap(final Map<K, V> map) {
+        return USE_LINKEDHASHMAP ? new LinkedHashMap<>(map) : new HashMap<>(map);
+    }
 }