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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.Maps;
13 import java.util.Collection;
14 import java.util.HashMap;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.util.MapAdaptor;
19 import org.opendaylight.yangtools.util.UnmodifiableMap;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
28 public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, SystemMapNode> {
29 private static final int DEFAULT_CAPACITY = 4;
31 private final Map<NodeIdentifierWithPredicates, MapEntryNode> value;
33 private @Nullable NodeIdentifier nodeIdentifier = null;
35 protected ImmutableMapNodeBuilder() {
36 this.value = new HashMap<>(DEFAULT_CAPACITY);
39 protected ImmutableMapNodeBuilder(final int sizeHint) {
41 this.value = Maps.newHashMapWithExpectedSize(sizeHint);
43 this.value = new HashMap<>(DEFAULT_CAPACITY);
47 protected ImmutableMapNodeBuilder(final SystemMapNode node) {
48 this.nodeIdentifier = node.getIdentifier();
49 this.value = MapAdaptor.getDefaultInstance().takeSnapshot(
50 node instanceof ImmutableMapNode ? ((ImmutableMapNode) node).children : node.asMap());
53 public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> create() {
54 return new ImmutableMapNodeBuilder();
57 public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> create(final int sizeHint) {
58 return new ImmutableMapNodeBuilder(sizeHint);
61 public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> create(final SystemMapNode node) {
62 return new ImmutableMapNodeBuilder(node);
66 public ImmutableMapNodeBuilder withChild(final MapEntryNode child) {
67 this.value.put(child.getIdentifier(), child);
72 public ImmutableMapNodeBuilder withoutChild(final PathArgument key) {
73 this.value.remove(key);
78 public ImmutableMapNodeBuilder withValue(final Collection<MapEntryNode> withValue) {
79 // TODO replace or putAll ?
80 for (final MapEntryNode mapEntryNode : withValue) {
81 withChild(mapEntryNode);
88 public ImmutableMapNodeBuilder withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
89 this.nodeIdentifier = withNodeIdentifier;
94 public SystemMapNode build() {
95 return new ImmutableMapNode(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
99 public ImmutableMapNodeBuilder addChild(final MapEntryNode child) {
100 return withChild(child);
104 public ImmutableMapNodeBuilder removeChild(final PathArgument key) {
105 return withoutChild(key);
108 protected static final class ImmutableMapNode extends AbstractNormalizedNode<NodeIdentifier, SystemMapNode>
109 implements SystemMapNode {
111 private final @NonNull Map<NodeIdentifierWithPredicates, MapEntryNode> children;
113 ImmutableMapNode(final NodeIdentifier nodeIdentifier,
114 final Map<NodeIdentifierWithPredicates, MapEntryNode> children) {
115 super(nodeIdentifier);
116 this.children = requireNonNull(children);
120 public MapEntryNode childByArg(final NodeIdentifierWithPredicates child) {
121 return children.get(child);
125 public Map<NodeIdentifierWithPredicates, MapEntryNode> asMap() {
126 return UnmodifiableMap.of(children);
131 return children.size();
135 protected Class<SystemMapNode> implementedType() {
136 return SystemMapNode.class;
140 protected int valueHashCode() {
141 return children.hashCode();
145 protected boolean valueEquals(final SystemMapNode other) {
146 final Map<NodeIdentifierWithPredicates, MapEntryNode> otherChildren =
147 other instanceof ImmutableMapNode ? ((ImmutableMapNode) other).children : other.asMap();
148 return children.equals(otherChildren);