Merge "Fixed possible NPE in CodecMapping."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableAugmentationNodeBuilder.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.Map;
11
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
16 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
17
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.ImmutableMap;
20
21 public class ImmutableAugmentationNodeBuilder
22         extends AbstractImmutableDataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> {
23
24     public static DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> create() {
25         return new ImmutableAugmentationNodeBuilder();
26     }
27
28     @Override
29     public DataContainerNodeBuilder<InstanceIdentifier.AugmentationIdentifier, AugmentationNode> withChild(
30             final DataContainerChild<?, ?> child) {
31         // Check nested augments
32         Preconditions.checkArgument(child instanceof AugmentationNode == false,
33                 "Unable to add: %s, as a child for: %s, Nested augmentations are not permitted", child.getNodeType(),
34                 nodeIdentifier == null ? this : nodeIdentifier);
35
36         return super.withChild(child);
37     }
38
39     @Override
40     public AugmentationNode build() {
41         return new ImmutableAugmentationNode(nodeIdentifier, value);
42     }
43
44     static final class ImmutableAugmentationNode
45             extends AbstractImmutableDataContainerNode<InstanceIdentifier.AugmentationIdentifier>
46             implements AugmentationNode {
47
48         ImmutableAugmentationNode(final InstanceIdentifier.AugmentationIdentifier nodeIdentifier, final Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children) {
49             super(ImmutableMap.copyOf(children), nodeIdentifier);
50         }
51     }
52 }