6a13ac0d01ca68712a71808304fc195093e17b61
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableContainerNodeBuilder.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.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.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;
22
23 public final class ImmutableContainerNodeBuilder
24         extends AbstractImmutableDataContainerNodeBuilder<NodeIdentifier, ContainerNode>
25         implements ContainerNode.Builder {
26     public ImmutableContainerNodeBuilder() {
27
28     }
29
30     public ImmutableContainerNodeBuilder(final int sizeHint) {
31         super(sizeHint);
32     }
33
34     private ImmutableContainerNodeBuilder(final ImmutableContainerNode node) {
35         super(node.name, node.children);
36     }
37
38     public static ContainerNode.@NonNull Builder create(final ContainerNode node) {
39         if (node instanceof ImmutableContainerNode immutableNode) {
40             return new ImmutableContainerNodeBuilder(immutableNode);
41         }
42         throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
43     }
44
45     @Override
46     public ContainerNode build() {
47         return new ImmutableContainerNode(getNodeIdentifier(), buildValue());
48     }
49
50     protected static final class ImmutableContainerNode extends AbstractContainerNode {
51         private final @NonNull NodeIdentifier name;
52         private final @NonNull Map<NodeIdentifier, Object> children;
53
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);
58         }
59
60         @Override
61         public NodeIdentifier name() {
62             return name;
63         }
64
65         @Override
66         public DataContainerChild childByArg(final NodeIdentifier child) {
67             return LazyLeafOperations.getChild(children, child);
68         }
69
70         @Override
71         public Collection<DataContainerChild> body() {
72             return new LazyValues(children);
73         }
74
75         @Override
76         public int size() {
77             return children.size();
78         }
79
80         @Override
81         protected int valueHashCode() {
82             return children.hashCode();
83         }
84
85         @Override
86         protected boolean valueEquals(final ContainerNode other) {
87             return other instanceof ImmutableContainerNode immutable ? children.equals(immutable.children)
88                 : ImmutableNormalizedNodeMethods.bodyEquals(this, other);
89         }
90     }
91 }