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 java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.util.UnmodifiableCollection;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
29 public class ImmutableOrderedLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
30 private Map<NodeWithValue, LeafSetEntryNode<T>> value;
31 private NodeIdentifier nodeIdentifier;
32 private boolean dirty;
34 protected ImmutableOrderedLeafSetNodeBuilder() {
35 value = new LinkedHashMap<>();
39 protected ImmutableOrderedLeafSetNodeBuilder(final ImmutableOrderedLeafSetNode<T> node) {
40 nodeIdentifier = node.getIdentifier();
41 value = node.getChildren();
45 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
46 return new ImmutableOrderedLeafSetNodeBuilder<>();
49 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
50 if (!(node instanceof ImmutableOrderedLeafSetNode<?>)) {
51 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
54 return new ImmutableOrderedLeafSetNodeBuilder<>((ImmutableOrderedLeafSetNode<T>) node);
57 private void checkDirty() {
59 value = new LinkedHashMap<>(value);
65 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
67 this.value.put(child.getIdentifier(), child);
72 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
74 this.value.remove(key);
79 public OrderedLeafSetNode<T> build() {
81 return new ImmutableOrderedLeafSetNode<>(nodeIdentifier, value);
85 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
86 this.nodeIdentifier = withNodeIdentifier;
91 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
93 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
94 withChild(leafSetEntry);
100 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
101 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
102 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
103 .withValue(childValue).build());
106 protected static final class ImmutableOrderedLeafSetNode<T> extends
107 AbstractImmutableNormalizedNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
108 OrderedLeafSetNode<T> {
110 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
112 ImmutableOrderedLeafSetNode(final NodeIdentifier nodeIdentifier,
113 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
114 super(nodeIdentifier);
115 this.children = children;
119 public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
120 return Optional.ofNullable(children.get(child));
124 public LeafSetEntryNode<T> getChild(final int position) {
125 return Iterables.get(children.values(), position);
129 protected int valueHashCode() {
130 return children.hashCode();
133 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
134 justification = "https://github.com/spotbugs/spotbugs/issues/811")
135 private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
136 return Collections.unmodifiableMap(children);
140 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
141 return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
145 public int getSize() {
146 return children.size();
150 public Collection<LeafSetEntryNode<T>> getValue() {
151 return UnmodifiableCollection.create(children.values());
156 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
157 final LeafSetEntryNode<T> child) {
158 return withChild(child);
162 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
163 removeChild(final PathArgument key) {
164 return withoutChild(key);