Promote ImmutableNodes to yang-data-spi
[yangtools.git] / data / yang-data-spi / src / main / java / org / opendaylight / yangtools / yang / data / spi / node / impl / ImmutableChoiceNode.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.spi.node.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Collection;
13 import java.util.Map;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.AbstractChoiceNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
20 import org.opendaylight.yangtools.yang.data.spi.node.LazyLeafOperations;
21 import org.opendaylight.yangtools.yang.data.spi.node.LazyValues;
22
23 final class ImmutableChoiceNode extends AbstractChoiceNode {
24     private final @NonNull NodeIdentifier name;
25     final @NonNull Map<NodeIdentifier, Object> children;
26
27     ImmutableChoiceNode(final NodeIdentifier name, final Map<NodeIdentifier, Object> children) {
28         this.name = requireNonNull(name);
29         // FIXME: move this to caller
30         this.children = ImmutableOffsetMap.unorderedCopyOf(children);
31     }
32
33     @Override
34     public NodeIdentifier name() {
35         return name;
36     }
37
38     @Override
39     public DataContainerChild childByArg(final NodeIdentifier child) {
40         return LazyLeafOperations.getChild(children, child);
41     }
42
43     @Override
44     public Collection<DataContainerChild> body() {
45         return new LazyValues(children);
46     }
47
48     @Override
49     public int size() {
50         return children.size();
51     }
52
53     @Override
54     protected int valueHashCode() {
55         return children.hashCode();
56     }
57
58     @Override
59     protected boolean valueEquals(final ChoiceNode other) {
60         return other instanceof ImmutableChoiceNode immutable ? children.equals(immutable.children)
61             : ImmutableNormalizedNodeMethods.bodyEquals(this, other);
62     }
63 }