Add schema-aware builders
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / SchemaAwareImmutableMapNodeBuilder.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 static java.util.Objects.requireNonNull;
11
12 import java.util.Set;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
18 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
19
20 final class SchemaAwareImmutableMapNodeBuilder extends ImmutableMapNodeBuilder {
21     private final ListSchemaNode schema;
22
23     SchemaAwareImmutableMapNodeBuilder(final ListSchemaNode schema) {
24         this.schema = requireNonNull(schema);
25         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
26     }
27
28     SchemaAwareImmutableMapNodeBuilder(final ListSchemaNode schema, final ImmutableMapNode node) {
29         super(node);
30         this.schema = requireNonNull(schema);
31         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
32     }
33
34     @Override
35     public ImmutableMapNodeBuilder withChild(final MapEntryNode child) {
36         final NodeIdentifierWithPredicates childId = child.getIdentifier();
37         final QName qname = schema.getQName();
38
39         DataValidationException.checkLegalChild(qname.equals(childId.getNodeType()), childId, schema, Set.of(qname));
40         return super.withChild(child);
41     }
42
43     @Override
44     public ImmutableMapNodeBuilder withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
45         throw new UnsupportedOperationException("Node identifier created from schema");
46     }
47 }