Eliminate AbstractMagnesiumDataOutput
[yangtools.git] / data / 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.api.schema.builder.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     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 immutableNode)) {
46             throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
47         }
48         return new ImmutableAugmentationNodeBuilder(immutableNode);
49     }
50
51     @Override
52     public DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> withChild(
53             final DataContainerChild child) {
54         // Check nested augments
55         if (child instanceof AugmentationNode aug) {
56             final var myId = getNodeIdentifier();
57             throw new DataValidationException(String.format(
58                 "Unable to add: %s, as a child for: %s, Nested augmentations are not permitted", aug.getIdentifier(),
59                 myId == null ? this : myId));
60         }
61
62         return super.withChild(child);
63     }
64
65     @Override
66     public DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> withoutChild(final PathArgument key) {
67         return super.withoutChild(key);
68     }
69
70     @Override
71     public AugmentationNode build() {
72         return new ImmutableAugmentationNode(getNodeIdentifier(), buildValue());
73     }
74
75     private static final class ImmutableAugmentationNode
76             extends AbstractImmutableDataContainerNode<AugmentationIdentifier, AugmentationNode>
77             implements AugmentationNode {
78
79         ImmutableAugmentationNode(final AugmentationIdentifier nodeIdentifier,
80                 final Map<PathArgument, Object> children) {
81             super(children, nodeIdentifier);
82         }
83
84         @Override
85         protected Class<AugmentationNode> implementedType() {
86             return AugmentationNode.class;
87         }
88     }
89 }