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.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.collect.Maps;
24 public class ImmutableMapEntryNodeBuilder
25 extends AbstractImmutableDataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> {
27 protected final Map<QName, InstanceIdentifier.PathArgument> childrenQNamesToPaths;
29 protected ImmutableMapEntryNodeBuilder() {
30 this.childrenQNamesToPaths = Maps.newLinkedHashMap();
33 public static DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> create() {
34 return new ImmutableMapEntryNodeBuilder();
37 // FIXME, find better solution than 2 maps (map from QName to Child ?)
40 public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withValue(final List<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> value) {
41 for (DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> childId : value) {
42 this.childrenQNamesToPaths.put(childId.getNodeType(), childId.getIdentifier());
44 return super.withValue(value);
48 public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withChild(final DataContainerChild<?, ?> child) {
49 childrenQNamesToPaths.put(child.getNodeType(), child.getIdentifier());
50 return super.withChild(child);
54 public MapEntryNode build() {
56 return new ImmutableMapEntryNode(nodeIdentifier, value);
59 private void checkKeys() {
60 for (QName keyQName : nodeIdentifier.getKeyValues().keySet()) {
62 InstanceIdentifier.PathArgument childNodePath = childrenQNamesToPaths.get(keyQName);
63 DataContainerChild<?, ?> childNode = value.get(childNodePath);
65 Preconditions.checkNotNull(childNode, "Key child node: %s, not present", keyQName);
67 Object actualValue = nodeIdentifier.getKeyValues().get(keyQName);
68 Object expectedValue = childNode.getValue();
69 Preconditions.checkArgument(expectedValue.equals(actualValue),
70 "Key child node with unexpected value, is: %s, should be: %s", actualValue, expectedValue);
74 static final class ImmutableMapEntryNode extends AbstractImmutableDataContainerNode<InstanceIdentifier.NodeIdentifierWithPredicates> implements MapEntryNode {
76 ImmutableMapEntryNode(final InstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier,
77 final Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children) {
78 super(ImmutableMap.copyOf(children), nodeIdentifier);