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 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;
28 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
29 private static final int DEFAULT_CAPACITY = 4;
31 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, LeafSetEntryNode<T>> create() {
52 return new ImmutableLeafSetNodeBuilder<>();
55 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
56 return new ImmutableLeafSetNodeBuilder<>(sizeHint);
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()));
64 return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
68 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
69 this.value.put(child.getIdentifier(), child);
74 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
75 this.value.remove(key);
80 public LeafSetNode<T> build() {
81 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
85 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
86 this.nodeIdentifier = withNodeIdentifier;
91 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
92 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
93 withChild(leafSetEntry);
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());
105 protected static final class ImmutableLeafSetNode<T> extends
106 AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
109 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
111 ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
112 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
113 super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
114 this.children = children;
118 public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
119 return Optional.ofNullable(children.get(child));
123 protected int valueHashCode() {
124 return children.hashCode();
128 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
129 return children.equals(((ImmutableLeafSetNode<?>) other).children);
134 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
135 final LeafSetEntryNode<T> child) {
136 return withChild(child);
140 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
141 removeChild(final PathArgument key) {
142 return withoutChild(key);