6c45323d60e579fecacd2aca639d791226a61a4c
[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 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.builder.impl.valid.DataValidationException;
17 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableDataContainerNode;
18
19 public class ImmutableAugmentationNodeBuilder
20         extends AbstractImmutableDataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> {
21
22     protected ImmutableAugmentationNodeBuilder() {
23         super();
24     }
25
26     protected ImmutableAugmentationNodeBuilder(final int sizeHint) {
27         super(sizeHint);
28     }
29
30     public ImmutableAugmentationNodeBuilder(final ImmutableAugmentationNode node) {
31         super(node);
32     }
33
34     public static DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> create() {
35         return new ImmutableAugmentationNodeBuilder();
36     }
37
38     public static DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> create(final int sizeHint) {
39         return new ImmutableAugmentationNodeBuilder(sizeHint);
40     }
41
42     public static DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> create(
43             final AugmentationNode node) {
44         if (!(node instanceof ImmutableAugmentationNode)) {
45             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
46         }
47
48         return new ImmutableAugmentationNodeBuilder((ImmutableAugmentationNode)node);
49     }
50
51     @Override
52     public DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> withChild(
53             final DataContainerChild<?, ?> child) {
54         // Check nested augments
55         DataValidationException.checkLegalData(!(child instanceof AugmentationNode),
56                 "Unable to add: %s, as a child for: %s, Nested augmentations are not permitted", child.getNodeType(),
57                 getNodeIdentifier() == null ? this : getNodeIdentifier());
58
59         return super.withChild(child);
60     }
61
62     @Override
63     public DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> withoutChild(final PathArgument key) {
64         return super.withoutChild(key);
65     }
66
67     @Override
68     public AugmentationNode build() {
69         return new ImmutableAugmentationNode(getNodeIdentifier(), buildValue());
70     }
71
72     private static final class ImmutableAugmentationNode
73             extends AbstractImmutableDataContainerNode<AugmentationIdentifier> implements AugmentationNode {
74
75         ImmutableAugmentationNode(final AugmentationIdentifier nodeIdentifier,
76                 final Map<PathArgument, DataContainerChild<? extends PathArgument, ?>> children) {
77             super(children, nodeIdentifier);
78         }
79     }
80 }