2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
10 import com.google.common.collect.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.util.MapAdaptor;
16 import org.opendaylight.yangtools.util.UnmodifiableCollection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.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.api.schema.SystemLeafSetNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, SystemLeafSetNode<T>> {
28 private static final int DEFAULT_CAPACITY = 4;
30 private final Map<NodeWithValue<?>, LeafSetEntryNode<T>> value;
32 private NodeIdentifier nodeIdentifier;
34 protected ImmutableLeafSetNodeBuilder() {
35 value = new HashMap<>(DEFAULT_CAPACITY);
38 protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
40 value = Maps.newHashMapWithExpectedSize(sizeHint);
42 value = new HashMap<>(DEFAULT_CAPACITY);
46 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
47 nodeIdentifier = node.getIdentifier();
48 value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
51 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create() {
52 return new ImmutableLeafSetNodeBuilder<>();
55 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final int sizeHint) {
56 return new ImmutableLeafSetNodeBuilder<>(sizeHint);
59 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final SystemLeafSetNode<T> node) {
60 if (node instanceof ImmutableLeafSetNode) {
61 return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
63 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
66 @Deprecated(since = "6.0.7", forRemoval = true)
67 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final LeafListSchemaNode schema) {
68 return new SchemaAwareImmutableLeafSetNodeBuilder<>(schema);
71 @Deprecated(since = "6.0.7", forRemoval = true)
72 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final LeafListSchemaNode schema,
73 final LeafSetNode<T> node) {
74 if (node instanceof ImmutableLeafSetNode) {
75 return new SchemaAwareImmutableLeafSetNodeBuilder<>(schema, (ImmutableLeafSetNode<T>) node);
77 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
81 public ImmutableLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
82 this.value.put(child.getIdentifier(), child);
87 public ImmutableLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
88 this.value.remove(key);
93 public SystemLeafSetNode<T> build() {
94 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
98 public ImmutableLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
99 this.nodeIdentifier = withNodeIdentifier;
104 public ImmutableLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
105 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
106 withChild(leafSetEntry);
112 public ImmutableLeafSetNodeBuilder<T> withChildValue(final T childValue) {
113 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
114 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
115 .withValue(childValue).build());
120 public ImmutableLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
121 return withChild(child);
125 public ImmutableLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
126 return withoutChild(key);
129 protected static final class ImmutableLeafSetNode<T>
130 extends AbstractImmutableNormalizedValueNode<NodeIdentifier, SystemLeafSetNode<?>,
131 Collection<@NonNull LeafSetEntryNode<T>>>
132 implements SystemLeafSetNode<T> {
134 private final Map<NodeWithValue<?>, LeafSetEntryNode<T>> children;
136 ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
137 final Map<NodeWithValue<?>, LeafSetEntryNode<T>> children) {
138 super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
139 this.children = children;
143 public LeafSetEntryNode<T> childByArg(final NodeWithValue<?> child) {
144 return children.get(child);
149 return children.size();
153 protected Class<SystemLeafSetNode<?>> implementedType() {
154 return (Class) SystemLeafSetNode.class;
158 protected int valueHashCode() {
159 return children.hashCode();
163 protected boolean valueEquals(final SystemLeafSetNode<?> other) {
164 if (other instanceof ImmutableLeafSetNode) {
165 return children.equals(((ImmutableLeafSetNode<?>) other).children);
167 if (size() != other.size()) {
170 for (var child : children.values()) {
171 if (!child.equals(other.childByArg(child.getIdentifier()))) {