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.base.Optional;
11 import java.util.Collection;
12 import java.util.HashMap;
14 import org.opendaylight.yangtools.concepts.Immutable;
15 import org.opendaylight.yangtools.util.MapAdaptor;
16 import org.opendaylight.yangtools.util.UnmodifiableCollection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
22 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26 public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, MapNode> {
27 private static final int DEFAULT_CAPACITY = 4;
28 private final Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> value;
29 private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
31 protected ImmutableMapNodeBuilder() {
32 this.value = new HashMap<>(DEFAULT_CAPACITY);
35 protected ImmutableMapNodeBuilder(final int sizeHint) {
36 this.value = new HashMap<>(DEFAULT_CAPACITY);
39 protected ImmutableMapNodeBuilder(final ImmutableMapNode node) {
40 this.nodeIdentifier = node.getIdentifier();
41 this.value = MapAdaptor.getDefaultInstance().takeSnapshot(node.children);
44 public static CollectionNodeBuilder<MapEntryNode, MapNode> create() {
45 return new ImmutableMapNodeBuilder();
48 public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final int sizeHint) {
49 return new ImmutableMapNodeBuilder(sizeHint);
52 public static CollectionNodeBuilder<MapEntryNode, MapNode> create(final MapNode node) {
53 if (!(node instanceof ImmutableMapNode)) {
54 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
57 return new ImmutableMapNodeBuilder((ImmutableMapNode) node);
61 public CollectionNodeBuilder<MapEntryNode, MapNode> withChild(final MapEntryNode child) {
62 this.value.put(child.getIdentifier(), child);
67 public CollectionNodeBuilder<MapEntryNode, MapNode> withoutChild(final YangInstanceIdentifier.PathArgument key) {
68 this.value.remove(key);
73 public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(final Collection<MapEntryNode> value) {
74 // TODO replace or putAll ?
75 for (final MapEntryNode mapEntryNode : value) {
76 withChild(mapEntryNode);
83 public CollectionNodeBuilder<MapEntryNode, MapNode> withNodeIdentifier(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
84 this.nodeIdentifier = nodeIdentifier;
89 public MapNode build() {
90 return new ImmutableMapNode(nodeIdentifier, MapAdaptor.getDefaultInstance().optimize(value));
94 public CollectionNodeBuilder<MapEntryNode, MapNode> addChild(
95 final MapEntryNode child) {
96 return withChild(child);
101 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, MapNode> removeChild(
102 final PathArgument key) {
103 return withoutChild(key);
106 protected static final class ImmutableMapNode extends AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Collection<MapEntryNode>> implements Immutable,MapNode {
108 private final Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children;
110 ImmutableMapNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
111 final Map<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> children) {
112 super(nodeIdentifier);
113 this.children = children;
117 public Optional<MapEntryNode> getChild(final YangInstanceIdentifier.NodeIdentifierWithPredicates child) {
118 return Optional.fromNullable(children.get(child));
122 public Collection<MapEntryNode> getValue() {
123 return UnmodifiableCollection.create(children.values());
127 protected int valueHashCode() {
128 return children.hashCode();
132 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
133 return children.equals(((ImmutableMapNode) other).children);