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.ImmutableMap;
11 import com.google.common.collect.Maps;
12 import java.util.Collection;
13 import java.util.HashMap;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.util.MapAdaptor;
18 import org.opendaylight.yangtools.util.UnmodifiableCollection;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
30 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
31 private static final int DEFAULT_CAPACITY = 4;
33 private final Map<NodeWithValue, LeafSetEntryNode<T>> value;
34 private NodeIdentifier nodeIdentifier;
36 protected ImmutableLeafSetNodeBuilder() {
37 value = new HashMap<>(DEFAULT_CAPACITY);
40 protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
42 value = Maps.newHashMapWithExpectedSize(sizeHint);
44 value = new HashMap<>(DEFAULT_CAPACITY);
48 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
49 nodeIdentifier = node.getIdentifier();
50 value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
53 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
54 return new ImmutableLeafSetNodeBuilder<>();
57 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
58 return new ImmutableLeafSetNodeBuilder<>(sizeHint);
61 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
62 if (!(node instanceof ImmutableLeafSetNode<?>)) {
63 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
66 return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
70 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
71 this.value.put(child.getIdentifier(), child);
76 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
77 this.value.remove(key);
82 public LeafSetNode<T> build() {
83 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
87 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
88 this.nodeIdentifier = withNodeIdentifier;
93 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
94 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
95 withChild(leafSetEntry);
102 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue,
103 final Map<QName, String> attributes) {
104 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
105 b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue));
106 b.withValue(childValue);
107 b.withAttributes(attributes);
108 return withChild(b.build());
112 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
113 return withChildValue(childValue, ImmutableMap.of());
116 protected static final class ImmutableLeafSetNode<T> extends
117 AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
120 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
122 ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
123 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
124 super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
125 this.children = children;
129 public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
130 return Optional.ofNullable(children.get(child));
134 protected int valueHashCode() {
135 return children.hashCode();
139 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
140 return children.equals(((ImmutableLeafSetNode<?>) other).children);
145 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
146 final LeafSetEntryNode<T> child) {
147 return withChild(child);
151 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
152 removeChild(final PathArgument key) {
153 return withoutChild(key);