BUG-7464: Fix MainNode.cachedSize()
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / LNode.java
1 /*
2  * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import java.util.Map.Entry;
19
20 final class LNode<K, V> extends MainNode<K, V> {
21     final ListMap<K, V> listmap;
22
23     LNode(final ListMap<K, V> listmap) {
24         this.listmap = listmap;
25     }
26
27     LNode(final K k, final V v) {
28         this(ListMap.map(k, v));
29     }
30
31     LNode(final K k1, final V v1, final K k2, final V v2) {
32         this(ListMap.map(k1, v1, k2, v2));
33     }
34
35     LNode<K, V> inserted(final K k, final V v) {
36         return new LNode<> (listmap.add (k, v));
37     }
38
39     MainNode<K, V> removed (final K k, final TrieMap<K, V> ct) {
40         ListMap<K, V> updmap = listmap.remove(k);
41         if (updmap.size () > 1) {
42             return new LNode<> (updmap);
43         }
44
45         final Entry<K, V> kv = updmap.iterator().next();
46         // create it tombed so that it gets compressed on subsequent accesses
47         return new TNode<>(kv.getKey(), kv.getValue(), ct.computeHash(kv.getKey()));
48     }
49
50     Option<V> get(final K k) {
51         return listmap.get(k);
52     }
53
54     @Override
55     int cachedSize(final TrieMap<K, V> ct) {
56         return listmap.size();
57     }
58
59     @Override
60     String string(final int lev) {
61         // (" " * lev) + "LNode(%s)".format(listmap.mkString(", "))
62         return "LNode";
63     }
64 }