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.Collections;
13 import java.util.HashMap;
15 import java.util.Optional;
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;
29 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
30 private static final int DEFAULT_CAPACITY = 4;
32 private final Map<NodeWithValue, LeafSetEntryNode<T>> value;
33 private NodeIdentifier nodeIdentifier;
35 protected ImmutableLeafSetNodeBuilder() {
36 value = new HashMap<>(DEFAULT_CAPACITY);
39 protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
41 value = Maps.newHashMapWithExpectedSize(sizeHint);
43 value = new HashMap<>(DEFAULT_CAPACITY);
47 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
48 nodeIdentifier = node.getIdentifier();
49 value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
52 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
53 return new ImmutableLeafSetNodeBuilder<>();
56 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
57 return new ImmutableLeafSetNodeBuilder<>(sizeHint);
60 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
61 if (!(node instanceof ImmutableLeafSetNode<?>)) {
62 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
65 return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
69 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
70 this.value.put(child.getIdentifier(), child);
75 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
76 this.value.remove(key);
81 public LeafSetNode<T> build() {
82 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
86 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
87 this.nodeIdentifier = withNodeIdentifier;
92 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
93 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
94 withChild(leafSetEntry);
101 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue,
102 final Map<QName, String> attributes) {
103 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
104 b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue));
105 b.withValue(childValue);
106 b.withAttributes(attributes);
107 return withChild(b.build());
111 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
112 return withChildValue(childValue, Collections.emptyMap());
115 protected static final class ImmutableLeafSetNode<T> extends
116 AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
119 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
121 ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
122 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
123 super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
124 this.children = children;
128 public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
129 return Optional.ofNullable(children.get(child));
133 protected int valueHashCode() {
134 return children.hashCode();
138 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
139 return children.equals(((ImmutableLeafSetNode<?>) other).children);
144 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
145 final LeafSetEntryNode<T> child) {
146 return withChild(child);
150 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
151 removeChild(final PathArgument key) {
152 return withoutChild(key);