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.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
26 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
27 private List<UnkeyedListEntryNode> value;
28 private NodeIdentifier nodeIdentifier;
29 private boolean dirty;
31 protected ImmutableUnkeyedListNodeBuilder() {
32 this.value = new LinkedList<>();
36 protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
37 this.nodeIdentifier = node.getIdentifier();
38 this.value = new LinkedList<>();
39 Iterables.addAll(value, node.getValue());
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(final UnkeyedListNode node) {
52 if (!(node instanceof ImmutableUnkeyedListNode)) {
53 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
56 return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
59 private void checkDirty() {
61 value = new LinkedList<>(value);
67 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
69 this.value.add(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(final Collection<UnkeyedListEntryNode> value) {
82 // TODO replace or putAll ?
83 for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
84 withChild(UnkeyedListEntryNode);
91 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
92 final NodeIdentifier nodeIdentifier) {
93 this.nodeIdentifier = nodeIdentifier;
98 public UnkeyedListNode build() {
100 if (value.isEmpty()) {
101 return new EmptyImmutableUnkeyedListNode(nodeIdentifier);
103 return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
107 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
108 return withChild(child);
112 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode> removeChild(
113 final PathArgument key) {
114 return withoutChild(key);
117 protected static final class EmptyImmutableUnkeyedListNode extends
118 AbstractImmutableNormalizedNode<NodeIdentifier, Collection<UnkeyedListEntryNode>> implements Immutable,
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<NodeIdentifier, Collection<UnkeyedListEntryNode>>
152 implements Immutable, UnkeyedListNode {
154 private final ImmutableList<UnkeyedListEntryNode> children;
156 ImmutableUnkeyedListNode(final 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();