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.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
20 import com.google.common.base.Optional;
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.Iterables;
23 import com.google.common.collect.Maps;
25 public class ImmutableMapNodeBuilder
26 implements CollectionNodeBuilder<MapEntryNode, MapNode> {
28 protected Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value = Maps.newLinkedHashMap();
29 protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
31 public static CollectionNodeBuilder<MapEntryNode, MapNode> create() {
32 return new ImmutableMapNodeBuilder();
36 public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
37 this.value.put(child.getIdentifier(), child);
42 public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(final List<MapEntryNode> value) {
43 // TODO replace or putAll ?
44 for (final MapEntryNode mapEntryNode : value) {
45 withChild(mapEntryNode);
52 public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
53 this.nodeIdentifier = nodeIdentifier;
58 public MapNode build() {
59 return new ImmutableMapNode(nodeIdentifier, value);
63 public CollectionNodeBuilder<MapEntryNode, MapNode> addChild(
64 final MapEntryNode child) {
65 return withChild(child);
68 static final class ImmutableMapNode extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements Immutable,MapNode {
70 private final Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mappedChildren;
72 ImmutableMapNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
73 final Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children) {
74 super(nodeIdentifier, ImmutableList.copyOf(children.values()));
75 this.mappedChildren = children;
79 public Optional<MapEntryNode> getChild(final InstanceIdentifier.NodeIdentifierWithPredicates child) {
80 return Optional.fromNullable(mappedChildren.get(child));
84 protected int valueHashCode() {
85 return mappedChildren.hashCode();
89 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
90 return mappedChildren.equals(((ImmutableMapNode) other).mappedChildren);