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.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;
28 abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument, R extends DataContainerNode<I>>
29 implements DataContainerNodeBuilder<I, R> {
31 private static final Logger LOG = LoggerFactory.getLogger(AbstractImmutableDataContainerNodeBuilder.class);
32 private static final int DEFAULT_CAPACITY = 4;
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;
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");
46 private Map<PathArgument, Object> value;
47 private I nodeIdentifier;
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.
55 private boolean dirty;
57 protected AbstractImmutableDataContainerNodeBuilder() {
58 this.value = newHashMap();
62 protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
64 this.value = newHashMap(sizeHint);
66 this.value = newHashMap();
71 protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
72 this.nodeIdentifier = node.getIdentifier();
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
81 this.value = node.getChildren();
85 protected final I getNodeIdentifier() {
86 return nodeIdentifier;
89 protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
90 return LazyLeafOperations.getChild(value, child);
93 protected final Map<PathArgument, Object> buildValue() {
94 if (value instanceof ModifiableMapPhase) {
95 return ((ModifiableMapPhase<PathArgument, Object>)value).toUnmodifiableMap();
102 private void checkDirty() {
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();
109 value = newHashMap(value);
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);
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(
147 final DataContainerChild<? extends PathArgument, ?> child) {
148 return withChild(child);
152 public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R>
153 removeChild(final PathArgument key) {
154 return withoutChild(key);
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);
162 private static <K, V> HashMap<K, V> newHashMap(final int size) {
163 return USE_LINKEDHASHMAP ? Maps.newLinkedHashMapWithExpectedSize(size) : Maps.newHashMapWithExpectedSize(size);
166 private static <K, V> HashMap<K, V> newHashMap(final Map<K, V> map) {
167 return USE_LINKEDHASHMAP ? new LinkedHashMap<>(map) : new HashMap<>(map);