02e6a44443989208898c2e3ec311bd64769c47d4
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / AbstractImmutableDataContainerNodeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
9
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.Map;
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;
20 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.CloneableChildrenMap;
21
22 abstract class AbstractImmutableDataContainerNodeBuilder<I extends YangInstanceIdentifier.PathArgument, R extends DataContainerNode<I>> implements DataContainerNodeBuilder<I, R> {
23     private static final int DEFAULT_CAPACITY = 4;
24     private Map<YangInstanceIdentifier.PathArgument, DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value;
25     private I nodeIdentifier;
26
27     /*
28      * Tracks whether the builder is dirty, e.g. whether the value map has been used
29      * to construct a child. If it has, we detect this condition before any further
30      * modification and create a new value map with same contents. This way we do not
31      * force a map copy if the builder is not reused.
32      */
33     private boolean dirty;
34
35     protected AbstractImmutableDataContainerNodeBuilder() {
36         this.value = new HashMap<>(DEFAULT_CAPACITY);
37         this.dirty = false;
38     }
39
40     protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
41         this.value = new HashMap<>(DEFAULT_CAPACITY);
42         this.dirty = false;
43     }
44
45     protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
46         this.nodeIdentifier = node.getIdentifier();
47
48         /*
49          * This quite awkward. What we actually want to be saying here is: give me
50          * a copy-on-write view of your children. The API involved in that could be
51          * a bit hairy, so we do the next best thing and rely on the fact that the
52          * returned object implements a specific interface, which leaks the functionality
53          * we need.
54          */
55         this.value = node.getChildren();
56         this.dirty = true;
57     }
58
59     protected final I getNodeIdentifier() {
60         return nodeIdentifier;
61     }
62
63     protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
64         return value.get(child);
65     }
66
67     protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
68         dirty = true;
69         return value;
70     }
71
72     private void checkDirty() {
73         if (dirty) {
74             if (value instanceof CloneableChildrenMap) {
75                 value = ((CloneableChildrenMap) value).createMutableClone();
76             } else {
77                 value = new HashMap<>(value);
78             }
79             dirty = false;
80         }
81     }
82
83     @Override
84     public DataContainerNodeBuilder<I, R> withValue(final Collection<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> value) {
85         // TODO Replace or putAll ?
86         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : value) {
87             withChild(dataContainerChild);
88         }
89         return this;
90     }
91
92     @Override
93     public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
94         checkDirty();
95         this.value.put(child.getIdentifier(), child);
96         return this;
97     }
98
99     @Override
100     public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
101         checkDirty();
102         this.value.remove(key);
103         return this;
104     }
105
106     @Override
107     public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
108         this.nodeIdentifier = nodeIdentifier;
109         return this;
110     }
111
112     @Override
113     public DataContainerNodeBuilder<I, R> addChild(
114             final DataContainerChild<? extends PathArgument, ?> child) {
115         return withChild(child);
116     }
117
118     @Override
119     public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
120         return withoutChild(key);
121     }
122 }