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.base.Optional;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
15 import org.opendaylight.yangtools.concepts.Immutable;
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;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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;
31 private final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
32 private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
34 protected ImmutableLeafSetNodeBuilder() {
35 value = new HashMap<>(DEFAULT_CAPACITY);
38 protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
39 value = new HashMap<>(sizeHint * 4 / 3);
42 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
43 nodeIdentifier = node.getIdentifier();
44 value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
47 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
48 return new ImmutableLeafSetNodeBuilder<>();
51 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
52 return new ImmutableLeafSetNodeBuilder<>(sizeHint);
55 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
56 if (!(node instanceof ImmutableLeafSetNode<?>)) {
57 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
60 return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
64 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
65 this.value.put(child.getIdentifier(), child);
70 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
71 this.value.remove(key);
76 public LeafSetNode<T> build() {
77 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
81 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
82 final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
83 this.nodeIdentifier = nodeIdentifier;
88 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
89 for (final LeafSetEntryNode<T> leafSetEntry : value) {
90 withChild(leafSetEntry);
97 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
98 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
99 b.withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
101 b.withAttributes(attributes);
102 return withChild(b.build());
106 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
107 return withChildValue(value, Collections.<QName,String>emptyMap());
110 protected final static class ImmutableLeafSetNode<T> extends
111 AbstractImmutableNormalizedValueNode<YangInstanceIdentifier.NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
112 Immutable, LeafSetNode<T> {
114 private final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
116 ImmutableLeafSetNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
117 final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
118 super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
119 this.children = children;
123 public Optional<LeafSetEntryNode<T>> getChild(final YangInstanceIdentifier.NodeWithValue child) {
124 return Optional.fromNullable(children.get(child));
128 protected int valueHashCode() {
129 return children.hashCode();
133 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
134 return children.equals(((ImmutableLeafSetNode<?>) other).children);
139 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
140 final LeafSetEntryNode<T> child) {
141 return withChild(child);
145 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
146 final PathArgument key) {
147 return withoutChild(key);