/* * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.opendaylight.yangtools.triemap; import java.util.Iterator; import java.util.Map.Entry; import java.util.Optional; final class LNode extends MainNode { private final ListMap listmap; private LNode(final ListMap listmap) { this.listmap = listmap; } LNode(final K k1, final V v1, final K k2, final V v2) { this(ListMap.map(k1, v1, k2, v2)); } LNode inserted(final K k, final V v) { return new LNode<>(listmap.add(k, v)); } // FIXME: can we also get the hashcode ? MainNode removed(final K k, final TrieMap ct) { // We only ever create ListMaps with two or more entries, and remove them as soon as they reach one element // (below), so we cannot observe a null return here. final ListMap map = listmap.remove(k); final Optional> maybeKv = map.maybeSingleton(); if (maybeKv.isPresent()) { final Entry kv = maybeKv.get(); // create it tombed so that it gets compressed on subsequent accesses return new TNode<>(kv.getKey(), kv.getValue(), ct.computeHash(kv.getKey())); } return new LNode<>(map); } Optional get(final K k) { return listmap.get(k); } @Override int cachedSize(final TrieMap ct) { return listmap.size(); } @Override String string(final int lev) { // (" " * lev) + "LNode(%s)".format(listmap.mkString(", ")) return "LNode"; } Iterator> iterator() { return listmap.iterator(); } }