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.UserLeafSetNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
25 public class ImmutableUserLeafSetNodeBuilder<T> implements ListNodeBuilder<T, UserLeafSetNode<T>> {
26 private Map<NodeWithValue, LeafSetEntryNode<T>> value;
27 private NodeIdentifier nodeIdentifier;
28 private boolean dirty;
30 ImmutableUserLeafSetNodeBuilder() {
31 value = new LinkedHashMap<>();
35 ImmutableUserLeafSetNodeBuilder(final ImmutableUserLeafSetNode<T> node) {
36 nodeIdentifier = node.name();
37 value = node.getChildren();
41 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create() {
42 return new ImmutableUserLeafSetNodeBuilder<>();
45 public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(
46 final UserLeafSetNode<T> node) {
47 if (!(node instanceof ImmutableUserLeafSetNode<?>)) {
48 throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
51 return new ImmutableUserLeafSetNodeBuilder<>((ImmutableUserLeafSetNode<T>) node);
54 private void checkDirty() {
56 value = new LinkedHashMap<>(value);
62 public ImmutableUserLeafSetNodeBuilder<T> withChild(final LeafSetEntryNode<T> child) {
64 value.put(child.name(), child);
69 public ImmutableUserLeafSetNodeBuilder<T> withoutChild(final PathArgument key) {
76 public UserLeafSetNode<T> build() {
78 return new ImmutableUserLeafSetNode<>(nodeIdentifier, value);
82 public ImmutableUserLeafSetNodeBuilder<T> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
83 nodeIdentifier = withNodeIdentifier;
88 public ImmutableUserLeafSetNodeBuilder<T> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
90 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
91 withChild(leafSetEntry);
97 public ImmutableUserLeafSetNodeBuilder<T> withChildValue(final T childValue) {
98 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
99 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
100 .withValue(childValue).build());
103 protected static final class ImmutableUserLeafSetNode<T>
104 extends AbstractNormalizedNode<NodeIdentifier, UserLeafSetNode<?>>
105 implements UserLeafSetNode<T> {
106 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
108 ImmutableUserLeafSetNode(final NodeIdentifier nodeIdentifier,
109 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
110 super(nodeIdentifier);
111 this.children = children;
115 public LeafSetEntryNode<T> childByArg(final NodeWithValue child) {
116 return children.get(child);
120 public LeafSetEntryNode<T> childAt(final int position) {
121 return Iterables.get(children.values(), position);
126 return children.size();
130 public Collection<LeafSetEntryNode<T>> body() {
131 return UnmodifiableCollection.create(children.values());
135 protected Class<UserLeafSetNode<?>> implementedType() {
136 return (Class) UserLeafSetNode.class;
140 protected int valueHashCode() {
141 return children.hashCode();
145 protected boolean valueEquals(final UserLeafSetNode<?> other) {
146 if (other instanceof ImmutableUserLeafSetNode<?> immutableOther) {
147 return children.equals(immutableOther.children);
149 // Note: performs a size() check first
150 return Iterables.elementsEqual(children.values(), other.body());
153 private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
154 return Collections.unmodifiableMap(children);
159 public ImmutableUserLeafSetNodeBuilder<T> addChild(final LeafSetEntryNode<T> child) {
160 return withChild(child);
164 public ImmutableUserLeafSetNodeBuilder<T> removeChild(final PathArgument key) {
165 return withoutChild(key);