ae0a75438980d912a94c3355313349f00d016ba1
[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 java.util.Objects.requireNonNull;
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 = requireNonNull(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     @SuppressWarnings("checkstyle:parameterName")
78     public void putAll(final Map<? extends K, ? extends V> m) {
79         throw unsupported();
80     }
81
82     @Override
83     public V putIfAbsent(final K key, final V value) {
84         throw unsupported();
85     }
86
87     @Override
88     public V remove(final Object key) {
89         throw unsupported();
90     }
91
92     @Override
93     public boolean remove(final Object key, final Object value) {
94         throw unsupported();
95     }
96
97     @Override
98     public boolean replace(final K key, final V oldValue, final V newValue) {
99         throw unsupported();
100     }
101
102     @Override
103     public V replace(final K key, final V value) {
104         throw unsupported();
105     }
106
107     @Override
108     public int size() {
109         return root.size(this);
110     }
111
112     @Override
113     public TrieMap<K, V> mutableSnapshot() {
114         return new MutableTrieMap<>(equiv(), new INode<>(new Gen(), root.gcasRead(this)));
115     }
116
117     @Override
118     public ImmutableTrieMap<K, V> immutableSnapshot() {
119         return this;
120     }
121
122     @Override
123     ImmutableEntrySet<K, V> createEntrySet() {
124         return new ImmutableEntrySet<>(this);
125     }
126
127     @Override
128     ImmutableKeySet<K> createKeySet() {
129         return new ImmutableKeySet<>(this);
130     }
131
132     @Override
133     boolean isReadOnly() {
134         return true;
135     }
136
137     @Override
138     ImmutableIterator<K, V> iterator() {
139         return immutableIterator();
140     }
141
142     @Override
143     INode<K, V> RDCSS_READ_ROOT(final boolean abort) {
144         return root;
145     }
146
147     static UnsupportedOperationException unsupported() {
148         return new UnsupportedOperationException("Attempted to modify a read-only view");
149     }
150 }