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;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
22 abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceIdentifier.PathArgument, R extends DataContainerNode<I>>
23 implements DataContainerNodeBuilder<I, R> {
25 private Map<YangInstanceIdentifier.PathArgument, DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value;
26 private I nodeIdentifier;
29 * Tracks whether the builder is dirty, e.g. whether the value map has been used
30 * to construct a child. If it has, we detect this condition before any further
31 * modification and create a new value map with same contents. This way we do not
32 * force a map copy if the builder is not reused.
34 private boolean dirty;
36 protected AbstractImmutableDataContainerNodeBuilder() {
37 this.value = new HashMap<>();
41 protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
42 this.nodeIdentifier = node.getIdentifier();
43 this.value = node.getChildren();
47 protected final I getNodeIdentifier() {
48 return nodeIdentifier;
51 protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
52 return value.get(child);
55 protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
60 private void checkDirty() {
62 value = new HashMap<>(value);
68 public DataContainerNodeBuilder<I, R> withValue(final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value) {
69 // TODO Replace or putAll ?
70 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : value) {
71 withChild(dataContainerChild);
77 public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
79 this.value.put(child.getIdentifier(), child);
84 public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
86 this.value.remove(key);
91 public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
92 this.nodeIdentifier = nodeIdentifier;
97 public DataContainerNodeBuilder<I, R> addChild(
98 final DataContainerChild<? extends PathArgument, ?> child) {
99 return withChild(child);
103 public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
104 return withoutChild(key);