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;
20 import java.util.Optional;
22 final class LNode<K, V> extends MainNode<K, V> {
23 private final LNodeEntries<K, V> listmap;
25 private LNode(final LNodeEntries<K, V> listmap) {
26 this.listmap = listmap;
29 LNode(final K k1, final V v1, final K k2, final V v2) {
30 this(LNodeEntries.map(k1, v1, k2, v2));
33 LNode<K, V> insertChild( final K k, final V v) {
34 return new LNode<>(listmap.insert(k, v));
37 MainNode<K, V> removeChild(final LNodeEntry<K, V> entry, final int hc) {
38 // We only ever create ListMaps with two or more entries, and remove them as soon as they reach one element
39 // (below), so we cannot observe a null return here.
40 final LNodeEntries<K, V> map = listmap.remove(entry);
41 final Optional<Entry<K, V>> maybeKv = map.maybeSingleton();
42 if (maybeKv.isPresent()) {
43 final Entry<K, V> kv = maybeKv.get();
44 // create it tombed so that it gets compressed on subsequent accesses
45 return new TNode<>(kv.getKey(), kv.getValue(), hc);
48 return new LNode<>(map);
51 MainNode<K, V> replaceChild(final LNodeEntry<K, V> entry, final V v) {
52 return new LNode<>(listmap.replace(entry, v));
55 LNodeEntry<K, V> get(final Equivalence<? super K> equiv, final K k) {
56 return listmap.findEntry(equiv, k);
60 int cachedSize(final TrieMap<K, V> ct) {
61 return listmap.size();
65 String string(final int lev) {
66 // (" " * lev) + "LNode(%s)".format(listmap.mkString(", "))
70 Iterator<Entry<K, V>> iterator() {
71 return listmap.iterator();