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.List;
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Maps;
26 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
28 protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value = Maps.newLinkedHashMap();
29 protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
31 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
32 return new ImmutableLeafSetNodeBuilder<>();
36 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
37 this.value.put(child.getIdentifier(), child);
42 public LeafSetNode<T> build() {
43 return new ImmutableLeafSetNode<>(nodeIdentifier, value);
47 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
48 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
49 this.nodeIdentifier = nodeIdentifier;
54 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
55 for (LeafSetEntryNode<T> leafSetEntry : value) {
56 withChild(leafSetEntry);
63 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
64 return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(
65 new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value));
68 private final static class ImmutableLeafSetNode<T> extends
69 AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
70 Immutable, LeafSetNode<T> {
72 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
74 ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
75 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
76 super(nodeIdentifier, children.values());
77 this.mappedChildren = children;
81 public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
82 return Optional.fromNullable(mappedChildren.get(child));
86 public String toString() {
87 final StringBuffer sb = new StringBuffer("ImmutableLeafSetNode{");
88 sb.append("nodeIdentifier=").append(nodeIdentifier);
89 sb.append(", children=").append(value);
96 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
97 final LeafSetEntryNode<T> child) {
98 return withChild(child);