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