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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.LinkedHashMap;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.util.UnmodifiableCollection;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
26 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
28 public class ImmutableUserLeafSetNodeBuilder<T> implements ListNodeBuilder<T, UserLeafSetNode<T>> {
29 private Map<NodeWithValue, LeafSetEntryNode<T>> value;
30 private NodeIdentifier nodeIdentifier;
31 private boolean dirty;
33 ImmutableUserLeafSetNodeBuilder() {
34 value = new LinkedHashMap<>();
38 ImmutableUserLeafSetNodeBuilder(final ImmutableUserLeafSetNode<T> node) {
39 nodeIdentifier = node.getIdentifier();
40 value = node.getChildren();
44 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create() {
45 return new ImmutableUserLeafSetNodeBuilder<>();
48 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(
49 final UserLeafSetNode<T> node) {
50 if (!(node instanceof ImmutableUserLeafSetNode<?>)) {
51 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
54 return new ImmutableUserLeafSetNodeBuilder<>((ImmutableUserLeafSetNode<T>) node);
57 @Deprecated(since = "6.0.7", forRemoval = true)
58 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(final LeafListSchemaNode schema) {
59 return new SchemaAwareImmutableOrderedLeafSetNodeBuilder<>(schema);
62 @Deprecated(since = "6.0.7", forRemoval = true)
63 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(final LeafListSchemaNode schema,
64 final LeafSetNode<T> node) {
65 if (node instanceof ImmutableUserLeafSetNode<?>) {
66 return new SchemaAwareImmutableOrderedLeafSetNodeBuilder<>(schema, (ImmutableUserLeafSetNode<T>) node);
68 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
71 private void checkDirty() {
73 value = new LinkedHashMap<>(value);
79 public ImmutableUserLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
81 this.value.put(child.getIdentifier(), child);
86 public ImmutableUserLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
88 this.value.remove(key);
93 public UserLeafSetNode<T> build() {
95 return new ImmutableUserLeafSetNode<>(nodeIdentifier, value);
99 public ImmutableUserLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
100 this.nodeIdentifier = withNodeIdentifier;
105 public ImmutableUserLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
107 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
108 withChild(leafSetEntry);
114 public ImmutableUserLeafSetNodeBuilder<T> withChildValue(final T childValue) {
115 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
116 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
117 .withValue(childValue).build());
120 protected static final class ImmutableUserLeafSetNode<T>
121 extends AbstractNormalizedNode<NodeIdentifier, UserLeafSetNode<?>>
122 implements UserLeafSetNode<T> {
123 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
125 ImmutableUserLeafSetNode(final NodeIdentifier nodeIdentifier,
126 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
127 super(nodeIdentifier);
128 this.children = children;
132 public LeafSetEntryNode<T> childByArg(final NodeWithValue child) {
133 return children.get(child);
137 public LeafSetEntryNode<T> childAt(final int position) {
138 return Iterables.get(children.values(), position);
143 return children.size();
147 public Collection<LeafSetEntryNode<T>> body() {
148 return UnmodifiableCollection.create(children.values());
152 protected Class<UserLeafSetNode<?>> implementedType() {
153 return (Class) UserLeafSetNode.class;
157 protected int valueHashCode() {
158 return children.hashCode();
162 protected boolean valueEquals(final UserLeafSetNode<?> other) {
163 return children.equals(((ImmutableUserLeafSetNode<?>) other).children);
166 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
167 justification = "https://github.com/spotbugs/spotbugs/issues/811")
168 private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
169 return Collections.unmodifiableMap(children);
174 public ImmutableUserLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
175 return withChild(child);
179 public ImmutableUserLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
180 return withoutChild(key);