Allow shared JSONNNSWriter use
[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 com.google.common.collect.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Map;
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;
23
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;
28
29     /*
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.
34      */
35     private boolean dirty;
36
37     protected AbstractImmutableDataContainerNodeBuilder() {
38         this.value = new HashMap<>(DEFAULT_CAPACITY);
39         this.dirty = false;
40     }
41
42     protected AbstractImmutableDataContainerNodeBuilder(final int sizeHint) {
43         if (sizeHint >= 0) {
44             this.value = Maps.newHashMapWithExpectedSize(sizeHint);
45         } else {
46             this.value = new HashMap<>(DEFAULT_CAPACITY);
47         }
48         this.dirty = false;
49     }
50
51     protected AbstractImmutableDataContainerNodeBuilder(final AbstractImmutableDataContainerNode<I> node) {
52         this.nodeIdentifier = node.getIdentifier();
53
54         /*
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
59          * we need.
60          */
61         this.value = node.getChildren();
62         this.dirty = true;
63     }
64
65     protected final I getNodeIdentifier() {
66         return nodeIdentifier;
67     }
68
69     protected final DataContainerChild<? extends PathArgument, ?> getChild(final PathArgument child) {
70         return value.get(child);
71     }
72
73     protected final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> buildValue() {
74         if (value instanceof ModifiableMapPhase) {
75             return ((ModifiableMapPhase<PathArgument, DataContainerChild<? extends PathArgument, ?>>)value).toUnmodifiableMap();
76         }
77
78         dirty = true;
79         return value;
80     }
81
82     private void checkDirty() {
83         if (dirty) {
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();
88             } else {
89                 value = new HashMap<>(value);
90             }
91             dirty = false;
92         }
93     }
94
95     @Override
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);
100         }
101         return this;
102     }
103
104     @Override
105     public DataContainerNodeBuilder<I, R> withChild(final DataContainerChild<?, ?> child) {
106         checkDirty();
107         this.value.put(child.getIdentifier(), child);
108         return this;
109     }
110
111     @Override
112     public DataContainerNodeBuilder<I, R> withoutChild(final PathArgument key) {
113         checkDirty();
114         this.value.remove(key);
115         return this;
116     }
117
118     @Override
119     public DataContainerNodeBuilder<I, R> withNodeIdentifier(final I nodeIdentifier) {
120         this.nodeIdentifier = nodeIdentifier;
121         return this;
122     }
123
124     @Override
125     public DataContainerNodeBuilder<I, R> addChild(
126             final DataContainerChild<? extends PathArgument, ?> child) {
127         return withChild(child);
128     }
129
130     @Override
131     public NormalizedNodeContainerBuilder<I, PathArgument, DataContainerChild<? extends PathArgument, ?>, R> removeChild(final PathArgument key) {
132         return withoutChild(key);
133     }
134 }