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.AbstractMap.SimpleImmutableEntry;
19 import java.util.Iterator;
20 import java.util.Map.Entry;
21 import java.util.NoSuchElementException;
22 import java.util.Optional;
25 * Mimic immutable ListMap in Scala
27 * @author Roman Levenstein <romixlev@gmail.com>
31 final class ListMap<K, V> {
35 // Modified during remove0 only
36 private ListMap<K, V> next;
38 private ListMap(final K k, final V v) {
42 private ListMap(final K k, final V v, final ListMap<K, V> next) {
48 static <K,V> ListMap<K, V> map(final K k1, final V v1, final K k2, final V v2) {
49 return new ListMap<>(k1, v1, new ListMap<>(k2, v2));
52 Optional<Entry<K, V>> maybeSingleton() {
53 return next != null ? Optional.empty() : Optional.of(new SimpleImmutableEntry<>(k, v));
58 for (ListMap<?, ?> wlk = next; wlk != null; wlk = wlk.next) {
64 Optional<V> get(final K key) {
66 return Optional.of(v);
69 // We do not perform recursion on purpose here, so we do not run out of stack if the key hashing fails.
70 for (ListMap<K, V> m = next; m != null; m = m.next) {
71 if (key.equals(m.k)) {
72 return Optional.of(m.v);
76 return Optional.empty();
79 ListMap<K,V> add(final K key, final V value) {
80 return new ListMap<>(key, value, remove(key));
83 ListMap<K, V> remove(final K key) {
91 private boolean contains(final K key) {
96 // We do not perform recursion on purpose here, so we do not run out of stack if the key hashing fails.
97 for (ListMap<K, V> m = next; m != null; m = m.next) {
98 if (key.equals(m.k)) {
106 private ListMap<K, V> remove0(final K key) {
107 ListMap<K, V> n = this;
108 ListMap<K, V> ret = null;
109 ListMap<K, V> lastN = null;
111 if (key.equals(n.k)) {
117 lastN.next = new ListMap<>(n.k, n.v);
120 ret = new ListMap<>(n.k, n.v);
128 Iterator<Entry<K, V>> iterator() {
129 return new NodeIterator<>(this);
132 static final class NodeIterator<K,V> implements Iterator<Entry<K, V>> {
133 private ListMap<K, V> n;
135 NodeIterator(final ListMap<K, V> n) {
140 public boolean hasNext() {
145 public Entry<K, V> next() {
147 throw new NoSuchElementException();
150 final Entry<K, V> res = new SimpleImmutableEntry<>(n.k, n.v);