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.MapEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
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;
22 public class ImmutableMapNodeBuilder
23 implements CollectionNodeBuilder<MapEntryNode, MapNode> {
25 protected Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value;
26 protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
28 public static CollectionNodeBuilder<MapEntryNode, MapNode> create() {
29 return new ImmutableMapNodeBuilder();
32 public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(MapEntryNode child) {
33 if(this.value == null) {
34 this.value = Maps.newLinkedHashMap();
37 this.value.put(child.getIdentifier(), child);
42 public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(List<MapEntryNode> value) {
43 // TODO replace or putAll ?
44 for (MapEntryNode mapEntryNode : value) {
45 withChild(mapEntryNode);
51 public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(InstanceIdentifier.NodeIdentifier nodeIdentifier) {
52 this.nodeIdentifier = nodeIdentifier;
57 public MapNode build() {
58 return new ImmutableMapNode(nodeIdentifier, value);
61 static final class ImmutableMapNode extends AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<MapEntryNode>> implements MapNode {
63 private final Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> mappedChildren;
65 ImmutableMapNode(InstanceIdentifier.NodeIdentifier nodeIdentifier,
66 Map<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children) {
67 super(nodeIdentifier, children.values());
68 this.mappedChildren = children;
72 public Optional<MapEntryNode> getChild(InstanceIdentifier.NodeIdentifierWithPredicates child) {
73 return Optional.fromNullable(mappedChildren.get(child));
77 public String toString() {
78 final StringBuffer sb = new StringBuffer("ImmutableMapNode{");
79 sb.append("nodeIdentifier=").append(nodeIdentifier);
80 sb.append(", children=").append(mappedChildren);