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.Iterables;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.LinkedHashMap;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.util.UnmodifiableCollection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
27 public class ImmutableUserLeafSetNodeBuilder<T> implements ListNodeBuilder<T, UserLeafSetNode<T>> {
28 private Map<NodeWithValue, LeafSetEntryNode<T>> value;
29 private NodeIdentifier nodeIdentifier;
30 private boolean dirty;
32 ImmutableUserLeafSetNodeBuilder() {
33 value = new LinkedHashMap<>();
37 ImmutableUserLeafSetNodeBuilder(final ImmutableUserLeafSetNode<T> node) {
38 nodeIdentifier = node.getIdentifier();
39 value = node.getChildren();
43 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create() {
44 return new ImmutableUserLeafSetNodeBuilder<>();
47 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(
48 final UserLeafSetNode<T> node) {
49 if (!(node instanceof ImmutableUserLeafSetNode<?>)) {
50 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
53 return new ImmutableUserLeafSetNodeBuilder<>((ImmutableUserLeafSetNode<T>) node);
56 @Deprecated(since = "6.0.7", forRemoval = true)
57 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(final LeafListSchemaNode schema) {
58 return new SchemaAwareImmutableOrderedLeafSetNodeBuilder<>(schema);
61 @Deprecated(since = "6.0.7", forRemoval = true)
62 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(final LeafListSchemaNode schema,
63 final LeafSetNode<T> node) {
64 if (node instanceof ImmutableUserLeafSetNode<?>) {
65 return new SchemaAwareImmutableOrderedLeafSetNodeBuilder<>(schema, (ImmutableUserLeafSetNode<T>) node);
67 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
70 private void checkDirty() {
72 value = new LinkedHashMap<>(value);
78 public ImmutableUserLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
80 this.value.put(child.getIdentifier(), child);
85 public ImmutableUserLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
87 this.value.remove(key);
92 public UserLeafSetNode<T> build() {
94 return new ImmutableUserLeafSetNode<>(nodeIdentifier, value);
98 public ImmutableUserLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
99 this.nodeIdentifier = withNodeIdentifier;
104 public ImmutableUserLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
106 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
107 withChild(leafSetEntry);
113 public ImmutableUserLeafSetNodeBuilder<T> withChildValue(final T childValue) {
114 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
115 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
116 .withValue(childValue).build());
119 protected static final class ImmutableUserLeafSetNode<T>
120 extends AbstractNormalizedNode<NodeIdentifier, UserLeafSetNode<?>>
121 implements UserLeafSetNode<T> {
122 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
124 ImmutableUserLeafSetNode(final NodeIdentifier nodeIdentifier,
125 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
126 super(nodeIdentifier);
127 this.children = children;
131 public LeafSetEntryNode<T> childByArg(final NodeWithValue child) {
132 return children.get(child);
136 public LeafSetEntryNode<T> childAt(final int position) {
137 return Iterables.get(children.values(), position);
142 return children.size();
146 public Collection<LeafSetEntryNode<T>> body() {
147 return UnmodifiableCollection.create(children.values());
151 protected Class<UserLeafSetNode<?>> implementedType() {
152 return (Class) UserLeafSetNode.class;
156 protected int valueHashCode() {
157 return children.hashCode();
161 protected boolean valueEquals(final UserLeafSetNode<?> other) {
162 if (other instanceof ImmutableUserLeafSetNode) {
163 return children.equals(((ImmutableUserLeafSetNode<?>) other).children);
165 // Note: performs a size() check first
166 return Iterables.elementsEqual(children.values(), other.body());
169 private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
170 return Collections.unmodifiableMap(children);
175 public ImmutableUserLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
176 return withChild(child);
180 public ImmutableUserLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
181 return withoutChild(key);