2 * Copyright (c) 2014 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.api.schema.tree.spi;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Maps;
12 import java.util.Collection;
13 import java.util.HashMap;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
19 final class LazyContainerNode extends ContainerNode {
20 protected LazyContainerNode(final NormalizedNode<?, ?> data, final Version version) {
21 super(data, version, version);
25 public Optional<TreeNode> getChild(final PathArgument key) {
26 // We do not cache the instantiated node as it is dirt cheap
27 final Optional<NormalizedNode<?, ?>> child = castData().getChild(key);
28 if (child.isPresent()) {
29 return Optional.of(TreeNodeFactory.createTreeNode(child.get(), getVersion()));
32 return Optional.absent();
35 // Assumes default load factor of 0.75 and capacity of 16.
36 private static <K, V> Map<K, V> allocateMap(final int hint) {
40 // Zero does not matter, but will be kept small if it is touched in the future
41 return new HashMap<>(1);
43 // Two entries, may end up being grown to 4
44 return new HashMap<>(2);
47 return new HashMap<>(4);
52 return new HashMap<>(8);
54 // No savings, defer to Guava
55 return Maps.newHashMapWithExpectedSize(hint);
60 public MutableTreeNode mutable() {
62 * We are creating a mutable view of the data, which means that the version
63 * is going to probably change -- and we need to make sure any unmodified
66 * The simplest thing to do is to just flush the amortized work and be done
69 final Collection<NormalizedNode<?, ?>> oldChildren = castData().getValue();
71 // Use a proper sizing hint here, as the default size can suck for both extremely large and extremely small
72 // collections. For the large ones we end up rehashing the table, for small ones we end up using more space
74 final Map<PathArgument, TreeNode> children = allocateMap(oldChildren.size());
75 for (NormalizedNode<?, ?> child : oldChildren) {
76 PathArgument id = child.getIdentifier();
77 children.put(id, TreeNodeFactory.createTreeNode(child, getVersion()));
80 return new Mutable(this, children);
83 @SuppressWarnings("unchecked")
84 private NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> castData() {
85 return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) getData();