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 com.google.common.collect.Iterables;
12 import java.util.Collection;
13 import java.util.LinkedHashMap;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.util.UnmodifiableCollection;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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.api.schema.OrderedMapNode;
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 ImmutableOrderedMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, OrderedMapNode> {
28 private static final int DEFAULT_CAPACITY = 4;
30 private Map<NodeIdentifierWithPredicates, MapEntryNode> value;
31 private NodeIdentifier nodeIdentifier;
32 private boolean dirty;
34 protected ImmutableOrderedMapNodeBuilder() {
35 this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
39 protected ImmutableOrderedMapNodeBuilder(final int sizeHint) {
41 this.value = new LinkedHashMap<>(sizeHint + sizeHint / 3);
43 this.value = new LinkedHashMap<>(DEFAULT_CAPACITY);
48 protected ImmutableOrderedMapNodeBuilder(final ImmutableOrderedMapNode node) {
49 this.nodeIdentifier = node.getIdentifier();
50 this.value = node.children;
54 public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create() {
55 return new ImmutableOrderedMapNodeBuilder();
58 public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final int sizeHint) {
59 return new ImmutableOrderedMapNodeBuilder(sizeHint);
62 public static CollectionNodeBuilder<MapEntryNode, OrderedMapNode> create(final MapNode node) {
63 if (!(node instanceof ImmutableOrderedMapNode)) {
64 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
67 return new ImmutableOrderedMapNodeBuilder((ImmutableOrderedMapNode) node);
70 private void checkDirty() {
72 value = new LinkedHashMap<>(value);
78 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withChild(final MapEntryNode child) {
80 this.value.put(child.getIdentifier(), child);
85 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withoutChild(final PathArgument key) {
87 this.value.remove(key);
92 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withValue(final Collection<MapEntryNode> value) {
93 // TODO replace or putAll ?
94 for (final MapEntryNode mapEntryNode : value) {
95 withChild(mapEntryNode);
102 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> withNodeIdentifier(final NodeIdentifier nodeIdentifier) {
103 this.nodeIdentifier = nodeIdentifier;
108 public OrderedMapNode build() {
110 return new ImmutableOrderedMapNode(nodeIdentifier, value);
114 public CollectionNodeBuilder<MapEntryNode, OrderedMapNode> addChild(
115 final MapEntryNode child) {
116 return withChild(child);
121 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, OrderedMapNode> removeChild(
122 final PathArgument key) {
123 return withoutChild(key);
126 protected static final class ImmutableOrderedMapNode
127 extends AbstractImmutableNormalizedNode<NodeIdentifier, Collection<MapEntryNode>>
128 implements Immutable, OrderedMapNode {
130 private final Map<NodeIdentifierWithPredicates, MapEntryNode> children;
132 ImmutableOrderedMapNode(final NodeIdentifier nodeIdentifier,
133 final Map<NodeIdentifierWithPredicates, MapEntryNode> children) {
134 super(nodeIdentifier);
135 this.children = children;
139 public Optional<MapEntryNode> getChild(final NodeIdentifierWithPredicates child) {
140 return Optional.fromNullable(children.get(child));
144 public MapEntryNode getChild(final int position) {
145 return Iterables.get(children.values(), position);
149 protected int valueHashCode() {
150 return children.hashCode();
154 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
155 return children.equals(((ImmutableOrderedMapNode) other).children);
159 public int getSize() {
160 return children.size();
164 public Collection<MapEntryNode> getValue() {
165 return UnmodifiableCollection.create(children.values());