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