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