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