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.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;
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) {
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(final NodeIdentifier nodeIdentifier) {
82 this.nodeIdentifier = nodeIdentifier;
87 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
88 for (final LeafSetEntryNode<T> leafSetEntry : value) {
89 withChild(leafSetEntry);
96 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
97 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
98 b.withNodeIdentifier(new NodeWithValue(nodeIdentifier.getNodeType(), value));
100 b.withAttributes(attributes);
101 return withChild(b.build());
105 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
106 return withChildValue(value, Collections.<QName,String>emptyMap());
109 protected final static class ImmutableLeafSetNode<T> extends
110 AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
111 Immutable, LeafSetNode<T> {
113 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
115 ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier, final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
116 super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
117 this.children = children;
121 public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
122 return Optional.fromNullable(children.get(child));
126 protected int valueHashCode() {
127 return children.hashCode();
131 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
132 return children.equals(((ImmutableLeafSetNode<?>) other).children);
137 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
138 final LeafSetEntryNode<T> child) {
139 return withChild(child);
143 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
144 final PathArgument key) {
145 return withoutChild(key);