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