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 java.util.HashMap;
11 import java.util.List;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
19 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
21 abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceIdentifier.PathArgument, R extends DataContainerNode<I>> implements DataContainerNodeBuilder<I, R> {
22 private static final int DEFAULT_CAPACITY = 4;
23 private Map<YangInstanceIdentifier.PathArgument, DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value;
24 private I nodeIdentifier;
27 * Tracks whether the builder is dirty, e.g. whether the value map has been used
28 * to construct a child. If it has, we detect this condition before any further
29 * modification and create a new value map with same contents. This way we do not
30 * force a map copy if the builder is not reused.
32 private boolean dirty;
34 protected AbstractImmutableDataContainerNodeBuilder() {
35 this.value = new HashMap<>(DEFAULT_CAPACITY);
39 protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
40 this.value = new HashMap<>(DEFAULT_CAPACITY);
44 protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
45 this.nodeIdentifier = node.getIdentifier();
48 * FIXME: BUG-2402: this call is not what we actually want. We are the
49 * only user of getChildren(), and we really want this to be a
50 * zero-copy operation if we happen to not modify the children.
51 * If we do, we want to perform an efficient copy-on-write before
54 * With this interface we end up creating a lot of short-lived
55 * objects in case we modify the map -- see checkDirty().
57 this.value = node.getChildren();
61 protected final I getNodeIdentifier() {
62 return nodeIdentifier;
65 protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
66 return value.get(child);
69 protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
74 private void checkDirty() {
77 * FIXME: BUG-2402: This is the second part of the above. Note
78 * that value here is usually a read-only view. Invocation
79 * of this constructor will force instantiation of a wrapper
80 * Map.Entry object, just to make sure this read path does
83 value = new HashMap<>(value);
89 public DataContainerNodeBuilder<I, R> withValue(final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value) {
90 // TODO Replace or putAll ?
91 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : value) {
92 withChild(dataContainerChild);
98 public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
100 this.value.put(child.getIdentifier(), child);
105 public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
107 this.value.remove(key);
112 public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
113 this.nodeIdentifier = nodeIdentifier;
118 public DataContainerNodeBuilder<I, R> addChild(
119 final DataContainerChild<? extends PathArgument, ?> child) {
120 return withChild(child);
124 public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
125 return withoutChild(key);