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.util.ModifiableMapPhase;
15 import org.opendaylight.yangtools.util.UnmodifiableMapPhase;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
18 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.CloneableMap;
24 abstract class AbstractImmutableDataContainerNodeBuilder<I extends PathArgument, R extends DataContainerNode<I>> implements DataContainerNodeBuilder<I, R> {
25 private static final int DEFAULT_CAPACITY = 4;
26 private Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> value;
27 private I nodeIdentifier;
30 * Tracks whether the builder is dirty, e.g. whether the value map has been used
31 * to construct a child. If it has, we detect this condition before any further
32 * modification and create a new value map with same contents. This way we do not
33 * force a map copy if the builder is not reused.
35 private boolean dirty;
37 protected AbstractImmutableDataContainerNodeBuilder() {
38 this.value = new HashMap<>(DEFAULT_CAPACITY);
42 protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
44 this.value = Maps.newHashMapWithExpectedSize(sizeHint);
46 this.value = new HashMap<>(DEFAULT_CAPACITY);
51 protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
52 this.nodeIdentifier = node.getIdentifier();
55 * This quite awkward. What we actually want to be saying here is: give me
56 * a copy-on-write view of your children. The API involved in that could be
57 * a bit hairy, so we do the next best thing and rely on the fact that the
58 * returned object implements a specific interface, which leaks the functionality
61 this.value = node.getChildren();
65 protected final I getNodeIdentifier() {
66 return nodeIdentifier;
69 protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
70 return value.get(child);
73 protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
74 if (value instanceof ModifiableMapPhase) {
75 return ((ModifiableMapPhase<PathArgument, DataContainerChild<? extends PathArgument, ?>>)value).toUnmodifiableMap();
82 private void checkDirty() {
84 if (value instanceof UnmodifiableMapPhase) {
85 value = ((UnmodifiableMapPhase<PathArgument, DataContainerChild<? extends PathArgument, ?>>) value).toModifiableMap();
86 } else if (value instanceof CloneableMap) {
87 value = ((CloneableMap<PathArgument, DataContainerChild<? extends PathArgument, ?>>) value).createMutableClone();
89 value = new HashMap<>(value);
96 public DataContainerNodeBuilder<I, R> withValue(final Collection<DataContainerChild<? extends PathArgument, ?>> value) {
97 // TODO Replace or putAll ?
98 for (final DataContainerChild<? extends PathArgument, ?> dataContainerChild : value) {
99 withChild(dataContainerChild);
105 public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
107 this.value.put(child.getIdentifier(), child);
112 public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
114 this.value.remove(key);
119 public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
120 this.nodeIdentifier = nodeIdentifier;
125 public DataContainerNodeBuilder<I, R> addChild(
126 final DataContainerChild<? extends PathArgument, ?> child) {
127 return withChild(child);
131 public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
132 return withoutChild(key);