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 java.util.Objects.requireNonNull;
12 import java.util.Arrays;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
15 import org.opendaylight.yangtools.yang.data.api.schema.AbstractLeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
18 public final class ImmutableLeafSetEntryNodeBuilder<T>
19 extends AbstractImmutableNormalizedNodeBuilder<NodeWithValue<T>, T, LeafSetEntryNode<T>>
20 implements LeafSetEntryNode.Builder<T> {
22 public LeafSetEntryNode<T> build() {
23 return AbstractImmutableLeafSetEntryNode.of(getNodeIdentifier(), getValue());
26 private abstract static sealed class AbstractImmutableLeafSetEntryNode<T> extends AbstractLeafSetEntryNode<T> {
27 private final @NonNull NodeWithValue<T> name;
29 AbstractImmutableLeafSetEntryNode(final NodeWithValue<T> name) {
30 this.name = requireNonNull(name);
33 static <T> @NonNull AbstractImmutableLeafSetEntryNode<T> of(final NodeWithValue<T> name, final T body) {
34 final var nameValue = name.getValue();
35 if (body instanceof byte[] bodyBytes) {
36 if (nameValue instanceof byte[] nameBytes && Arrays.equals(nameBytes, bodyBytes)) {
37 @SuppressWarnings("unchecked")
38 final var ret = (AbstractImmutableLeafSetEntryNode<T>)
39 new ImmutableBinaryLeafSetEntryNode((NodeWithValue<byte[]>) name);
42 } else if (nameValue.equals(body)) {
43 return new ImmutableLeafSetEntryNode<>(name);
46 throw new IllegalArgumentException(
47 "Node identifier contains different value: " + name + " than value itself: " + body);
51 public final NodeWithValue<T> name() {
56 protected final T value() {
57 return name.getValue();
61 private static final class ImmutableLeafSetEntryNode<T> extends AbstractImmutableLeafSetEntryNode<T> {
62 ImmutableLeafSetEntryNode(final NodeWithValue<T> name) {
67 protected T wrappedValue() {
72 private static final class ImmutableBinaryLeafSetEntryNode extends AbstractImmutableLeafSetEntryNode<byte[]> {
73 ImmutableBinaryLeafSetEntryNode(final NodeWithValue<byte[]> name) {
78 protected byte[] wrappedValue() {
79 return value().clone();