Added NormalizedNodeContainerBuilder and helpers.
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapEntryNodeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
9
10 import java.util.List;
11 import java.util.Map;
12
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;
19
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.collect.Maps;
23
24 public class ImmutableMapEntryNodeBuilder
25         extends AbstractImmutableDataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> {
26
27     protected final Map<QName, InstanceIdentifier.PathArgument> childrenQNamesToPaths;
28
29     protected ImmutableMapEntryNodeBuilder() {
30         this.childrenQNamesToPaths = Maps.newLinkedHashMap();
31     }
32
33     public static DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> create() {
34         return new ImmutableMapEntryNodeBuilder();
35     }
36
37     // FIXME, find better solution than 2 maps (map from QName to Child ?)
38
39     @Override
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());
43         }
44         return super.withValue(value);
45     }
46
47     @Override
48     public DataContainerNodeBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withChild(final DataContainerChild<?, ?> child) {
49         childrenQNamesToPaths.put(child.getNodeType(), child.getIdentifier());
50         return super.withChild(child);
51     }
52
53     @Override
54     public MapEntryNode build() {
55         checkKeys();
56         return new ImmutableMapEntryNode(nodeIdentifier, value);
57     }
58
59     private void checkKeys() {
60         for (QName keyQName : nodeIdentifier.getKeyValues().keySet()) {
61
62             InstanceIdentifier.PathArgument childNodePath = childrenQNamesToPaths.get(keyQName);
63             DataContainerChild<?, ?> childNode = value.get(childNodePath);
64
65             Preconditions.checkNotNull(childNode, "Key child node: %s, not present", keyQName);
66
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);
71         }
72     }
73
74     static final class ImmutableMapEntryNode extends AbstractImmutableDataContainerNode<InstanceIdentifier.NodeIdentifierWithPredicates> implements MapEntryNode {
75
76         ImmutableMapEntryNode(final InstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier,
77                               final Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children) {
78             super(ImmutableMap.copyOf(children), nodeIdentifier);
79         }
80     }
81 }