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.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
21 public final class ImmutableLeafSetNodeSchemaAwareBuilder<T> extends ImmutableLeafSetNodeBuilder<T> {
23 private final LeafListSchemaNode schema;
25 private ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema) {
26 this.schema = requireNonNull(schema);
27 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
30 public ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema, final ImmutableLeafSetNode<T> node) {
32 this.schema = requireNonNull(schema);
33 // FIXME: Preconditions.checkArgument(schema.getQName().equals(node.getIdentifier()));
34 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
37 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema) {
38 return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema);
41 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema,
42 final LeafSetNode<T> node) {
43 if (!(node instanceof ImmutableLeafSetNode<?>)) {
44 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
47 return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema, (ImmutableLeafSetNode<T>) node);
51 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
52 // TODO check value type
53 return super.withChildValue(childValue);
57 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
58 checkArgument(schema.getQName().equals(child.getNodeType()),
59 "Incompatible node type, should be: %s, is: %s", schema.getQName(), child.getNodeType());
60 // TODO check value type using TypeProvider ?
61 DataValidationException.checkLegalChild(schema.getQName().equals(child.getNodeType()), child.getIdentifier(),
62 schema, Collections.singleton(schema.getQName()));
63 return super.withChild(child);
67 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
68 throw new UnsupportedOperationException("Node identifier created from schema");