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 org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.util.MapAdaptor;
16 import org.opendaylight.yangtools.util.UnmodifiableCollection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
25 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, SystemLeafSetNode<T>> {
26 private static final int DEFAULT_CAPACITY = 4;
28 private final Map<NodeWithValue<?>, LeafSetEntryNode<T>> value;
30 private NodeIdentifier nodeIdentifier;
32 protected ImmutableLeafSetNodeBuilder() {
33 value = new HashMap<>(DEFAULT_CAPACITY);
36 protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
38 value = Maps.newHashMapWithExpectedSize(sizeHint);
40 value = new HashMap<>(DEFAULT_CAPACITY);
44 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
45 nodeIdentifier = node.getIdentifier();
46 value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
49 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create() {
50 return new ImmutableLeafSetNodeBuilder<>();
53 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final int sizeHint) {
54 return new ImmutableLeafSetNodeBuilder<>(sizeHint);
57 public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final SystemLeafSetNode<T> node) {
58 if (!(node instanceof ImmutableLeafSetNode<?>)) {
59 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
62 return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
66 public ImmutableLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
67 this.value.put(child.getIdentifier(), child);
72 public ImmutableLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
73 this.value.remove(key);
78 public SystemLeafSetNode<T> build() {
79 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
83 public ImmutableLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
84 this.nodeIdentifier = withNodeIdentifier;
89 public ImmutableLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
90 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
91 withChild(leafSetEntry);
97 public ImmutableLeafSetNodeBuilder<T> withChildValue(final T childValue) {
98 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
99 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
100 .withValue(childValue).build());
105 public ImmutableLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
106 return withChild(child);
110 public ImmutableLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
111 return withoutChild(key);
114 protected static final class ImmutableLeafSetNode<T>
115 extends AbstractImmutableNormalizedValueNode<NodeIdentifier, SystemLeafSetNode<?>,
116 Collection<@NonNull LeafSetEntryNode<T>>>
117 implements SystemLeafSetNode<T> {
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 LeafSetEntryNode<T> childByArg(final NodeWithValue<?> child) {
129 return children.get(child);
134 return children.size();
138 protected Class<SystemLeafSetNode<?>> implementedType() {
139 return (Class) SystemLeafSetNode.class;
143 protected int valueHashCode() {
144 return children.hashCode();
148 protected boolean valueEquals(final SystemLeafSetNode<?> other) {
149 return children.equals(((ImmutableLeafSetNode<?>) other).children);