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;
22 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.Iterables;
27 public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> {
29 private List<UnkeyedListEntryNode> value;
30 private InstanceIdentifier.NodeIdentifier nodeIdentifier;
31 private boolean dirty = false;
33 protected ImmutableUnkeyedListNodeBuilder() {
34 this.value = new LinkedList<>();
38 protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
39 this.nodeIdentifier = node.getIdentifier();
40 this.value = new LinkedList<>();
41 Iterables.addAll(value, node.getValue());
45 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create() {
46 return new ImmutableUnkeyedListNodeBuilder();
49 public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(final UnkeyedListNode node) {
50 if (!(node instanceof ImmutableUnkeyedListNode)) {
51 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
54 return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
57 private void checkDirty() {
59 value = new LinkedList<>(value);
65 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
67 this.value.add(child);
72 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withoutChild(
73 final InstanceIdentifier.PathArgument key) {
75 throw new UnsupportedOperationException("Children does not have identifiers.");
79 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withValue(final List<UnkeyedListEntryNode> value) {
80 // TODO replace or putAll ?
81 for (final UnkeyedListEntryNode UnkeyedListEntryNode : value) {
82 withChild(UnkeyedListEntryNode);
89 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
90 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
91 this.nodeIdentifier = nodeIdentifier;
96 public UnkeyedListNode build() {
98 return new ImmutableUnkeyedListNode(nodeIdentifier, ImmutableList.copyOf(value));
102 public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> addChild(final UnkeyedListEntryNode child) {
103 return withChild(child);
107 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, UnkeyedListEntryNode, UnkeyedListNode> removeChild(
108 final PathArgument key) {
109 return withoutChild(key);
112 protected static final class ImmutableUnkeyedListNode extends
113 AbstractImmutableNormalizedValueNode<InstanceIdentifier.NodeIdentifier, Iterable<UnkeyedListEntryNode>>
114 implements Immutable, UnkeyedListNode {
116 private final ImmutableList<UnkeyedListEntryNode> children;
118 ImmutableUnkeyedListNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
119 final ImmutableList<UnkeyedListEntryNode> children) {
120 super(nodeIdentifier, children);
121 this.children = children;
125 protected int valueHashCode() {
126 return children.hashCode();
130 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
131 return children.equals(((ImmutableUnkeyedListNode) other).children);
135 public UnkeyedListEntryNode getChild(final int position) {
136 return children.get(position);
140 public int getSize() {
141 return children.size();