d747a4a8952d2fcd7adb0731d352d93538386885
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableMapNodeSchemaAwareBuilder.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.Collections;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20
21 public class ImmutableMapNodeSchemaAwareBuilder extends ImmutableMapNodeBuilder {
22     private final ListSchemaNode schema;
23
24     protected ImmutableMapNodeSchemaAwareBuilder(final ListSchemaNode schema) {
25         this.schema = requireNonNull(schema);
26         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
27     }
28
29     protected ImmutableMapNodeSchemaAwareBuilder(final ListSchemaNode schema, final ImmutableMapNode node) {
30         super(node);
31         this.schema = requireNonNull(schema);
32         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
33     }
34
35     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create(final ListSchemaNode schema) {
36         return new ImmutableMapNodeSchemaAwareBuilder(schema);
37     }
38
39     public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create(final ListSchemaNode schema,
40             final MapNode node) {
41         if (!(node instanceof ImmutableMapNode)) {
42             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
43         }
44
45         return new ImmutableMapNodeSchemaAwareBuilder(schema, (ImmutableMapNode) node);
46     }
47
48     @Override
49     public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
50         DataValidationException.checkLegalChild(schema.getQName().equals(child.getNodeType()), child.getIdentifier(),
51             schema, Collections.singleton(schema.getQName()));
52         return super.withChild(child);
53     }
54
55     @Override
56     public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
57         throw new UnsupportedOperationException("Node identifier created from schema");
58     }
59 }