Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableContainerNodeSchemaAwareBuilder.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.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
14 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17
18 public final class ImmutableContainerNodeSchemaAwareBuilder extends ImmutableContainerNodeBuilder {
19     private final DataNodeContainerValidator validator;
20
21     private ImmutableContainerNodeSchemaAwareBuilder(final ContainerSchemaNode schema) {
22         this.validator = new DataNodeContainerValidator(schema);
23         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
24     }
25
26     private ImmutableContainerNodeSchemaAwareBuilder(final ContainerSchemaNode schema,
27             final ImmutableContainerNode node) {
28         super(node);
29         this.validator = new DataNodeContainerValidator(schema);
30         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
31     }
32
33     public static @NonNull DataContainerNodeBuilder<NodeIdentifier, ContainerNode> create(
34             final ContainerSchemaNode schema) {
35         return new ImmutableContainerNodeSchemaAwareBuilder(schema);
36     }
37
38     public static @NonNull DataContainerNodeBuilder<NodeIdentifier, ContainerNode> create(
39             final ContainerSchemaNode schema, final ContainerNode node) {
40         if (!(node instanceof ImmutableContainerNode)) {
41             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
42         }
43         return new ImmutableContainerNodeSchemaAwareBuilder(schema, (ImmutableContainerNode)node);
44     }
45
46     @Override
47     public DataContainerNodeBuilder<NodeIdentifier, ContainerNode> withNodeIdentifier(
48             final NodeIdentifier withNodeIdentifier) {
49         throw new UnsupportedOperationException("Node identifier created from schema");
50     }
51
52     @Override
53     public DataContainerNodeBuilder<NodeIdentifier, ContainerNode> withChild(final DataContainerChild<?, ?> child) {
54         validator.validateChild(child.getIdentifier());
55         return super.withChild(child);
56     }
57
58     @Override
59     public ContainerNode build() {
60         // TODO check when statements... somewhere
61         return super.build();
62     }
63 }