0edc0b9f2573c65b6b9db07dacc2f97a737596d4
[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.collect.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.util.MapAdaptor;
17 import org.opendaylight.yangtools.util.UnmodifiableCollection;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
27
28 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
29     private static final int DEFAULT_CAPACITY = 4;
30
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         if (sizeHint >= 0) {
40             value = Maps.newHashMapWithExpectedSize(sizeHint);
41         } else {
42             value = new HashMap<>(DEFAULT_CAPACITY);
43         }
44     }
45
46     protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
47         nodeIdentifier = node.getIdentifier();
48         value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
49     }
50
51     public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
52         return new ImmutableLeafSetNodeBuilder<>();
53     }
54
55     public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
56         return new ImmutableLeafSetNodeBuilder<>(sizeHint);
57     }
58
59     public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
60         if (!(node instanceof ImmutableLeafSetNode<?>)) {
61             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
62         }
63
64         return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
65     }
66
67     @Override
68     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
69         this.value.put(child.getIdentifier(), child);
70         return this;
71     }
72
73     @Override
74     public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
75         this.value.remove(key);
76         return this;
77     }
78
79     @Override
80     public LeafSetNode<T> build() {
81         return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
82     }
83
84     @Override
85     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
86         this.nodeIdentifier = withNodeIdentifier;
87         return this;
88     }
89
90     @Override
91     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
92         for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
93             withChild(leafSetEntry);
94         }
95         return this;
96     }
97
98     @Override
99     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
100         return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
101             .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
102             .withValue(childValue).build());
103     }
104
105     protected static final class ImmutableLeafSetNode<T> extends
106             AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
107             LeafSetNode<T> {
108
109         private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
110
111         ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
112                 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
113             super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
114             this.children = children;
115         }
116
117         @Override
118         public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
119             return Optional.ofNullable(children.get(child));
120         }
121
122         @Override
123         public int size() {
124             return children.size();
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>>
146             removeChild(final PathArgument key) {
147         return withoutChild(key);
148     }
149
150 }