Migrate to tech.pantheon.TrieMap
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / TrieMap.java
1 /*
2  * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import static java.util.Objects.requireNonNull;
19
20 import com.google.common.collect.ForwardingObject;
21 import java.io.Serializable;
22 import java.util.Collection;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.function.BiFunction;
27 import java.util.function.Function;
28
29 /**
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.
32  *
33  * @author Aleksandar Prokopec (original Scala implementation)
34  * @author Roman Levenstein (original Java 6 port)
35  * @author Robert Varga
36  *
37  * @param <K> the type of keys maintained by this map
38  * @param <V> the type of mapped values
39  * @deprecated use {@link tech.pantheon.triemap.TrieMap} instead.
40  */
41 @Deprecated
42 public abstract class TrieMap<K, V> extends ForwardingObject implements ConcurrentMap<K,V>, Serializable {
43     private static final long serialVersionUID = 1L;
44
45     private final tech.pantheon.triemap.TrieMap<K, V> delegate;
46
47     TrieMap(final tech.pantheon.triemap.TrieMap<K, V> delegate) {
48         this.delegate = requireNonNull(delegate);
49     }
50
51     public static <K, V> MutableTrieMap<K, V> create() {
52         return new MutableTrieMap<>(tech.pantheon.triemap.TrieMap.create());
53     }
54
55     /**
56      * Returns a snapshot of this TrieMap. This operation is lock-free and
57      * linearizable. Modification operations on this Map and the returned one
58      * are isolated from each other.
59      *
60      * <p>
61      * The snapshot is lazily updated - the first time some branch in the
62      * snapshot or this TrieMap are accessed, they are rewritten. This means
63      * that the work of rebuilding both the snapshot and this TrieMap is
64      * distributed across all the threads doing updates or accesses subsequent
65      * to the snapshot creation.
66      *
67      * @return A read-write TrieMap containing the contents of this map.
68      */
69     public abstract TrieMap<K, V> mutableSnapshot();
70
71     /**
72      * Returns a read-only snapshot of this TrieMap. This operation is lock-free
73      * and linearizable.
74      *
75      * <p>
76      * The snapshot is lazily updated - the first time some branch of this
77      * TrieMap are accessed, it is rewritten. The work of creating the snapshot
78      * is thus distributed across subsequent updates and accesses on this
79      * TrieMap by all threads. Note that the snapshot itself is never rewritten
80      * unlike when calling {@link #mutableSnapshot()}, but the obtained snapshot
81      * cannot be modified.
82      *
83      * <p>
84      * This method is used by other methods such as `size` and `iterator`.
85      *
86      * @return A read-only TrieMap containing the contents of this map.
87      */
88     public abstract ImmutableTrieMap<K, V> immutableSnapshot();
89
90     @Override
91     public final boolean containsKey(final Object key) {
92         return delegate.containsKey(key);
93     }
94
95     @Override
96     public final boolean containsValue(final Object value) {
97         return delegate.containsValue(value);
98     }
99
100     @Override
101     public final Set<Entry<K, V>> entrySet() {
102         return delegate.entrySet();
103     }
104
105     @Override
106     public final Set<K> keySet() {
107         return delegate.keySet();
108     }
109
110     @Override
111     public final V get(final Object key) {
112         return delegate.get(key);
113     }
114
115     @Override
116     public final void clear() {
117         delegate.clear();
118     }
119
120     @Override
121     public final V put(final K key, final V value) {
122         return delegate.put(key, value);
123     }
124
125     @Override
126     public final V putIfAbsent(final K key, final V value) {
127         return delegate.putIfAbsent(key, value);
128     }
129
130     @Override
131     public final V remove(final Object key) {
132         return delegate.remove(key);
133     }
134
135     @Override
136     public final boolean remove(final Object key, final Object value) {
137         return delegate.remove(key, value);
138     }
139
140     @Override
141     public final boolean replace(final K key, final V oldValue, final V newValue) {
142         return delegate.replace(key, oldValue, newValue);
143     }
144
145     @Override
146     public final V replace(final K key, final V value) {
147         return delegate.replace(key, value);
148     }
149
150     @Override
151     public final int size() {
152         return delegate.size();
153     }
154
155     @Override
156     public final boolean isEmpty() {
157         return delegate.isEmpty();
158     }
159
160     @Override
161     public final void putAll(final Map<? extends K, ? extends V> m) {
162         delegate.putAll(m);
163     }
164
165     @Override
166     public final Collection<V> values() {
167         return delegate.values();
168     }
169
170     @Override
171     public final V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
172         return delegate.compute(key, remappingFunction);
173     }
174
175     @Override
176     public final V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
177         return delegate.computeIfAbsent(key, mappingFunction);
178     }
179
180     @Override
181     public final V computeIfPresent(final K key,
182             final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
183         return delegate.computeIfPresent(key, remappingFunction);
184     }
185
186     @Override
187     public final V merge(final K key, final V value,
188             final BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
189         return delegate.merge(key, value, remappingFunction);
190     }
191
192     @Override
193     public final int hashCode() {
194         return delegate.hashCode();
195     }
196
197     @Override
198     public final boolean equals(final Object o) {
199         return delegate.equals(o);
200     }
201
202     @Override
203     protected final tech.pantheon.triemap.TrieMap<K, V> delegate() {
204         return delegate;
205     }
206 }