BUG-457 Fix TODOs and FIXMEs in yang-data-impl schema package.
[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 com.google.common.collect.Sets;
11 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
14 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
16 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
17
18 import com.google.common.base.Preconditions;
19
20 public class ImmutableMapNodeSchemaAwareBuilder extends ImmutableMapNodeBuilder {
21
22     private final ListSchemaNode schema;
23
24     protected ImmutableMapNodeSchemaAwareBuilder(final ListSchemaNode schema) {
25         this.schema = Preconditions.checkNotNull(schema);
26         super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
27     }
28
29     protected ImmutableMapNodeSchemaAwareBuilder(final ListSchemaNode schema, final ImmutableMapNode node) {
30         super(node);
31         this.schema = Preconditions.checkNotNull(schema);
32         super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
33     }
34
35     public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final ListSchemaNode schema) {
36         return new ImmutableMapNodeSchemaAwareBuilder(schema);
37     }
38
39     public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final ListSchemaNode schema, final MapNode node) {
40         if (!(node instanceof ImmutableMapNode)) {
41             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
42         }
43
44         return new ImmutableMapNodeSchemaAwareBuilder(schema, (ImmutableMapNode) node);
45     }
46
47     @Override
48     public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
49         DataValidationException.checkLegalChild(schema.getQName().equals(child.getNodeType()), child.getIdentifier(), schema, Sets.newHashSet(schema.getQName()));
50         return super.withChild(child);
51     }
52
53     @Override
54     public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
55         throw new UnsupportedOperationException("Node identifier created from schema");
56     }
57 }