abd3c1f8d4ebaa0cd9e764b3a3b2eaf4dd12fb9f
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / spi / LazyContainerNode.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Maps;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Map;
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;
18
19 final class LazyContainerNode extends ContainerNode {
20     protected LazyContainerNode(final NormalizedNode<?, ?> data, final Version version) {
21         super(data, version, version);
22     }
23
24     @Override
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()));
30         }
31
32         return Optional.absent();
33     }
34
35     // Assumes default load factor of 0.75 and capacity of 16.
36     private static <K, V> Map<K, V> allocateMap(final int hint) {
37         switch (hint) {
38         case 0:
39         case 1:
40             // Zero does not matter, but will be kept small if it is touched in the future
41             return new HashMap<>(1);
42         case 2:
43             // Two entries, may end up being grown to 4
44             return new HashMap<>(2);
45         case 3:
46             // 4 * 0.75 = 3
47             return new HashMap<>(4);
48         case 4:
49         case 5:
50         case 6:
51             // 8 * 0.75 = 6
52             return new HashMap<>(8);
53         default:
54             // No savings, defer to Guava
55             return Maps.newHashMapWithExpectedSize(hint);
56         }
57     }
58
59     @Override
60     public MutableTreeNode mutable() {
61         /*
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
64          * children retain it.
65          *
66          * The simplest thing to do is to just flush the amortized work and be done
67          * with it.
68          */
69         final Collection<NormalizedNode<?, ?>> oldChildren = castData().getValue();
70
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
73         // than necessary.
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()));
78         }
79
80         return new Mutable(this, children);
81     }
82
83     @SuppressWarnings("unchecked")
84     private NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> castData() {
85         return (NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>>) getData();
86     }
87 }