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