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