ca8a6a2f61cf6931e7d956b13a5d4dfdda0a5415
[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.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
11 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
12 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
13 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
14 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16
17 public final class ImmutableContainerNodeSchemaAwareBuilder extends ImmutableContainerNodeBuilder {
18
19     private final DataNodeContainerValidator validator;
20     // TODO remove schema aware builders, replace by validator called at build() ?
21
22     private ImmutableContainerNodeSchemaAwareBuilder(final ContainerSchemaNode schema) {
23         this.validator = new DataNodeContainerValidator(schema);
24         super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
25     }
26
27     private ImmutableContainerNodeSchemaAwareBuilder(final ContainerSchemaNode schema, final ImmutableContainerNode node) {
28         super(node);
29         this.validator = new DataNodeContainerValidator(schema);
30         super.withNodeIdentifier(new InstanceIdentifier.NodeIdentifier(schema.getQName()));
31     }
32
33     public static DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> create(final ContainerSchemaNode schema) {
34         return new ImmutableContainerNodeSchemaAwareBuilder(schema);
35     }
36
37     public static DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> create(final ContainerSchemaNode schema, 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 DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> withNodeIdentifier(final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
46         throw new UnsupportedOperationException("Node identifier created from schema");
47     }
48
49     @Override
50     public DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifier, ContainerNode> withChild(final DataContainerChild<?, ?> child) {
51         validator.validateChild(child.getIdentifier());
52         return super.withChild(child);
53     }
54
55     @Override
56     public ContainerNode build() {
57         // TODO check when statements... somewhere
58         return super.build();
59     }
60 }