Scripted update of if statements
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableChoiceNodeSchemaAwareBuilder.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.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
15 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataNodeContainerValidator;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21
22 public class ImmutableChoiceNodeSchemaAwareBuilder extends ImmutableChoiceNodeBuilder {
23
24     private final ChoiceSchemaNode schema;
25     private DataNodeContainerValidator validator;
26
27     protected ImmutableChoiceNodeSchemaAwareBuilder(final ChoiceSchemaNode schema) {
28         this.schema = Preconditions.checkNotNull(schema, "Schema was null");
29         super.withNodeIdentifier(NodeIdentifier.create(schema.getQName()));
30     }
31
32     @Override
33     public DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> withNodeIdentifier(final NodeIdentifier nodeIdentifier) {
34         throw new UnsupportedOperationException("Node identifier created from schema");
35     }
36
37     @Override
38     public DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> withChild(final DataContainerChild<?, ?> child) {
39         if (validator == null) {
40             Optional<ChoiceCaseNode> detectedCaseOpt = SchemaUtils.detectCase(schema, child);
41             DataValidationException.checkLegalChild(detectedCaseOpt.isPresent(), child.getIdentifier(), schema);
42             validator = new DataNodeContainerValidator(detectedCaseOpt.get());
43         }
44
45         return super.withChild(validator.validateChild(child));
46     }
47
48     @Override
49     public ChoiceNode build() {
50         // TODO validate when statement
51         return super.build();
52     }
53
54     public static DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> create(final ChoiceSchemaNode schema) {
55         return new ImmutableChoiceNodeSchemaAwareBuilder(schema);
56     }
57 }