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 com.google.common.base.Preconditions;
11 import java.util.Collections;
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;
19 public final class ImmutableLeafSetNodeSchemaAwareBuilder<T> extends ImmutableLeafSetNodeBuilder<T> {
21 private final LeafListSchemaNode schema;
23 private ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema) {
24 this.schema = Preconditions.checkNotNull(schema);
25 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
28 public ImmutableLeafSetNodeSchemaAwareBuilder(final LeafListSchemaNode schema, final ImmutableLeafSetNode<T> node) {
30 this.schema = Preconditions.checkNotNull(schema);
31 // FIXME: Preconditions.checkArgument(schema.getQName().equals(node.getIdentifier()));
32 super.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
35 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema) {
36 return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema);
39 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafListSchemaNode schema,
40 final LeafSetNode<T> node) {
41 if (!(node instanceof ImmutableLeafSetNode<?>)) {
42 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
45 return new ImmutableLeafSetNodeSchemaAwareBuilder<>(schema, (ImmutableLeafSetNode<T>) node);
49 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
50 // TODO check value type
51 return super.withChildValue(value);
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(),
60 schema, Collections.singleton(schema.getQName()));
61 return super.withChild(child);
65 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier nodeIdentifier) {
66 throw new UnsupportedOperationException("Node identifier created from schema");