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
33
34
35
36
37
38
39
40 public class UnmodifiableTrie<K, V> implements Trie<K, V>, Serializable, Unmodifiable {
41
42
43 private static final long serialVersionUID = -7156426030315945159L;
44
45
46
47
48
49
50
51
52
53
54 public static <K, V> Trie<K, V> unmodifiableTrie(final Trie<K, ? extends V> trie) {
55 if (trie instanceof Unmodifiable) {
56 @SuppressWarnings("unchecked")
57 final Trie<K, V> tmpTrie = (Trie<K, V>) trie;
58 return tmpTrie;
59 }
60 return new UnmodifiableTrie<>(trie);
61 }
62
63
64
65
66 private final Trie<K, V> delegate;
67
68
69
70
71
72
73
74 public UnmodifiableTrie(final Trie<K, ? extends V> trie) {
75 @SuppressWarnings("unchecked")
76 final Trie<K, V> tmpTrie = (Trie<K, V>) Objects.requireNonNull(trie, "trie");
77 this.delegate = tmpTrie;
78 }
79
80 @Override
81 public void clear() {
82 throw new UnsupportedOperationException();
83 }
84
85 @Override
86 public Comparator<? super K> comparator() {
87 return delegate.comparator();
88 }
89
90 @Override
91 public boolean containsKey(final Object key) {
92 return delegate.containsKey(key);
93 }
94
95 @Override
96 public boolean containsValue(final Object value) {
97 return delegate.containsValue(value);
98 }
99
100 @Override
101 public Set<Entry<K, V>> entrySet() {
102 return Collections.unmodifiableSet(delegate.entrySet());
103 }
104
105 @Override
106 public boolean equals(final Object obj) {
107 return delegate.equals(obj);
108 }
109
110 @Override
111 public K firstKey() {
112 return delegate.firstKey();
113 }
114
115 @Override
116 public V get(final Object key) {
117 return delegate.get(key);
118 }
119
120 @Override
121 public int hashCode() {
122 return delegate.hashCode();
123 }
124
125 @Override
126 public SortedMap<K, V> headMap(final K toKey) {
127 return Collections.unmodifiableSortedMap(delegate.headMap(toKey));
128 }
129
130 @Override
131 public boolean isEmpty() {
132 return delegate.isEmpty();
133 }
134
135 @Override
136 public Set<K> keySet() {
137 return Collections.unmodifiableSet(delegate.keySet());
138 }
139
140 @Override
141 public K lastKey() {
142 return delegate.lastKey();
143 }
144
145 @Override
146 public OrderedMapIterator<K, V> mapIterator() {
147 final OrderedMapIterator<K, V> it = delegate.mapIterator();
148 return UnmodifiableOrderedMapIterator.unmodifiableOrderedMapIterator(it);
149 }
150
151 @Override
152 public K nextKey(final K key) {
153 return delegate.nextKey(key);
154 }
155
156 @Override
157 public SortedMap<K, V> prefixMap(final K key) {
158 return Collections.unmodifiableSortedMap(delegate.prefixMap(key));
159 }
160
161 @Override
162 public K previousKey(final K key) {
163 return delegate.previousKey(key);
164 }
165
166 @Override
167 public V put(final K key, final V value) {
168 throw new UnsupportedOperationException();
169 }
170
171 @Override
172 public void putAll(final Map<? extends K, ? extends V> m) {
173 throw new UnsupportedOperationException();
174 }
175
176 @Override
177 public V remove(final Object key) {
178 throw new UnsupportedOperationException();
179 }
180
181 @Override
182 public int size() {
183 return delegate.size();
184 }
185
186 @Override
187 public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
188 return Collections.unmodifiableSortedMap(delegate.subMap(fromKey, toKey));
189 }
190
191 @Override
192 public SortedMap<K, V> tailMap(final K fromKey) {
193 return Collections.unmodifiableSortedMap(delegate.tailMap(fromKey));
194 }
195
196 @Override
197 public String toString() {
198 return delegate.toString();
199 }
200
201 @Override
202 public Collection<V> values() {
203 return Collections.unmodifiableCollection(delegate.values());
204 }
205
206 }