2 * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.opendaylight.yangtools.triemap;
18 import java.util.Iterator;
19 import java.util.Map.Entry;
21 final class LNode<K, V> extends MainNode<K, V> {
22 private final ListMap<K, V> listmap;
24 private LNode(final ListMap<K, V> listmap) {
25 this.listmap = listmap;
28 LNode(final K k1, final V v1, final K k2, final V v2) {
29 this(ListMap.map(k1, v1, k2, v2));
32 LNode<K, V> inserted(final K k, final V v) {
33 return new LNode<>(listmap.add(k, v));
36 MainNode<K, V> removed(final K k, final TrieMap<K, V> ct) {
37 // We only ever create ListMaps with two or more entries, and remove them as soon as they reach one element
38 // (below), so we cannot observe a null return here.
39 final ListMap<K, V> updmap = listmap.remove(k);
40 if (updmap.size() > 1) {
41 return new LNode<>(updmap);
44 final Entry<K, V> kv = updmap.iterator().next();
45 // create it tombed so that it gets compressed on subsequent accesses
46 return new TNode<>(kv.getKey(), kv.getValue(), ct.computeHash(kv.getKey()));
49 Option<V> get(final K k) {
50 return listmap.get(k);
54 int cachedSize(final TrieMap<K, V> ct) {
55 return listmap.size();
59 String string(final int lev) {
60 // (" " * lev) + "LNode(%s)".format(listmap.mkString(", "))
64 Iterator<Entry<K, V>> iterator() {
65 return listmap.iterator();