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 this.value = new LinkedList<>();
35 protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
36 this.nodeIdentifier = node.getIdentifier();
37 // FIXME: clean this up, notably reuse unmodified lists
38 this.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)) {
54 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
57 return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
60 private void checkDirty() {
62 value = new LinkedList<>(value);
68 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
70 this.value.add(child);
75 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
76 final PathArgument key) {
78 throw new UnsupportedOperationException("Children does not have identifiers.");
82 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(
83 final Collection<UnkeyedListEntryNode> withValue) {
84 // TODO replace or putAll ?
85 for (final UnkeyedListEntryNode node : withValue) {
93 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
94 final NodeIdentifier withNodeIdentifier) {
95 this.nodeIdentifier = withNodeIdentifier;
100 public UnkeyedListNode build() {
102 if (value.isEmpty()) {
103 return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
105 return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
109 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
110 return withChild(child);
114 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode>
115 removeChild(final PathArgument key) {
116 return withoutChild(key);
119 protected static final class EmptyImmutableUnkeyedListNode
120 extends AbstractNormalizedNode<NodeIdentifier, UnkeyedListNode> implements UnkeyedListNode {
121 protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
122 super(nodeIdentifier);
126 public ImmutableList<UnkeyedListEntryNode> body() {
127 return ImmutableList.of();
131 public UnkeyedListEntryNode childAt(final int position) {
132 throw new IndexOutOfBoundsException();
141 protected Class<UnkeyedListNode> implementedType() {
142 return UnkeyedListNode.class;
146 protected int valueHashCode() {
151 protected boolean valueEquals(final UnkeyedListNode other) {
152 return other.isEmpty();
156 protected static final class ImmutableUnkeyedListNode
157 extends AbstractImmutableNormalizedValueNode<NodeIdentifier, UnkeyedListNode,
158 Collection<@NonNull UnkeyedListEntryNode>>
159 implements UnkeyedListNode {
161 private final ImmutableList<UnkeyedListEntryNode> children;
163 ImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier,
164 final ImmutableList<UnkeyedListEntryNode> children) {
165 super(nodeIdentifier, children);
166 this.children = children;
170 public UnkeyedListEntryNode childAt(final int position) {
171 return children.get(position);
176 return children.size();
180 protected Class<UnkeyedListNode> implementedType() {
181 return UnkeyedListNode.class;
185 protected int valueHashCode() {
186 return children.hashCode();
190 protected boolean valueEquals(final UnkeyedListNode other) {
191 final Collection<UnkeyedListEntryNode> otherChildren;
192 if (other instanceof ImmutableUnkeyedListNode) {
193 otherChildren = ((ImmutableUnkeyedListNode) other).children;
195 otherChildren = other.body();
197 return otherChildren instanceof List ? children.equals(otherChildren)
198 : Iterables.elementsEqual(children, otherChildren);