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.AbstractContainerNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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 ImmutableContainerNodeBuilder
24 extends AbstractImmutableDataContainerNodeBuilder<NodeIdentifier, ContainerNode>
25 implements ContainerNode.Builder {
26 public ImmutableContainerNodeBuilder() {
30 public ImmutableContainerNodeBuilder(final int sizeHint) {
34 private ImmutableContainerNodeBuilder(final ImmutableContainerNode node) {
35 super(node.name, node.children);
38 public static ContainerNode.@NonNull Builder create(final ContainerNode node) {
39 if (node instanceof ImmutableContainerNode immutableNode) {
40 return new ImmutableContainerNodeBuilder(immutableNode);
42 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
46 public ContainerNode build() {
47 return new ImmutableContainerNode(getNodeIdentifier(), buildValue());
50 protected static final class ImmutableContainerNode extends AbstractContainerNode {
51 private final @NonNull NodeIdentifier name;
52 private final @NonNull Map<NodeIdentifier, Object> children;
54 ImmutableContainerNode(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 ContainerNode other) {
87 return other instanceof ImmutableContainerNode immutable ? children.equals(immutable.children)
88 : ImmutableNormalizedNodeMethods.bodyEquals(this, other);