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.yang.data.api.InstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
17 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Maps;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
23 public class ImmutableLeafSetNodeBuilder<T>
24 implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
26 protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
27 protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
29 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
30 return new ImmutableLeafSetNodeBuilder<>();
33 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(LeafSetEntryNode<T> child) {
34 if(this.value == null) {
35 this.value = Maps.newLinkedHashMap();
38 this.value.put(child.getIdentifier(), child);
43 public LeafSetNode<T> build() {
44 return new ImmutableLeafSetNode<>(nodeIdentifier, value);
48 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
49 this.nodeIdentifier = nodeIdentifier;
54 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(List<LeafSetEntryNode<T>> value) {
55 for (LeafSetEntryNode<T> leafSetEntry : value) {
56 withChild(leafSetEntry);
63 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(T value) {
64 return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value));
67 final class ImmutableLeafSetNode<T> extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements LeafSetNode<T> {
69 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
71 ImmutableLeafSetNode(InstanceIdentifier.NodeIdentifier nodeIdentifier, Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
72 super(nodeIdentifier, children.values());
73 this.mappedChildren = children;
77 public Optional<LeafSetEntryNode<T>> getChild(InstanceIdentifier.NodeWithValue child) {
78 return Optional.fromNullable(mappedChildren.get(child));
82 public String toString() {
83 final StringBuffer sb = new StringBuffer("ImmutableLeafSetNode{");
84 sb.append("nodeIdentifier=").append(nodeIdentifier);
85 sb.append(", children=").append(value);