BUG-457 Fix TODOs and FIXMEs in yang-data-impl schema package.
[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.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.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
22 abstract class AbstractImmutableDataContainerNodeBuilder<I extends InstanceIdentifier.PathArgument, R extends DataContainerNode<I>>
23         implements DataContainerNodeBuilder<I, R> {
24
25     private Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> value;
26     private I nodeIdentifier;
27
28     /*
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.
33      */
34     private boolean dirty;
35
36     protected AbstractImmutableDataContainerNodeBuilder() {
37         this.value = new HashMap<>();
38         this.dirty = false;
39     }
40
41     protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
42         this.value = node.getChildren();
43         this.dirty = true;
44     }
45
46     protected final I getNodeIdentifier() {
47         return nodeIdentifier;
48     }
49
50     protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
51         return value.get(child);
52     }
53
54     protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
55         dirty = true;
56         return value;
57     }
58
59     private void checkDirty() {
60         if (dirty) {
61             value = new HashMap<>(value);
62             dirty = false;
63         }
64     }
65
66     @Override
67     public DataContainerNodeBuilder<I, R> withValue(final List<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> value) {
68         // TODO Replace or putAll ?
69         for (final DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> dataContainerChild : value) {
70             withChild(dataContainerChild);
71         }
72         return this;
73     }
74
75     @Override
76     public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
77         checkDirty();
78         this.value.put(child.getIdentifier(), child);
79         return this;
80     }
81
82     @Override
83     public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
84         checkDirty();
85         this.value.remove(key);
86         return this;
87     }
88
89     @Override
90     public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
91         this.nodeIdentifier = nodeIdentifier;
92         return this;
93     }
94
95     @Override
96     public DataContainerNodeBuilder<I, R> addChild(
97             final DataContainerChild<? extends PathArgument, ?> child) {
98         return withChild(child);
99     }
100
101     @Override
102     public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
103         return withoutChild(key);
104     }
105 }