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 java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.util.MapAdaptor;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
28 import com.google.common.base.Optional;
29 import com.google.common.collect.Iterables;
31 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
33 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
34 private InstanceIdentifier.NodeIdentifier nodeIdentifier;
36 protected ImmutableLeafSetNodeBuilder() {
37 value = new HashMap<>();
40 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
41 nodeIdentifier = node.getIdentifier();
42 value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
45 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
46 return new ImmutableLeafSetNodeBuilder<>();
49 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
50 if (!(node instanceof ImmutableLeafSetNode<?>)) {
51 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
54 return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
58 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
59 this.value.put(child.getIdentifier(), child);
64 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
65 this.value.remove(key);
70 public LeafSetNode<T> build() {
71 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
75 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
76 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
77 this.nodeIdentifier = nodeIdentifier;
82 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
83 for (final LeafSetEntryNode<T> leafSetEntry : value) {
84 withChild(leafSetEntry);
91 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
92 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
93 b.withNodeIdentifier(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
95 b.withAttributes(attributes);
96 return withChild(b.build());
100 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
101 return withChildValue(value, Collections.<QName,String>emptyMap());
104 protected final static class ImmutableLeafSetNode<T> extends
105 AbstractImmutableNormalizedValueNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
106 Immutable, LeafSetNode<T> {
108 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
110 ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
111 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
112 super(nodeIdentifier, Iterables.unmodifiableIterable(children.values()));
113 this.children = children;
117 public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
118 return Optional.fromNullable(children.get(child));
122 protected int valueHashCode() {
123 return children.hashCode();
127 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
128 return children.equals(((ImmutableLeafSetNode<?>) other).children);
133 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
134 final LeafSetEntryNode<T> child) {
135 return withChild(child);
139 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
140 final PathArgument key) {
141 return withoutChild(key);