/* * 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.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.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.MapNode; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode; public class ImmutableMapNodeBuilder implements CollectionNodeBuilder { private static final int DEFAULT_CAPACITY = 4; private final Map value; private NodeIdentifier nodeIdentifier; protected ImmutableMapNodeBuilder() { this.value = new HashMap<>(DEFAULT_CAPACITY); } protected ImmutableMapNodeBuilder(final int sizeHint) { if (sizeHint >= 0) { this.value = Maps.newHashMapWithExpectedSize(sizeHint); } else { this.value = new HashMap<>(DEFAULT_CAPACITY); } } protected ImmutableMapNodeBuilder(final ImmutableMapNode node) { this.nodeIdentifier = node.getIdentifier(); this.value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children); } public static @NonNull CollectionNodeBuilder create() { return new ImmutableMapNodeBuilder(); } public static @NonNull CollectionNodeBuilder create(final int sizeHint) { return new ImmutableMapNodeBuilder(sizeHint); } public static CollectionNodeBuilder create(final MapNode node) { if (!(node instanceof ImmutableMapNode)) { throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass())); } return new ImmutableMapNodeBuilder((ImmutableMapNode) node); } @Override public CollectionNodeBuilder withChild(final MapEntryNode child) { this.value.put(child.getIdentifier(), child); return this; } @Override public CollectionNodeBuilder withoutChild(final PathArgument key) { this.value.remove(key); return this; } @Override public CollectionNodeBuilder withValue(final Collection withValue) { // TODO replace or putAll ? for (final MapEntryNode mapEntryNode : withValue) { withChild(mapEntryNode); } return this; } @Override public CollectionNodeBuilder withNodeIdentifier(final NodeIdentifier withNodeIdentifier) { this.nodeIdentifier = withNodeIdentifier; return this; } @Override public MapNode build() { return new ImmutableMapNode(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value)); } @Override public CollectionNodeBuilder addChild( final MapEntryNode child) { return withChild(child); } @Override public NormalizedNodeContainerBuilder removeChild( final PathArgument key) { return withoutChild(key); } protected static final class ImmutableMapNode extends AbstractImmutableNormalizedNode> implements MapNode { private final Map children; ImmutableMapNode(final NodeIdentifier nodeIdentifier, final Map children) { super(nodeIdentifier); this.children = children; } @Override public Optional getChild(final NodeIdentifierWithPredicates child) { return Optional.ofNullable(children.get(child)); } @Override public Collection getValue() { return UnmodifiableCollection.create(children.values()); } @Override public int size() { return children.size(); } @Override protected int valueHashCode() { return children.hashCode(); } @Override protected boolean valueEquals(final AbstractImmutableNormalizedNode other) { return children.equals(((ImmutableMapNode) other).children); } } }