Seal NormalizedNode hierarchy
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableLeafSetEntryNodeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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;
17
18 public final class ImmutableLeafSetEntryNodeBuilder<T>
19         extends AbstractImmutableNormalizedNodeBuilder<NodeWithValue<T>, T, LeafSetEntryNode<T>>
20         implements LeafSetEntryNode.Builder<T> {
21     @Override
22     public LeafSetEntryNode<T> build() {
23         return AbstractImmutableLeafSetEntryNode.of(getNodeIdentifier(), getValue());
24     }
25
26     private abstract static sealed class AbstractImmutableLeafSetEntryNode<T> extends AbstractLeafSetEntryNode<T> {
27         private final @NonNull NodeWithValue<T> name;
28
29         AbstractImmutableLeafSetEntryNode(final NodeWithValue<T> name) {
30             this.name = requireNonNull(name);
31         }
32
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);
40                     return ret;
41                 }
42             } else if (nameValue.equals(body)) {
43                 return new ImmutableLeafSetEntryNode<>(name);
44             }
45
46             throw new IllegalArgumentException(
47                 "Node identifier contains different value: " + name + " than value itself: " + body);
48         }
49
50         @Override
51         public final NodeWithValue<T> name() {
52             return name;
53         }
54
55         @Override
56         protected final T value() {
57             return name.getValue();
58         }
59     }
60
61     private static final class ImmutableLeafSetEntryNode<T> extends AbstractImmutableLeafSetEntryNode<T> {
62         ImmutableLeafSetEntryNode(final NodeWithValue<T> name) {
63             super(name);
64         }
65
66         @Override
67         protected T wrappedValue() {
68             return value();
69         }
70     }
71
72     private static final class ImmutableBinaryLeafSetEntryNode extends AbstractImmutableLeafSetEntryNode<byte[]> {
73         ImmutableBinaryLeafSetEntryNode(final NodeWithValue<byte[]> name) {
74             super(name);
75         }
76
77         @Override
78         protected byte[] wrappedValue() {
79             return value().clone();
80         }
81     }
82 }