2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
10 import com.google.common.collect.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.LinkedHashMap;
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;
29 abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument, R extends DataContainerNode>
30 implements DataContainerNodeBuilder<I, R> {
32 private static final Logger LOG = LoggerFactory.getLogger(AbstractImmutableDataContainerNodeBuilder.class);
33 private static final int DEFAULT_CAPACITY = 4;
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;
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");
47 private Map<PathArgument, Object> value;
48 private I nodeIdentifier;
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.
56 private boolean dirty;
58 protected AbstractImmutableDataContainerNodeBuilder() {
59 this.value = newHashMap();
63 protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
65 this.value = newHashMap(sizeHint);
67 this.value = newHashMap();
72 protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I, R> node) {
73 this.nodeIdentifier = node.getIdentifier();
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
82 this.value = node.getChildren();
86 protected final I getNodeIdentifier() {
87 return nodeIdentifier;
90 protected final @Nullable DataContainerChild getChild(final PathArgument child) {
91 return LazyLeafOperations.getChild(value, child);
94 protected final Map<PathArgument, Object> buildValue() {
95 if (value instanceof ModifiableMapPhase) {
96 return ((ModifiableMapPhase<PathArgument, Object>)value).toUnmodifiableMap();
103 private void checkDirty() {
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();
110 value = newHashMap(value);
117 public DataContainerNodeBuilder<I, R> withValue(final Collection<DataContainerChild> withValue) {
118 // TODO Replace or putAll ?
119 for (final DataContainerChild dataContainerChild : withValue) {
120 withChild(dataContainerChild);
126 public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild child) {
128 LazyLeafOperations.putChild(value, child);
133 public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
135 this.value.remove(key);
140 public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I withNodeIdentifier) {
141 this.nodeIdentifier = withNodeIdentifier;
146 public DataContainerNodeBuilder<I, R> addChild(final DataContainerChild child) {
147 return withChild(child);
151 public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild, R> removeChild(final PathArgument key) {
152 return withoutChild(key);
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);
160 private static <K, V> HashMap<K, V> newHashMap(final int size) {
161 return USE_LINKEDHASHMAP ? Maps.newLinkedHashMapWithExpectedSize(size) : Maps.newHashMapWithExpectedSize(size);
164 private static <K, V> HashMap<K, V> newHashMap(final Map<K, V> map) {
165 return USE_LINKEDHASHMAP ? new LinkedHashMap<>(map) : new HashMap<>(map);