6e4a52a07bd7f661e99bf2db8de82fb127ea110a
[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.ContainerLike;
17
18 public final class ImmutableContainerNodeSchemaAwareBuilder extends ImmutableContainerNodeBuilder {
19     private final DataNodeContainerValidator validator;
20
21     private ImmutableContainerNodeSchemaAwareBuilder(final ContainerLike schema) {
22         this.validator = new DataNodeContainerValidator(schema);
23         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
24     }
25
26     private ImmutableContainerNodeSchemaAwareBuilder(final ContainerLike schema, final ImmutableContainerNode node) {
27         super(node);
28         this.validator = new DataNodeContainerValidator(schema);
29         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
30     }
31
32     public static @NonNull DataContainerNodeBuilder<NodeIdentifier, ContainerNode> create(final ContainerLike schema) {
33         return new ImmutableContainerNodeSchemaAwareBuilder(schema);
34     }
35
36     public static @NonNull DataContainerNodeBuilder<NodeIdentifier, ContainerNode> create(final ContainerLike schema,
37             final ContainerNode node) {
38         if (!(node instanceof ImmutableContainerNode)) {
39             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
40         }
41         return new ImmutableContainerNodeSchemaAwareBuilder(schema, (ImmutableContainerNode)node);
42     }
43
44     @Override
45     public DataContainerNodeBuilder<NodeIdentifier, ContainerNode> withNodeIdentifier(
46             final NodeIdentifier withNodeIdentifier) {
47         throw new UnsupportedOperationException("Node identifier created from schema");
48     }
49
50     @Override
51     public DataContainerNodeBuilder<NodeIdentifier, ContainerNode> withChild(final DataContainerChild<?, ?> child) {
52         validator.validateChild(child.getIdentifier());
53         return super.withChild(child);
54     }
55
56     @Override
57     public ContainerNode build() {
58         // TODO check when statements... somewhere
59         return super.build();
60     }
61 }