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.Iterables;
11 import java.util.Collection;
12 import java.util.LinkedHashMap;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.util.UnmodifiableCollection;
16 import org.opendaylight.yangtools.util.UnmodifiableMap;
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.UserMapNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode;
26 public class ImmutableUserMapNodeBuilder implements CollectionNodeBuilder<MapEntryNode, UserMapNode> {
27 private static final int DEFAULT_CAPACITY = 4;
29 private Map<NodeIdentifierWithPredicates, MapEntryNode> value;
30 private NodeIdentifier nodeIdentifier;
31 private boolean dirty;
33 ImmutableUserMapNodeBuilder() {
34 value = new LinkedHashMap<>(DEFAULT_CAPACITY);
38 private ImmutableUserMapNodeBuilder(final int sizeHint) {
40 value = new LinkedHashMap<>(sizeHint + sizeHint / 3);
42 value = new LinkedHashMap<>(DEFAULT_CAPACITY);
47 private ImmutableUserMapNodeBuilder(final ImmutableUserMapNode node) {
48 nodeIdentifier = node.getIdentifier();
49 value = node.children;
53 public static @NonNull CollectionNodeBuilder<MapEntryNode, UserMapNode> create() {
54 return new ImmutableUserMapNodeBuilder();
57 public static @NonNull CollectionNodeBuilder<MapEntryNode, UserMapNode> create(final int sizeHint) {
58 return new ImmutableUserMapNodeBuilder(sizeHint);
61 public static @NonNull CollectionNodeBuilder<MapEntryNode, UserMapNode> create(final UserMapNode node) {
62 if (!(node instanceof ImmutableUserMapNode)) {
63 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
66 return new ImmutableUserMapNodeBuilder((ImmutableUserMapNode) node);
69 private void checkDirty() {
71 value = new LinkedHashMap<>(value);
77 public CollectionNodeBuilder<MapEntryNode, UserMapNode> withChild(final MapEntryNode child) {
79 value.put(child.getIdentifier(), child);
84 public CollectionNodeBuilder<MapEntryNode, UserMapNode> withoutChild(final PathArgument key) {
91 public CollectionNodeBuilder<MapEntryNode, UserMapNode> withValue(final Collection<MapEntryNode> withValue) {
92 // TODO replace or putAll ?
93 for (final MapEntryNode mapEntryNode : withValue) {
94 withChild(mapEntryNode);
101 public CollectionNodeBuilder<MapEntryNode, UserMapNode> withNodeIdentifier(
102 final NodeIdentifier withNodeIdentifier) {
103 nodeIdentifier = withNodeIdentifier;
108 public UserMapNode build() {
110 return new ImmutableUserMapNode(nodeIdentifier, value);
114 public CollectionNodeBuilder<MapEntryNode, UserMapNode> addChild(
115 final MapEntryNode child) {
116 return withChild(child);
121 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, MapEntryNode, UserMapNode> removeChild(
122 final PathArgument key) {
123 return withoutChild(key);
126 protected static final class ImmutableUserMapNode
127 extends AbstractNormalizedNode<NodeIdentifier, UserMapNode> implements UserMapNode {
128 private final Map<NodeIdentifierWithPredicates, MapEntryNode> children;
130 ImmutableUserMapNode(final NodeIdentifier nodeIdentifier,
131 final Map<NodeIdentifierWithPredicates, MapEntryNode> children) {
132 super(nodeIdentifier);
133 this.children = children;
137 public MapEntryNode childByArg(final NodeIdentifierWithPredicates child) {
138 return children.get(child);
142 public MapEntryNode childAt(final int position) {
143 return Iterables.get(children.values(), position);
148 return children.size();
152 public Collection<MapEntryNode> body() {
153 return UnmodifiableCollection.create(children.values());
157 public Map<NodeIdentifierWithPredicates, MapEntryNode> asMap() {
158 return UnmodifiableMap.of(children);
162 protected Class<UserMapNode> implementedType() {
163 return UserMapNode.class;
167 protected int valueHashCode() {
168 // Order is important
170 for (MapEntryNode child : children.values()) {
171 hashCode = 31 * hashCode + child.hashCode();
177 protected boolean valueEquals(final UserMapNode other) {
178 final Map<NodeIdentifierWithPredicates, MapEntryNode> otherChildren;
179 if (other instanceof ImmutableUserMapNode) {
180 otherChildren = ((ImmutableUserMapNode) other).children;
182 otherChildren = other.asMap();
184 return Iterables.elementsEqual(children.values(), otherChildren.values());