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 java.util.Optional;
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.OrderedLeafSetNode;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
28 public class ImmutableOrderedLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
29 private Map<NodeWithValue, LeafSetEntryNode<T>> value;
30 private NodeIdentifier nodeIdentifier;
31 private boolean dirty;
33 protected ImmutableOrderedLeafSetNodeBuilder() {
34 value = new LinkedHashMap<>();
38 protected ImmutableOrderedLeafSetNodeBuilder(final ImmutableOrderedLeafSetNode<T> node) {
39 nodeIdentifier = node.getIdentifier();
40 value = node.getChildren();
44 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
45 return new ImmutableOrderedLeafSetNodeBuilder<>();
48 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
49 if (!(node instanceof ImmutableOrderedLeafSetNode<?>)) {
50 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
53 return new ImmutableOrderedLeafSetNodeBuilder<>((ImmutableOrderedLeafSetNode<T>) node);
56 private void checkDirty() {
58 value = new LinkedHashMap<>(value);
64 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
66 this.value.put(child.getIdentifier(), child);
71 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
73 this.value.remove(key);
78 public OrderedLeafSetNode<T> build() {
80 return new ImmutableOrderedLeafSetNode<>(nodeIdentifier, value);
84 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
85 this.nodeIdentifier = withNodeIdentifier;
90 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
92 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
93 withChild(leafSetEntry);
99 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
100 return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
101 .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
102 .withValue(childValue).build());
105 protected static final class ImmutableOrderedLeafSetNode<T> extends
106 AbstractImmutableNormalizedNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
107 OrderedLeafSetNode<T> {
109 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
111 ImmutableOrderedLeafSetNode(final NodeIdentifier nodeIdentifier,
112 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
113 super(nodeIdentifier);
114 this.children = children;
118 public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
119 return Optional.ofNullable(children.get(child));
123 public LeafSetEntryNode<T> getChild(final int position) {
124 return Iterables.get(children.values(), position);
128 protected int valueHashCode() {
129 return children.hashCode();
132 private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
133 return Collections.unmodifiableMap(children);
137 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
138 return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
142 public int getSize() {
143 return children.size();
147 public Collection<LeafSetEntryNode<T>> getValue() {
148 return UnmodifiableCollection.create(children.values());
153 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
154 final LeafSetEntryNode<T> child) {
155 return withChild(child);
159 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
160 removeChild(final PathArgument key) {
161 return withoutChild(key);