2 * Copyright (c) 2015 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;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Collection;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.SystemLeafSetNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
20 import org.opendaylight.yangtools.yang.data.util.LeafsetEntryInterner;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
24 final class InterningLeafSetNodeBuilder<T> implements SystemLeafSetNode.Builder<T> {
25 private final SystemLeafSetNode.Builder<T> delegate;
26 private final LeafsetEntryInterner interner;
28 private InterningLeafSetNodeBuilder(final SystemLeafSetNode.Builder<T> delegate,
29 final LeafsetEntryInterner interner) {
30 this.delegate = requireNonNull(delegate);
31 this.interner = requireNonNull(interner);
34 private static @Nullable LeafsetEntryInterner getInterner(final @Nullable DataSchemaNode schema) {
35 return schema instanceof LeafListSchemaNode leafListSchema ? LeafsetEntryInterner.forSchema(leafListSchema)
39 static <T> ListNodeBuilder<T, SystemLeafSetNode<T>> create(final @Nullable DataSchemaNode schema) {
40 final var delegate = Builders.<T>leafSetBuilder();
41 final var interner = getInterner(schema);
42 return interner == null ? delegate : new InterningLeafSetNodeBuilder<>(delegate, interner);
45 static <T> ListNodeBuilder<T, SystemLeafSetNode<T>> create(final @Nullable DataSchemaNode schema,
47 final var delegate = Builders.<T>leafSetBuilder(sizeHint);
48 final var interner = getInterner(schema);
49 return interner == null ? delegate : new InterningLeafSetNodeBuilder<>(delegate, interner);
53 public ListNodeBuilder<T, SystemLeafSetNode<T>> withNodeIdentifier(final NodeIdentifier nodeIdentifier) {
54 return delegate.withNodeIdentifier(nodeIdentifier);
58 public ListNodeBuilder<T, SystemLeafSetNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
59 // FIXME: pass through interner
60 return delegate.withValue(value);
64 public ListNodeBuilder<T, SystemLeafSetNode<T>> withChild(final LeafSetEntryNode<T> child) {
65 return delegate.withChild(interner.intern(child));
69 public ListNodeBuilder<T, SystemLeafSetNode<T>> withoutChild(final PathArgument key) {
70 return delegate.withoutChild(key);
74 public ListNodeBuilder<T, SystemLeafSetNode<T>> withChildValue(final T child) {
75 // TODO Auto-generated method stub
76 throw new UnsupportedOperationException();
80 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, SystemLeafSetNode<T>>
81 addChild(final LeafSetEntryNode<T> child) {
82 return withChild(child);
86 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, SystemLeafSetNode<T>>
87 removeChild(final PathArgument key) {
88 return withoutChild(key);
92 public SystemLeafSetNode<T> build() {
93 return delegate.build();