2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
22 public final class ImmutableOrderedLeafSetNodeSchemaAwareBuilder<T> extends ImmutableOrderedLeafSetNodeBuilder<T> {
24 private final LeafListSchemaNode schema;
26 private ImmutableOrderedLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema) {
27 this.schema = requireNonNull(schema);
28 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
31 public ImmutableOrderedLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema,
32 final ImmutableOrderedLeafSetNode<T> node) {
34 this.schema = requireNonNull(schema);
35 // FIXME: Preconditions.checkArgument(schema.getQName().equals(node.getIdentifier()));
36 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
39 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema) {
40 return new ImmutableOrderedLeafSetNodeSchemaAwareBuilder<>(schema);
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()));
49 return new ImmutableOrderedLeafSetNodeSchemaAwareBuilder<>(schema, (ImmutableOrderedLeafSetNode<T>) node);
53 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
54 // TODO check value type
55 return super.withChildValue(childValue);
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);
69 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
70 throw new UnsupportedOperationException("Node identifier created from schema");