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;
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;
21 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.CloneableChildrenMap;
23 abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceIdentifier.PathArgument, R extends DataContainerNode<I>> implements DataContainerNodeBuilder<I, R> {
24 private static final int DEFAULT_CAPACITY = 4;
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<>(DEFAULT_CAPACITY);
41 protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
43 this.value = Maps.newHashMapWithExpectedSize(sizeHint);
45 this.value = new HashMap<>(DEFAULT_CAPACITY);
50 protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
51 this.nodeIdentifier = node.getIdentifier();
54 * This quite awkward. What we actually want to be saying here is: give me
55 * a copy-on-write view of your children. The API involved in that could be
56 * a bit hairy, so we do the next best thing and rely on the fact that the
57 * returned object implements a specific interface, which leaks the functionality
60 this.value = node.getChildren();
64 protected final I getNodeIdentifier() {
65 return nodeIdentifier;
68 protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
69 return value.get(child);
72 protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
77 private void checkDirty() {
79 if (value instanceof CloneableChildrenMap) {
80 value = ((CloneableChildrenMap) value).createMutableClone();
82 value = new HashMap<>(value);
89 public DataContainerNodeBuilder<I, R> withValue(final Collection<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);