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.Collection;
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.impl.schema.nodes.LazyLeafOperations;
21 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.LazyValues;
23 public final class ImmutableChoiceNodeBuilder
24 extends AbstractImmutableDataContainerNodeBuilder<NodeIdentifier, ChoiceNode>
25 implements ChoiceNode.Builder {
26 public ImmutableChoiceNodeBuilder() {
30 public ImmutableChoiceNodeBuilder(final int sizeHint) {
34 private ImmutableChoiceNodeBuilder(final ImmutableChoiceNode node) {
35 super(node.name, node.children);
38 public static ChoiceNode.@NonNull Builder create(final ChoiceNode node) {
39 if (node instanceof ImmutableChoiceNode immutableNode) {
40 return new ImmutableChoiceNodeBuilder(immutableNode);
42 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
46 public ChoiceNode build() {
47 return new ImmutableChoiceNode(getNodeIdentifier(), buildValue());
50 private static final class ImmutableChoiceNode extends AbstractChoiceNode {
51 private final @NonNull NodeIdentifier name;
52 private final @NonNull Map<NodeIdentifier, Object> children;
54 ImmutableChoiceNode(final NodeIdentifier name, final Map<NodeIdentifier, Object> children) {
55 this.name = requireNonNull(name);
56 // FIXME: move this to caller
57 this.children = ImmutableOffsetMap.unorderedCopyOf(children);
61 public NodeIdentifier name() {
66 public DataContainerChild childByArg(final NodeIdentifier child) {
67 return LazyLeafOperations.getChild(children, child);
71 public Collection<DataContainerChild> body() {
72 return new LazyValues(children);
77 return children.size();
81 protected int valueHashCode() {
82 return children.hashCode();
86 protected boolean valueEquals(final ChoiceNode other) {
87 return other instanceof ImmutableChoiceNode immutable ? children.equals(immutable.children)
88 : ImmutableNormalizedNodeMethods.bodyEquals(this, other);