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.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument, R extends DataContainerNode<I>>
28 implements DataContainerNodeBuilder<I, R> {
30 private static final Logger LOG = LoggerFactory.getLogger(AbstractImmutableDataContainerNodeBuilder.class);
31 private static final int DEFAULT_CAPACITY = 4;
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;
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");
45 private Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> value;
46 private I nodeIdentifier;
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.
54 private boolean dirty;
56 protected AbstractImmutableDataContainerNodeBuilder() {
57 this.value = newHashMap();
61 protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
63 this.value = newHashMap(sizeHint);
65 this.value = newHashMap();
70 protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
71 this.nodeIdentifier = node.getIdentifier();
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
80 this.value = node.getChildren();
84 protected final I getNodeIdentifier() {
85 return nodeIdentifier;
88 protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
89 return value.get(child);
92 protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
93 if (value instanceof ModifiableMapPhase) {
94 return ((ModifiableMapPhase<PathArgument, DataContainerChild<? extends PathArgument, ?>>)value)
102 private void checkDirty() {
104 if (value instanceof UnmodifiableMapPhase) {
105 value = ((UnmodifiableMapPhase<PathArgument, DataContainerChild<? extends PathArgument, ?>>) value)
107 } else if (value instanceof CloneableMap) {
108 value = ((CloneableMap<PathArgument, DataContainerChild<? extends PathArgument, ?>>) value)
109 .createMutableClone();
111 value = newHashMap(value);
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);
128 public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
130 this.value.put(child.getIdentifier(), child);
135 public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
137 this.value.remove(key);
142 public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I withNodeIdentifier) {
143 this.nodeIdentifier = withNodeIdentifier;
148 public DataContainerNodeBuilder<I, R> addChild(
149 final DataContainerChild<? extends PathArgument, ?> child) {
150 return withChild(child);
154 public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R>
155 removeChild(final PathArgument key) {
156 return withoutChild(key);
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);
164 private static <K, V> HashMap<K, V> newHashMap(final int size) {
165 return USE_LINKEDHASHMAP ? Maps.newLinkedHashMapWithExpectedSize(size) : Maps.newHashMapWithExpectedSize(size);
168 private static <K, V> HashMap<K, V> newHashMap(final Map<K, V> map) {
169 return USE_LINKEDHASHMAP ? new LinkedHashMap<>(map) : new HashMap<>(map);