31fc2270c9f4185f7f4ec0f992e0d5e263806815
[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.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
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<NodeWithValue, LeafSetEntryNode<T>> value;
32     private 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(final NodeIdentifier nodeIdentifier) {
82         this.nodeIdentifier = nodeIdentifier;
83         return this;
84     }
85
86     @Override
87     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
88         for (final LeafSetEntryNode<T> leafSetEntry : value) {
89             withChild(leafSetEntry);
90         }
91         return this;
92     }
93
94
95     @Override
96     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
97         final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
98         b.withNodeIdentifier(new NodeWithValue(nodeIdentifier.getNodeType(), value));
99         b.withValue(value);
100         b.withAttributes(attributes);
101         return withChild(b.build());
102     }
103
104     @Override
105     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
106         return withChildValue(value, Collections.<QName,String>emptyMap());
107     }
108
109     protected final static class ImmutableLeafSetNode<T> extends
110             AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
111             Immutable, LeafSetNode<T> {
112
113         private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
114
115         ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier, final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
116             super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
117             this.children = children;
118         }
119
120         @Override
121         public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
122             return Optional.fromNullable(children.get(child));
123         }
124
125         @Override
126         protected int valueHashCode() {
127             return children.hashCode();
128         }
129
130         @Override
131         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
132             return children.equals(((ImmutableLeafSetNode<?>) other).children);
133         }
134     }
135
136     @Override
137     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
138             final LeafSetEntryNode<T> child) {
139         return withChild(child);
140     }
141
142     @Override
143     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
144             final PathArgument key) {
145         return withoutChild(key);
146     }
147
148 }