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.List;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableList;
27 import com.google.common.collect.Iterables;
28 import com.google.common.collect.Maps;
30 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
32 protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value = Maps.newLinkedHashMap();
33 protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
35 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
36 return new ImmutableLeafSetNodeBuilder<>();
40 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
41 this.value.put(child.getIdentifier(), child);
46 public LeafSetNode<T> build() {
47 return new ImmutableLeafSetNode<>(nodeIdentifier, value);
51 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
52 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
53 this.nodeIdentifier = nodeIdentifier;
58 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
59 for (final LeafSetEntryNode<T> leafSetEntry : value) {
60 withChild(leafSetEntry);
68 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
69 return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(
70 new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value, attributes));
75 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
76 return withChildValue(value, Collections.<QName,String>emptyMap());
80 private final static class ImmutableLeafSetNode<T> extends
81 AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
82 Immutable, LeafSetNode<T> {
84 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
86 ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
87 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
88 super(nodeIdentifier, ImmutableList.copyOf(children.values()));
89 this.mappedChildren = children;
93 public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
94 return Optional.fromNullable(mappedChildren.get(child));
98 protected int valueHashCode() {
99 return mappedChildren.hashCode();
103 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
104 return mappedChildren.equals(((ImmutableLeafSetNode<?>) other).mappedChildren);
109 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
110 final LeafSetEntryNode<T> child) {
111 return withChild(child);