1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.trie;
18
19 import java.io.Serializable;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Set;
26 import java.util.SortedMap;
27
28 import org.apache.commons.collections4.OrderedMapIterator;
29 import org.apache.commons.collections4.Trie;
30 import org.apache.commons.collections4.Unmodifiable;
31 import org.apache.commons.collections4.iterators.UnmodifiableOrderedMapIterator;
32 import org.apache.commons.collections4.map.UnmodifiableEntrySet;
33
34
35
36
37
38
39
40
41 public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodifiable {
42
43
44 private static final long serialVersionUID = -7156426030315945159L;
45
46
47
48
49
50
51
52
53
54
55 public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) {
56 if (trie instanceof Unmodifiable) {
57 @SuppressWarnings("unchecked")
58 final Trie<K, V> tmpTrie = (Trie<K, V>) trie;
59 return tmpTrie;
60 }
61 return new UnmodifiableTrie<>(trie);
62 }
63
64
65
66
67 private final Trie<K, V> delegate;
68
69
70
71
72
73
74
75 public UnmodifiableTrie(final Trie<K, ? extends V> trie) {
76 @SuppressWarnings("unchecked")
77 final Trie<K, V> tmpTrie = (Trie<K, V>) Objects.requireNonNull(trie, "trie");
78 this.delegate = tmpTrie;
79 }
80
81
82
83
84
85
86 @Override
87 public void clear() {
88 throw new UnsupportedOperationException();
89 }
90
91 @Override
92 public Comparator<? super K> comparator() {
93 return delegate.comparator();
94 }
95
96 @Override
97 public boolean containsKey(final Object key) {
98 return delegate.containsKey(key);
99 }
100
101 @Override
102 public boolean containsValue(final Object value) {
103 return delegate.containsValue(value);
104 }
105
106 @Override
107 public Set<Entry<K, V>> entrySet() {
108 return UnmodifiableEntrySet.unmodifiableEntrySet(delegate.entrySet());
109 }
110
111 @Override
112 public boolean equals(final Object obj) {
113 return delegate.equals(obj);
114 }
115
116 @Override
117 public K firstKey() {
118 return delegate.firstKey();
119 }
120
121 @Override
122 public V get(final Object key) {
123 return delegate.get(key);
124 }
125
126 @Override
127 public int hashCode() {
128 return delegate.hashCode();
129 }
130
131 @Override
132 public SortedMap<K, V> headMap(final K toKey) {
133 return Collections.unmodifiableSortedMap(delegate.headMap(toKey));
134 }
135
136 @Override
137 public boolean isEmpty() {
138 return delegate.isEmpty();
139 }
140
141 @Override
142 public Set<K> keySet() {
143 return Collections.unmodifiableSet(delegate.keySet());
144 }
145
146 @Override
147 public K lastKey() {
148 return delegate.lastKey();
149 }
150
151 @Override
152 public OrderedMapIterator<K, V> mapIterator() {
153 final OrderedMapIterator<K, V> it = delegate.mapIterator();
154 return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
155 }
156
157 @Override
158 public K nextKey(final K key) {
159 return delegate.nextKey(key);
160 }
161
162 @Override
163 public SortedMap<K, V> prefixMap(final K key) {
164 return Collections.unmodifiableSortedMap(delegate.prefixMap(key));
165 }
166
167 @Override
168 public K previousKey(final K key) {
169 return delegate.previousKey(key);
170 }
171
172
173
174
175
176
177
178
179 @Override
180 public V put(final K key, final V value) {
181 throw new UnsupportedOperationException();
182 }
183
184
185
186
187
188
189
190 @Override
191 public void putAll(final Map<? extends K, ? extends V> m) {
192 throw new UnsupportedOperationException();
193 }
194
195
196
197
198
199
200
201 @Override
202 public V remove(final Object key) {
203 throw new UnsupportedOperationException();
204 }
205
206 @Override
207 public int size() {
208 return delegate.size();
209 }
210
211 @Override
212 public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
213 return Collections.unmodifiableSortedMap(delegate.subMap(fromKey, toKey));
214 }
215
216 @Override
217 public SortedMap<K, V> tailMap(final K fromKey) {
218 return Collections.unmodifiableSortedMap(delegate.tailMap(fromKey));
219 }
220
221 @Override
222 public String toString() {
223 return delegate.toString();
224 }
225
226 @Override
227 public Collection<V> values() {
228 return Collections.unmodifiableCollection(delegate.values());
229 }
230
231 }