Improve ImmutableAugmentationNodeBuilder defensiveness
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetNodeSchemaAwareBuilder.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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collections;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
20 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
21
22 public final class ImmutableLeafSetNodeSchemaAwareBuilder<T> extends ImmutableLeafSetNodeBuilder<T> {
23     private final LeafListSchemaNode schema;
24
25     private ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema) {
26         this.schema = requireNonNull(schema);
27         super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
28     }
29
30     public ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema, final ImmutableLeafSetNode<T> node) {
31         super(node);
32         this.schema = requireNonNull(schema);
33         // FIXME: Preconditions.checkArgument(schema.getQName().equals(node.getIdentifier()));
34         super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
35     }
36
37     public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final LeafListSchemaNode schema) {
38         return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema);
39     }
40
41     public static <T> @NonNull ListNodeBuilder<T, SystemLeafSetNode<T>> create(final LeafListSchemaNode schema,
42             final SystemLeafSetNode<T> node) {
43         if (!(node instanceof ImmutableLeafSetNode<?>)) {
44             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
45         }
46
47         return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema, (ImmutableLeafSetNode<T>) node);
48     }
49
50     @Override
51     public ImmutableLeafSetNodeBuilder<T> withChildValue(final T childValue) {
52         // TODO check value type
53         return super.withChildValue(childValue);
54     }
55
56     @Override
57     public ImmutableLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
58         checkArgument(schema.getQName().equals(child.getNodeType()),
59                 "Incompatible node type, should be: %s, is: %s", schema.getQName(), child.getNodeType());
60         // TODO check value type using TypeProvider ?
61         DataValidationException.checkLegalChild(schema.getQName().equals(child.getNodeType()), child.getIdentifier(),
62             schema, Collections.singleton(schema.getQName()));
63         return super.withChild(child);
64     }
65
66     @Override
67     public ImmutableLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
68         throw new UnsupportedOperationException("Node identifier created from schema");
69     }
70 }