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.Maps;
11 import java.util.Collection;
12 import java.util.HashMap;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.util.MapAdaptor;
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.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
25 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
27 public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, MapNode> {
28 private static final int DEFAULT_CAPACITY = 4;
30 private final Map<NodeIdentifierWithPredicates, MapEntryNode> value;
31 private NodeIdentifier nodeIdentifier;
33 protected ImmutableMapNodeBuilder() {
34 this.value = new HashMap<>(DEFAULT_CAPACITY);
37 protected ImmutableMapNodeBuilder(final int sizeHint) {
39 this.value = Maps.newHashMapWithExpectedSize(sizeHint);
41 this.value = new HashMap<>(DEFAULT_CAPACITY);
45 protected ImmutableMapNodeBuilder(final ImmutableMapNode node) {
46 this.nodeIdentifier = node.getIdentifier();
47 this.value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
50 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create() {
51 return new ImmutableMapNodeBuilder();
54 public static @NonNull CollectionNodeBuilder<MapEntryNode, MapNode> create(final int sizeHint) {
55 return new ImmutableMapNodeBuilder(sizeHint);
58 public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final MapNode node) {
59 if (!(node instanceof ImmutableMapNode)) {
60 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
63 return new ImmutableMapNodeBuilder((ImmutableMapNode) node);
67 public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
68 this.value.put(child.getIdentifier(), child);
73 public CollectionNodeBuilder<MapEntryNode, MapNode> withoutChild(final PathArgument key) {
74 this.value.remove(key);
79 public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(final Collection<MapEntryNode> withValue) {
80 // TODO replace or putAll ?
81 for (final MapEntryNode mapEntryNode : withValue) {
82 withChild(mapEntryNode);
89 public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
90 this.nodeIdentifier = withNodeIdentifier;
95 public MapNode build() {
96 return new ImmutableMapNode(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
100 public CollectionNodeBuilder<MapEntryNode, MapNode> addChild(
101 final MapEntryNode child) {
102 return withChild(child);
107 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, MapNode> removeChild(
108 final PathArgument key) {
109 return withoutChild(key);
112 protected static final class ImmutableMapNode
113 extends AbstractImmutableNormalizedNode<NodeIdentifier, Collection<MapEntryNode>> implements MapNode {
115 private final Map<NodeIdentifierWithPredicates, MapEntryNode> children;
117 ImmutableMapNode(final NodeIdentifier nodeIdentifier,
118 final Map<NodeIdentifierWithPredicates, MapEntryNode> children) {
119 super(nodeIdentifier);
120 this.children = children;
124 public Optional<MapEntryNode> getChild(final NodeIdentifierWithPredicates child) {
125 return Optional.ofNullable(children.get(child));
129 public Collection<MapEntryNode> getValue() {
130 return UnmodifiableCollection.create(children.values());
134 protected int valueHashCode() {
135 return children.hashCode();
139 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
140 return children.equals(((ImmutableMapNode) other).children);