e6c29b6bdede7d31f2c9f52750c9c38ac77943dd
[yangtools.git] / third-party / triemap / src / main / java / org / opendaylight / yangtools / triemap / ImmutableTrieMap.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 com.google.common.base.Preconditions.checkNotNull;
19
20 import com.google.common.annotations.Beta;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22 import java.util.Map;
23 import java.util.function.BiFunction;
24 import java.util.function.Function;
25
26 /**
27  * An immutable TrieMap
28  *
29  * @author Robert Varga
30  *
31  * @param <K> the type of keys maintained by this map
32  * @param <V> the type of mapped values
33  */
34 @Beta
35 public final class ImmutableTrieMap<K, V> extends TrieMap<K, V> {
36     private static final long serialVersionUID = 1L;
37
38     @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled by SerializationProxy")
39     private final INode<K, V> root;
40
41     ImmutableTrieMap(final INode<K, V> root, final Equivalence<? super K> equiv) {
42         super(equiv);
43         this.root = checkNotNull(root);
44     }
45
46     @Override
47     public void clear() {
48         throw unsupported();
49     }
50
51     @Override
52     public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
53         throw unsupported();
54     }
55
56     @Override
57     public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
58         throw unsupported();
59     }
60
61     @Override
62     public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
63         throw unsupported();
64     }
65
66     @Override
67     public V merge(final K key, final V value, final BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
68         throw unsupported();
69     }
70
71     @Override
72     public V put(final K key, final V value) {
73         throw unsupported();
74     }
75
76     @Override
77     public void putAll(final Map<? extends K, ? extends V> m) {
78         throw unsupported();
79     }
80
81     @Override
82     public V putIfAbsent(final K key, final V value) {
83         throw unsupported();
84     }
85
86     @Override
87     public V remove(final Object key) {
88         throw unsupported();
89     }
90
91     @Override
92     public boolean remove(final Object key, final Object value) {
93         throw unsupported();
94     }
95
96     @Override
97     public boolean replace(final K key, final V oldValue, final V newValue) {
98         throw unsupported();
99     }
100
101     @Override
102     public V replace(final K key, final V value) {
103         throw unsupported();
104     }
105
106     @Override
107     public int size() {
108         return root.size(this);
109     }
110
111     @Override
112     public final TrieMap<K, V> mutableSnapshot() {
113         return new MutableTrieMap<>(equiv(), new INode<>(new Gen(), root.gcasRead(this)));
114     }
115
116     @Override
117     public ImmutableTrieMap<K, V> immutableSnapshot() {
118         return this;
119     }
120
121     @Override
122     ImmutableEntrySet<K, V> createEntrySet() {
123         return new ImmutableEntrySet<>(this);
124     }
125
126     @Override
127     ImmutableKeySet<K> createKeySet() {
128         return new ImmutableKeySet<>(this);
129     }
130
131     @Override
132     boolean isReadOnly() {
133         return true;
134     }
135
136     @Override
137     ImmutableIterator<K, V> iterator() {
138         return immutableIterator();
139     }
140
141     @Override
142     INode<K, V> RDCSS_READ_ROOT(final boolean abort) {
143         return root;
144     }
145
146     static UnsupportedOperationException unsupported() {
147         return new UnsupportedOperationException("Attempted to modify a read-only view");
148     }
149 }