4ea607e5c02b64d38625b3f7efed3a42358bbba0
[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 java.util.Map;
22 import java.util.function.BiFunction;
23 import java.util.function.Function;
24
25 /**
26  * An immutable TrieMap
27  *
28  * @author Robert Varga
29  *
30  * @param <K> the type of keys maintained by this map
31  * @param <V> the type of mapped values
32  */
33 @Beta
34 public final class ImmutableTrieMap<K, V> extends TrieMap<K, V> {
35     private final INode<K, V> root;
36
37     ImmutableTrieMap(final INode<K, V> root, final Equivalence<? super K> equiv) {
38         super(equiv);
39         this.root = checkNotNull(root);
40     }
41
42     @Override
43     public void clear() {
44         throw unsupported();
45     }
46
47     @Override
48     public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
49         throw unsupported();
50     }
51
52     @Override
53     public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
54         throw unsupported();
55     }
56
57     @Override
58     public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
59         throw unsupported();
60     }
61
62     @Override
63     public V merge(final K key, final V value, final BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
64         throw unsupported();
65     }
66
67     @Override
68     public V put(final K key, final V value) {
69         throw unsupported();
70     }
71
72     @Override
73     public void putAll(final Map<? extends K, ? extends V> m) {
74         throw unsupported();
75     }
76
77     @Override
78     public V putIfAbsent(final K key, final V value) {
79         throw unsupported();
80     }
81
82     @Override
83     public V remove(final Object key) {
84         throw unsupported();
85     }
86
87     @Override
88     public boolean remove(final Object key, final Object value) {
89         throw unsupported();
90     }
91
92     @Override
93     public boolean replace(final K key, final V oldValue, final V newValue) {
94         throw unsupported();
95     }
96
97     @Override
98     public V replace(final K key, final V value) {
99         throw unsupported();
100     }
101
102     @Override
103     public int size() {
104         return root.cachedSize(this);
105     }
106
107     @Override
108     public final TrieMap<K, V> mutableSnapshot() {
109         return new MutableTrieMap<>(equiv(), new INode<>(new Gen(), root.gcasRead(this)));
110     }
111
112     @Override
113     public ImmutableTrieMap<K, V> immutableSnapshot() {
114         return this;
115     }
116
117     @Override
118     boolean isReadOnly() {
119         return true;
120     }
121
122     @Override
123     INode<K, V> RDCSS_READ_ROOT(final boolean abort) {
124         return root;
125     }
126
127     private static UnsupportedOperationException unsupported() {
128         return new UnsupportedOperationException("Attempted to modify a read-only view");
129     }
130 }