*/
package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
-import java.util.Collections;
-import java.util.List;
+import com.google.common.collect.Maps;
+import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
-
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.util.MapAdaptor;
+import org.opendaylight.yangtools.util.UnmodifiableCollection;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
-
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Maps;
+import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode;
public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
+ private static final int DEFAULT_CAPACITY = 4;
+
+ private final Map<NodeWithValue, LeafSetEntryNode<T>> value;
+ private NodeIdentifier nodeIdentifier;
+
+ protected ImmutableLeafSetNodeBuilder() {
+ value = new HashMap<>(DEFAULT_CAPACITY);
+ }
+
+ protected ImmutableLeafSetNodeBuilder(final int sizeHint) {
+ if (sizeHint >= 0) {
+ value = Maps.newHashMapWithExpectedSize(sizeHint);
+ } else {
+ value = new HashMap<>(DEFAULT_CAPACITY);
+ }
+ }
- protected Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value = Maps.newLinkedHashMap();
- protected InstanceIdentifier.NodeIdentifier nodeIdentifier;
+ protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
+ nodeIdentifier = node.getIdentifier();
+ value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
+ }
- public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
+ public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
return new ImmutableLeafSetNodeBuilder<>();
}
+ public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final int sizeHint) {
+ return new ImmutableLeafSetNodeBuilder<>(sizeHint);
+ }
+
+ public static <T> @NonNull ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
+ if (!(node instanceof ImmutableLeafSetNode<?>)) {
+ throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+ }
+
+ return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode<T>) node);
+ }
+
@Override
public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
this.value.put(child.getIdentifier(), child);
return this;
}
+ @Override
+ public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
+ this.value.remove(key);
+ return this;
+ }
+
@Override
public LeafSetNode<T> build() {
- return new ImmutableLeafSetNode<>(nodeIdentifier, value);
+ return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
}
@Override
- public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
- final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
- this.nodeIdentifier = nodeIdentifier;
+ public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
+ this.nodeIdentifier = withNodeIdentifier;
return this;
}
@Override
- public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
- for (LeafSetEntryNode<T> leafSetEntry : value) {
+ public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> withValue) {
+ for (final LeafSetEntryNode<T> leafSetEntry : withValue) {
withChild(leafSetEntry);
}
-
return this;
}
-
@Override
- public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, Map<QName, String> attributes) {
- return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(
- new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value, attributes));
-
+ public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T childValue) {
+ return withChild(ImmutableLeafSetEntryNodeBuilder.<T>create()
+ .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue))
+ .withValue(childValue).build());
}
- @Override
- public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(T value) {
- return withChildValue(value, Collections.<QName,String>emptyMap());
- }
+ protected static final class ImmutableLeafSetNode<T> extends
+ AbstractImmutableNormalizedValueNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
+ LeafSetNode<T> {
+ private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
- private final static class ImmutableLeafSetNode<T> extends
- AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
- Immutable, LeafSetNode<T> {
+ ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier,
+ final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
+ super(nodeIdentifier, UnmodifiableCollection.create(children.values()));
+ this.children = children;
+ }
- private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> mappedChildren;
+ @Override
+ public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
+ return Optional.ofNullable(children.get(child));
+ }
- ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
- final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
- super(nodeIdentifier, ImmutableList.copyOf(children.values()));
- this.mappedChildren = children;
+ @Override
+ public int size() {
+ return children.size();
}
@Override
- public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
- return Optional.fromNullable(mappedChildren.get(child));
+ protected int valueHashCode() {
+ return children.hashCode();
}
+ @Override
+ protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+ return children.equals(((ImmutableLeafSetNode<?>) other).children);
+ }
}
@Override
return withChild(child);
}
+ @Override
+ public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
+ removeChild(final PathArgument key) {
+ return withoutChild(key);
+ }
+
}