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