/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl; import com.google.common.collect.Maps; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; 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.common.QName; 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 org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedValueNode; public class ImmutableLeafSetNodeBuilder implements ListNodeBuilder> { private static final int DEFAULT_CAPACITY = 4; private final Map> 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 ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode node) { nodeIdentifier = node.getIdentifier(); value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children); } public static @NonNull ListNodeBuilder> create() { return new ImmutableLeafSetNodeBuilder<>(); } public static @NonNull ListNodeBuilder> create(final int sizeHint) { return new ImmutableLeafSetNodeBuilder<>(sizeHint); } public static @NonNull ListNodeBuilder> create(final LeafSetNode node) { if (!(node instanceof ImmutableLeafSetNode)) { throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass())); } return new ImmutableLeafSetNodeBuilder<>((ImmutableLeafSetNode) node); } @Override public ListNodeBuilder> withChild(final LeafSetEntryNode child) { this.value.put(child.getIdentifier(), child); return this; } @Override public ListNodeBuilder> withoutChild(final PathArgument key) { this.value.remove(key); return this; } @Override public LeafSetNode build() { return new ImmutableLeafSetNode<>(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value)); } @Override public ListNodeBuilder> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) { this.nodeIdentifier = withNodeIdentifier; return this; } @Override public ListNodeBuilder> withValue(final Collection> withValue) { for (final LeafSetEntryNode leafSetEntry : withValue) { withChild(leafSetEntry); } return this; } @Override public ListNodeBuilder> withChildValue(final T childValue, final Map attributes) { final ImmutableLeafSetEntryNodeBuilder b = ImmutableLeafSetEntryNodeBuilder.create(); b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue)); b.withValue(childValue); b.withAttributes(attributes); return withChild(b.build()); } @Override public ListNodeBuilder> withChildValue(final T childValue) { return withChildValue(childValue, Collections.emptyMap()); } protected static final class ImmutableLeafSetNode extends AbstractImmutableNormalizedValueNode>> implements LeafSetNode { private final Map> children; ImmutableLeafSetNode(final NodeIdentifier nodeIdentifier, final Map> children) { super(nodeIdentifier, UnmodifiableCollection.create(children.values())); this.children = children; } @Override public Optional> getChild(final NodeWithValue child) { return Optional.ofNullable(children.get(child)); } @Override protected int valueHashCode() { return children.hashCode(); } @Override protected boolean valueEquals(final AbstractImmutableNormalizedNode other) { return children.equals(((ImmutableLeafSetNode) other).children); } } @Override public NormalizedNodeContainerBuilder, LeafSetNode> addChild( final LeafSetEntryNode child) { return withChild(child); } @Override public NormalizedNodeContainerBuilder, LeafSetNode> removeChild(final PathArgument key) { return withoutChild(key); } }