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.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 UnkeyedListNode node) {
49 if (!(node instanceof ImmutableUnkeyedListNode)) {
50 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
53 return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
56 private void checkDirty() {
58 value = new LinkedList<>(value);
64 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
66 this.value.add(child);
71 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
72 final InstanceIdentifier.PathArgument key) {
74 throw new UnsupportedOperationException("Children does not have identifiers.");
78 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final List<UnkeyedListEntryNode> value) {
79 // TODO replace or putAll ?
80 for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
81 withChild(UnkeyedListEntryNode);
88 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
89 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
90 this.nodeIdentifier = nodeIdentifier;
95 public UnkeyedListNode build() {
97 return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
101 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
102 return withChild(child);
106 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode> removeChild(
107 final PathArgument key) {
108 return withoutChild(key);
111 protected static final class ImmutableUnkeyedListNode extends
112 AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>>
113 implements Immutable, UnkeyedListNode {
115 private final ImmutableList<UnkeyedListEntryNode> children;
117 ImmutableUnkeyedListNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
118 final ImmutableList<UnkeyedListEntryNode> children) {
119 super(nodeIdentifier, children);
120 this.children = children;
124 protected int valueHashCode() {
125 return children.hashCode();
129 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
130 return children.equals(((ImmutableUnkeyedListNode) other).children);
134 public UnkeyedListEntryNode getChild(final int position) {
135 return children.get(position);
139 public int getSize() {
140 return children.size();