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 static com.google.common.base.Preconditions.checkNotNull;
19 import static org.opendaylight.yangtools.triemap.LookupResult.RESTART;
21 import com.google.common.annotations.Beta;
22 import java.io.ObjectStreamException;
23 import java.io.Serializable;
24 import java.util.AbstractMap;
25 import java.util.Optional;
27 import java.util.concurrent.ConcurrentMap;
30 * This is a port of Scala's TrieMap class from the Scala Collections library. This implementation does not support
31 * null keys nor null values.
33 * @author Aleksandar Prokopec (original Scala implementation)
34 * @author Roman Levenstein (original Java 6 port)
35 * @author Robert Varga
37 * @param <K> the type of keys maintained by this map
38 * @param <V> the type of mapped values
41 public abstract class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,V>, Serializable {
42 private static final long serialVersionUID = 1L;
44 private final Equivalence<? super K> equiv;
46 private AbstractEntrySet<K, V> entrySet;
48 TrieMap(final Equivalence<? super K> equiv) {
52 public static <K, V> TrieMap<K, V> create() {
53 return new MutableTrieMap<>(Equivalence.equals());
57 * Returns a snapshot of this TrieMap. This operation is lock-free and
60 * The snapshot is lazily updated - the first time some branch in the
61 * snapshot or this TrieMap are accessed, they are rewritten. This means
62 * that the work of rebuilding both the snapshot and this TrieMap is
63 * distributed across all the threads doing updates or accesses subsequent
64 * to the snapshot creation.
66 public abstract TrieMap<K, V> mutableSnapshot();
69 * Returns a read-only snapshot of this TrieMap. This operation is lock-free
72 * The snapshot is lazily updated - the first time some branch of this
73 * TrieMap are accessed, it is rewritten. The work of creating the snapshot
74 * is thus distributed across subsequent updates and accesses on this
75 * TrieMap by all threads. Note that the snapshot itself is never rewritten
76 * unlike when calling the `snapshot` method, but the obtained snapshot
79 * This method is used by other methods such as `size` and `iterator`.
81 public abstract ImmutableTrieMap<K, V> immutableSnapshot();
84 public final boolean containsKey(final Object key) {
85 return get(key) != null;
89 public final boolean containsValue(final Object value) {
90 return super.containsValue(checkNotNull(value));
94 public final Set<Entry<K, V>> entrySet() {
95 AbstractEntrySet<K, V> ret = entrySet;
97 entrySet = ret = createEntrySet();
103 public final V get(final Object key) {
104 @SuppressWarnings("unchecked")
105 final K k = (K) checkNotNull(key);
106 return lookuphc(k, computeHash(k));
110 public abstract void clear();
113 public abstract V put(K key, V value);
116 public abstract V putIfAbsent(K key, V value);
119 public abstract V remove(Object key);
122 public abstract boolean remove(Object key, Object value);
125 public abstract boolean replace(K key, V oldValue, V newValue);
128 public abstract V replace(K key, V value);
131 public abstract int size();
133 /* internal methods implemented by subclasses */
135 abstract AbstractEntrySet<K, V> createEntrySet();
137 abstract boolean isReadOnly();
139 abstract INode<K, V> RDCSS_READ_ROOT(boolean abort);
142 * Return an iterator over a TrieMap.
144 * If this is a read-only snapshot, it would return a read-only iterator.
146 * If it is the original TrieMap or a non-readonly snapshot, it would return
147 * an iterator that would allow for updates.
151 abstract AbstractIterator<K, V> iterator();
153 /* internal methods provided for subclasses */
156 * Return an iterator over a TrieMap.
157 * This is a read-only iterator.
161 final ImmutableIterator<K, V> immutableIterator() {
162 return new ImmutableIterator<>(immutableSnapshot());
165 @SuppressWarnings("null")
166 static <V> V toNullable(final Optional<V> opt) {
167 return opt.orElse(null);
170 final int computeHash(final K k) {
171 return equiv.hash(k);
174 final Object writeReplace() throws ObjectStreamException {
175 return new SerializationProxy(immutableSnapshot(), isReadOnly());
178 /* package-protected utility methods */
180 final Equivalence<? super K> equiv() {
184 final INode<K, V> readRoot() {
185 return RDCSS_READ_ROOT(false);
188 // FIXME: abort = false by default
189 final INode<K, V> readRoot(final boolean abort) {
190 return RDCSS_READ_ROOT(abort);
193 final INode<K, V> RDCSS_READ_ROOT() {
194 return RDCSS_READ_ROOT(false);
197 final boolean equal(final K k1, final K k2) {
198 return equiv.equivalent(k1, k2);
201 /* private implementation methods */
203 @SuppressWarnings("unchecked")
204 private V lookuphc(final K k, final int hc) {
207 // Keep looping as long as RESTART is being indicated
208 res = RDCSS_READ_ROOT().rec_lookup(k, hc, 0, null, this);
209 } while (res == RESTART);