b4a32244466042debb13ed32e0a9019ed5747601
[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 final class LNode<K, V> extends MainNode<K, V> {
19     // Internally-linked single list of of entries
20     private final LNodeEntries<K, V> entries;
21     private final int size;
22
23     private LNode(final LNodeEntries<K, V> entries, final int size) {
24         this.entries = entries;
25         this.size = size;
26     }
27
28     LNode(final K k1, final V v1, final K k2, final V v2) {
29         this(LNodeEntries.map(k1, v1, k2, v2), 2);
30     }
31
32     LNode<K, V> insertChild( final K k, final V v) {
33         return new LNode<>(entries.insert(k, v), size + 1);
34     }
35
36     MainNode<K, V> removeChild(final LNodeEntry<K, V> entry, final int hc) {
37         // While remove() can return null, that case will never happen here, as we are starting off with two entries
38         // so we cannot observe a null return here.
39         final LNodeEntries<K, V> map = entries.remove(entry);
40
41         // If the returned LNode would have only one element, we turn it into a TNode, hence above null return from
42         // remove() can never happen.
43         if (size == 2) {
44             // create it tombed so that it gets compressed on subsequent accesses
45             return new TNode<>(map.getKey(), map.getValue(), hc);
46         }
47
48         return new LNode<>(map, size - 1);
49     }
50
51     MainNode<K, V> replaceChild(final LNodeEntry<K, V> entry, final V v) {
52         return new LNode<>(entries.replace(entry, v), size);
53     }
54
55     LNodeEntry<K, V> get(final Equivalence<? super K> equiv, final K k) {
56         return entries.findEntry(equiv, k);
57     }
58
59     LNodeEntries<K, V> entries() {
60         return entries;
61     }
62
63     @Override
64     int cachedSize(final TrieMap<?, ?> ct) {
65         return size;
66     }
67
68     @Override
69     String string(final int lev) {
70         // (" " * lev) + "LNode(%s)".format(listmap.mkString(", "))
71         return "LNode";
72     }
73 }