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.common.QName;
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>> {
31 private Map<NodeWithValue, LeafSetEntryNode<T>> value;
32 private NodeIdentifier nodeIdentifier;
33 private boolean dirty;
35 protected ImmutableOrderedLeafSetNodeBuilder() {
36 value = new LinkedHashMap<>();
40 protected ImmutableOrderedLeafSetNodeBuilder(final ImmutableOrderedLeafSetNode<T> node) {
41 nodeIdentifier = node.getIdentifier();
42 value = node.getChildren();
46 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
47 return new ImmutableOrderedLeafSetNodeBuilder<>();
50 public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
51 if (!(node instanceof ImmutableOrderedLeafSetNode<?>)) {
52 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
55 return new ImmutableOrderedLeafSetNodeBuilder<>((ImmutableOrderedLeafSetNode<T>) node);
58 private void checkDirty() {
60 value = new LinkedHashMap<>(value);
66 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
68 this.value.put(child.getIdentifier(), child);
73 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
75 this.value.remove(key);
80 public OrderedLeafSetNode<T> build() {
82 return new ImmutableOrderedLeafSetNode<>(nodeIdentifier, value);
86 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
87 this.nodeIdentifier = withNodeIdentifier;
92 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
94 for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
95 withChild(leafSetEntry);
101 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue,
102 final Map<QName, String> attributes) {
103 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
104 b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue));
105 b.withValue(childValue);
106 b.withAttributes(attributes);
107 return withChild(b.build());
111 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
112 return withChildValue(childValue, Collections.emptyMap());
115 protected static final class ImmutableOrderedLeafSetNode<T> extends
116 AbstractImmutableNormalizedNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
117 OrderedLeafSetNode<T> {
119 private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
121 ImmutableOrderedLeafSetNode(final NodeIdentifier nodeIdentifier,
122 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
123 super(nodeIdentifier);
124 this.children = children;
128 public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
129 return Optional.ofNullable(children.get(child));
133 public LeafSetEntryNode<T> getChild(final int position) {
134 return Iterables.get(children.values(), position);
138 protected int valueHashCode() {
139 return children.hashCode();
142 private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
143 return Collections.unmodifiableMap(children);
147 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
148 return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
152 public int getSize() {
153 return children.size();
157 public Collection<LeafSetEntryNode<T>> getValue() {
158 return UnmodifiableCollection.create(children.values());
163 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
164 final LeafSetEntryNode<T> child) {
165 return withChild(child);
169 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
170 removeChild(final PathArgument key) {
171 return withoutChild(key);