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.UserLeafSetNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26 public class ImmutableUserLeafSetNodeBuilder<T> implements ListNodeBuilder<T, UserLeafSetNode<T>> {
27 private Map<NodeWithValue, LeafSetEntryNode<T>> value;
28 private NodeIdentifier nodeIdentifier;
29 private boolean dirty;
31 protected ImmutableUserLeafSetNodeBuilder() {
32 value = new LinkedHashMap<>();
36 protected ImmutableUserLeafSetNodeBuilder(final ImmutableUserLeafSetNode<T> node) {
37 nodeIdentifier = node.getIdentifier();
38 value = node.getChildren();
42 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create() {
43 return new ImmutableUserLeafSetNodeBuilder<>();
46 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(
47 final UserLeafSetNode<T> node) {
48 if (!(node instanceof ImmutableUserLeafSetNode<?>)) {
49 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
52 return new ImmutableUserLeafSetNodeBuilder<>((ImmutableUserLeafSetNode<T>) node);
55 private void checkDirty() {
57 value = new LinkedHashMap<>(value);
63 public ImmutableUserLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
65 this.value.put(child.getIdentifier(), child);
70 public ImmutableUserLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
72 this.value.remove(key);
77 public UserLeafSetNode<T> build() {
79 return new ImmutableUserLeafSetNode<>(nodeIdentifier, value);
83 public ImmutableUserLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
84 this.nodeIdentifier = withNodeIdentifier;
89 public ImmutableUserLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
91 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
92 withChild(leafSetEntry);
98 public ImmutableUserLeafSetNodeBuilder<T> withChildValue(final T childValue) {
99 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
100 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
101 .withValue(childValue).build());
104 protected static final class ImmutableUserLeafSetNode<T>
105 extends AbstractImmutableNormalizedNode<NodeIdentifier, UserLeafSetNode<?>>
106 implements UserLeafSetNode<T> {
107 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
109 ImmutableUserLeafSetNode(final NodeIdentifier nodeIdentifier,
110 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
111 super(nodeIdentifier);
112 this.children = children;
116 public LeafSetEntryNode<T> childByArg(final NodeWithValue child) {
117 return children.get(child);
121 public LeafSetEntryNode<T> getChild(final int position) {
122 return Iterables.get(children.values(), position);
127 return children.size();
131 public Collection<LeafSetEntryNode<T>> body() {
132 return UnmodifiableCollection.create(children.values());
136 protected Class<UserLeafSetNode<?>> implementedType() {
137 return (Class) UserLeafSetNode.class;
141 protected int valueHashCode() {
142 return children.hashCode();
146 protected boolean valueEquals(final UserLeafSetNode<?> other) {
147 return children.equals(((ImmutableUserLeafSetNode<?>) other).children);
150 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
151 justification = "https://github.com/spotbugs/spotbugs/issues/811")
152 private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
153 return Collections.unmodifiableMap(children);
158 public ImmutableUserLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
159 return withChild(child);
163 public ImmutableUserLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
164 return withoutChild(key);