Merge "BUG-731: eliminate magic constants"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeBuilder.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.Collections;
11 import java.util.LinkedHashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
25
26 import com.google.common.base.Optional;
27 import com.google.common.collect.Iterables;
28
29 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
30
31     private Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
32     private InstanceIdentifier.NodeIdentifier nodeIdentifier;
33     private boolean dirty;
34
35     protected ImmutableLeafSetNodeBuilder() {
36         value = new LinkedHashMap<>();
37         dirty = false;
38     }
39
40     protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
41         nodeIdentifier = node.getIdentifier();
42         value = node.getChildren();
43         dirty = true;
44     }
45
46     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
47         return new ImmutableLeafSetNodeBuilder<>();
48     }
49
50     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
51         if (!(node instanceof ImmutableLeafSetNode<?>)) {
52             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
53         }
54
55         return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
56     }
57
58     private void checkDirty() {
59         if (dirty) {
60             value = new LinkedHashMap<>(value);
61             dirty = false;
62         }
63     }
64
65     @Override
66     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
67         checkDirty();
68         this.value.put(child.getIdentifier(), child);
69         return this;
70     }
71
72     @Override
73     public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
74         checkDirty();
75         this.value.remove(key);
76         return this;
77     }
78
79     @Override
80     public LeafSetNode<T> build() {
81         dirty = true;
82         return new ImmutableLeafSetNode<>(nodeIdentifier, value);
83     }
84
85     @Override
86     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
87             final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
88         this.nodeIdentifier = nodeIdentifier;
89         return this;
90     }
91
92     @Override
93     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
94         checkDirty();
95         for (final LeafSetEntryNode<T> leafSetEntry : value) {
96             withChild(leafSetEntry);
97         }
98         return this;
99     }
100
101
102     @Override
103     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
104         final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
105         b.withNodeIdentifier(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
106         b.withValue(value);
107         b.withAttributes(attributes);
108         return withChild(b.build());
109     }
110
111     @Override
112     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
113         return withChildValue(value, Collections.<QName,String>emptyMap());
114     }
115
116     protected final static class ImmutableLeafSetNode<T> extends
117             AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
118             Immutable, LeafSetNode<T> {
119
120         private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
121
122         ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
123                 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
124             super(nodeIdentifier, Iterables.unmodifiableIterable(children.values()));
125             this.children = children;
126         }
127
128         @Override
129         public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
130             return Optional.fromNullable(children.get(child));
131         }
132
133         @Override
134         protected int valueHashCode() {
135             return children.hashCode();
136         }
137
138         private Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> getChildren() {
139             return Collections.unmodifiableMap(children);
140         }
141
142         @Override
143         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
144             return children.equals(((ImmutableLeafSetNode<?>) other).children);
145         }
146     }
147
148     @Override
149     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
150             final LeafSetEntryNode<T> child) {
151         return withChild(child);
152     }
153
154     @Override
155     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
156             final PathArgument key) {
157         return withoutChild(key);
158     }
159
160 }