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;
27 import com.google.common.base.Optional;
28 import com.google.common.collect.Iterables;
30 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
32 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
33 private InstanceIdentifier.NodeIdentifier nodeIdentifier;
35 protected ImmutableLeafSetNodeBuilder() {
36 value = new HashMap<>();
39 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
40 nodeIdentifier = node.getIdentifier();
41 value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
44 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
45 return new ImmutableLeafSetNodeBuilder<>();
48 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
49 if (!(node instanceof ImmutableLeafSetNode<?>)) {
50 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
53 return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
57 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
58 this.value.put(child.getIdentifier(), child);
63 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
64 this.value.remove(key);
69 public LeafSetNode<T> build() {
70 return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
74 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
75 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
76 this.nodeIdentifier = nodeIdentifier;
81 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
82 for (final LeafSetEntryNode<T> leafSetEntry : value) {
83 withChild(leafSetEntry);
90 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
91 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
92 b.withNodeIdentifier(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
94 b.withAttributes(attributes);
95 return withChild(b.build());
99 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
100 return withChildValue(value, Collections.<QName,String>emptyMap());
103 protected final static class ImmutableLeafSetNode<T> extends
104 AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
105 Immutable, LeafSetNode<T> {
107 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
109 ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
110 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
111 super(nodeIdentifier, Iterables.unmodifiableIterable(children.values()));
112 this.children = children;
116 public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
117 return Optional.fromNullable(children.get(child));
121 protected int valueHashCode() {
122 return children.hashCode();
126 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
127 return children.equals(((ImmutableLeafSetNode<?>) other).children);
132 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
133 final LeafSetEntryNode<T> child) {
134 return withChild(child);
138 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
139 final PathArgument key) {
140 return withoutChild(key);