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.Collections;
14 import java.util.LinkedList;
15 import java.util.List;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
27 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
28 private List<UnkeyedListEntryNode> value;
29 private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
30 private boolean dirty;
32 protected ImmutableUnkeyedListNodeBuilder() {
33 this.value = new LinkedList<>();
37 protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
38 this.nodeIdentifier = node.getIdentifier();
39 this.value = new LinkedList<>();
40 Iterables.addAll(value, node.getValue());
44 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create() {
45 return new ImmutableUnkeyedListNodeBuilder();
48 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final int sizeHint) {
49 return new ImmutableUnkeyedListNodeBuilder();
52 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(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 YangInstanceIdentifier.PathArgument key) {
78 throw new UnsupportedOperationException("Children does not have identifiers.");
82 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final Collection<UnkeyedListEntryNode> value) {
83 // TODO replace or putAll ?
84 for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
85 withChild(UnkeyedListEntryNode);
92 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
93 final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
94 this.nodeIdentifier = nodeIdentifier;
99 public UnkeyedListNode build() {
101 if (value.isEmpty()) {
102 return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
104 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> removeChild(
115 final PathArgument key) {
116 return withoutChild(key);
119 protected static final class EmptyImmutableUnkeyedListNode extends AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Collection<UnkeyedListEntryNode>> implements Immutable, UnkeyedListNode {
120 protected EmptyImmutableUnkeyedListNode(final NodeIdentifier nodeIdentifier) {
121 super(nodeIdentifier);
125 public Collection<UnkeyedListEntryNode> getValue() {
126 return Collections.emptySet();
130 public UnkeyedListEntryNode getChild(final int position) {
131 throw new IndexOutOfBoundsException();
135 public int getSize() {
140 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
141 return Collections.EMPTY_LIST.equals(other.getValue());
145 protected int valueHashCode() {
146 return Collections.EMPTY_LIST.hashCode();
150 protected static final class ImmutableUnkeyedListNode extends
151 AbstractImmutableNormalizedValueNode<YangInstanceIdentifier.NodeIdentifier, Collection<UnkeyedListEntryNode>>
152 implements Immutable, UnkeyedListNode {
154 private final ImmutableList<UnkeyedListEntryNode> children;
156 ImmutableUnkeyedListNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
157 final ImmutableList<UnkeyedListEntryNode> children) {
158 super(nodeIdentifier, children);
159 this.children = children;
163 protected int valueHashCode() {
164 return children.hashCode();
168 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
169 return children.equals(((ImmutableUnkeyedListNode) other).children);
173 public UnkeyedListEntryNode getChild(final int position) {
174 return children.get(position);
178 public int getSize() {
179 return children.size();