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 ImmutableLeafSetNodeSchemaAwareBuilder<T> extends ImmutableLeafSetNodeBuilder<T> {
24 private final LeafListSchemaNode schema;
26 private ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema) {
27 this.schema = requireNonNull(schema);
28 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
31 public ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema, final ImmutableLeafSetNode<T> node) {
33 this.schema = requireNonNull(schema);
34 // FIXME: Preconditions.checkArgument(schema.getQName().equals(node.getIdentifier()));
35 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
38 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema) {
39 return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema);
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()));
48 return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema, (ImmutableLeafSetNode<T>) node);
52 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
53 // TODO check value type
54 return super.withChildValue(childValue);
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);
68 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
69 throw new UnsupportedOperationException("Node identifier created from schema");