2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
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;
20 public class ImmutableAugmentationNodeBuilder
21 extends AbstractImmutableDataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> {
23 protected ImmutableAugmentationNodeBuilder() {
26 protected ImmutableAugmentationNodeBuilder(final int sizeHint) {
30 ImmutableAugmentationNodeBuilder(final ImmutableAugmentationNode node) {
34 public static @NonNull DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> create() {
35 return new ImmutableAugmentationNodeBuilder();
38 public static @NonNull DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> create(
40 return new ImmutableAugmentationNodeBuilder(sizeHint);
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());
48 return new ImmutableAugmentationNodeBuilder(immutableNode);
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));
62 return super.withChild(child);
66 public DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> withoutChild(final PathArgument key) {
67 return super.withoutChild(key);
71 public AugmentationNode build() {
72 return new ImmutableAugmentationNode(getNodeIdentifier(), buildValue());
75 private static final class ImmutableAugmentationNode
76 extends AbstractImmutableDataContainerNode<AugmentationIdentifier, AugmentationNode>
77 implements AugmentationNode {
79 ImmutableAugmentationNode(final AugmentationIdentifier nodeIdentifier,
80 final Map<PathArgument, Object> children) {
81 super(children, nodeIdentifier);
85 protected Class<AugmentationNode> implementedType() {
86 return AugmentationNode.class;