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 java.util.LinkedList;
11 import java.util.List;
13 import org.opendaylight.yangtools.concepts.Immutable;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
21 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.Iterables;
26 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
28 private List<UnkeyedListEntryNode> value;
29 private InstanceIdentifier.NodeIdentifier nodeIdentifier;
30 private boolean dirty = false;
32 protected ImmutableUnkeyedListNodeBuilder() {
33 this.value = new LinkedList<>();
37 protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
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 UnkeyedListNode node) {
48 if (!(node instanceof ImmutableUnkeyedListNode)) {
49 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
52 return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
55 private void checkDirty() {
57 value = new LinkedList<>(value);
63 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
65 this.value.add(child);
70 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
71 final InstanceIdentifier.PathArgument key) {
73 throw new UnsupportedOperationException("Children does not have identifiers.");
77 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final List<UnkeyedListEntryNode> value) {
78 // TODO replace or putAll ?
79 for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
80 withChild(UnkeyedListEntryNode);
87 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
88 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
89 this.nodeIdentifier = nodeIdentifier;
94 public UnkeyedListNode build() {
96 return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
100 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
101 return withChild(child);
105 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode> removeChild(
106 final PathArgument key) {
107 return withoutChild(key);
110 protected static final class ImmutableUnkeyedListNode extends
111 AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>>
112 implements Immutable, UnkeyedListNode {
114 private final ImmutableList<UnkeyedListEntryNode> children;
116 ImmutableUnkeyedListNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
117 final ImmutableList<UnkeyedListEntryNode> children) {
118 super(nodeIdentifier, children);
119 this.children = children;
123 protected int valueHashCode() {
124 return children.hashCode();
128 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
129 return children.equals(((ImmutableUnkeyedListNode) other).children);
133 public UnkeyedListEntryNode getChild(final int position) {
134 return children.get(position);
138 public int getSize() {
139 return children.size();