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