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 com.google.common.collect.ImmutableList;
11 import com.google.common.collect.Iterables;
12 import java.util.Collection;
13 import java.util.LinkedList;
14 import java.util.List;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
21 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
23 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
25 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
26 private List<UnkeyedListEntryNode> value;
27 private NodeIdentifier nodeIdentifier;
28 private boolean dirty;
30 protected ImmutableUnkeyedListNodeBuilder() {
31 value = new LinkedList<>();
35 protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
36 nodeIdentifier = node.getIdentifier();
37 // FIXME: clean this up, notably reuse unmodified lists
38 value = new LinkedList<>();
39 Iterables.addAll(value, node.body());
43 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create() {
44 return new ImmutableUnkeyedListNodeBuilder();
47 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final int sizeHint) {
48 return new ImmutableUnkeyedListNodeBuilder();
51 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(
52 final UnkeyedListNode node) {
53 if (!(node instanceof ImmutableUnkeyedListNode immutableNode)) {
54 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
56 return new ImmutableUnkeyedListNodeBuilder(immutableNode);
59 private void checkDirty() {
61 value = new LinkedList<>(value);
67 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
74 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
75 final PathArgument key) {
77 throw new UnsupportedOperationException("Children does not have identifiers.");
81 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(
82 final Collection<UnkeyedListEntryNode> withValue) {
83 // TODO replace or putAll ?
84 for (final UnkeyedListEntryNode node : withValue) {
92 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
93 final NodeIdentifier withNodeIdentifier) {
94 nodeIdentifier = withNodeIdentifier;
99 public UnkeyedListNode build() {
101 if (value.isEmpty()) {
102 return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
104 return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
108 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
109 return withChild(child);
113 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode>
114 removeChild(final PathArgument key) {
115 return withoutChild(key);
118 protected static final class EmptyImmutableUnkeyedListNode
119 extends AbstractNormalizedNode<NodeIdentifier, UnkeyedListNode> implements UnkeyedListNode {
120 protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
121 super(nodeIdentifier);
125 public ImmutableList<UnkeyedListEntryNode> body() {
126 return ImmutableList.of();
130 public UnkeyedListEntryNode childAt(final int position) {
131 throw new IndexOutOfBoundsException();
140 protected Class<UnkeyedListNode> implementedType() {
141 return UnkeyedListNode.class;
145 protected int valueHashCode() {
150 protected boolean valueEquals(final UnkeyedListNode other) {
151 return other.isEmpty();
155 protected static final class ImmutableUnkeyedListNode
156 extends AbstractImmutableNormalizedValueNode<NodeIdentifier, UnkeyedListNode,
157 Collection<@NonNull UnkeyedListEntryNode>>
158 implements UnkeyedListNode {
160 private final ImmutableList<UnkeyedListEntryNode> children;
162 ImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier,
163 final ImmutableList<UnkeyedListEntryNode> children) {
164 super(nodeIdentifier, children);
165 this.children = children;
169 public UnkeyedListEntryNode childAt(final int position) {
170 return children.get(position);
175 return children.size();
179 protected Class<UnkeyedListNode> implementedType() {
180 return UnkeyedListNode.class;
184 protected int valueHashCode() {
185 return children.hashCode();
189 protected boolean valueEquals(final UnkeyedListNode other) {
190 final Collection<UnkeyedListEntryNode> otherChildren;
191 if (other instanceof ImmutableUnkeyedListNode immutableOther) {
192 otherChildren = immutableOther.children;
194 otherChildren = other.body();
196 return otherChildren instanceof List ? children.equals(otherChildren)
197 : Iterables.elementsEqual(children, otherChildren);