1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.Collection;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.SortedMap;
27
28 import org.apache.commons.collections4.BoundedMap;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.collection.UnmodifiableCollection;
31 import org.apache.commons.collections4.set.UnmodifiableSet;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class FixedSizeSortedMap<K, V>
63 extends AbstractSortedMapDecorator<K, V>
64 implements BoundedMap<K, V>, Serializable {
65
66
67 private static final long serialVersionUID = 3126019624511683653L;
68
69
70
71
72
73
74
75
76
77
78
79 public static <K, V> FixedSizeSortedMap<K, V> fixedSizeSortedMap(final SortedMap<K, V> map) {
80 return new FixedSizeSortedMap<>(map);
81 }
82
83
84
85
86
87
88
89 protected FixedSizeSortedMap(final SortedMap<K, V> map) {
90 super(map);
91 }
92
93 @Override
94 public void clear() {
95 throw new UnsupportedOperationException("Map is fixed size");
96 }
97
98 @Override
99 public Set<Map.Entry<K, V>> entrySet() {
100 return UnmodifiableSet.unmodifiableSet(map.entrySet());
101 }
102
103
104
105
106
107
108 protected SortedMap<K, V> getSortedMap() {
109 return (SortedMap<K, V>) map;
110 }
111
112 @Override
113 public SortedMap<K, V> headMap(final K toKey) {
114 return new FixedSizeSortedMap<>(getSortedMap().headMap(toKey));
115 }
116
117 @Override
118 public boolean isFull() {
119 return true;
120 }
121
122 @Override
123 public Set<K> keySet() {
124 return UnmodifiableSet.unmodifiableSet(map.keySet());
125 }
126
127 @Override
128 public int maxSize() {
129 return size();
130 }
131
132 @Override
133 public V put(final K key, final V value) {
134 if (!map.containsKey(key)) {
135 throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
136 }
137 return map.put(key, value);
138 }
139
140 @Override
141 public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
142 if (CollectionUtils.isSubCollection(mapToCopy.keySet(), keySet())) {
143 throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
144 }
145 map.putAll(mapToCopy);
146 }
147
148
149
150
151
152
153
154
155 @SuppressWarnings("unchecked")
156 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
157 in.defaultReadObject();
158 map = (Map<K, V>) in.readObject();
159 }
160
161 @Override
162 public V remove(final Object key) {
163 throw new UnsupportedOperationException("Map is fixed size");
164 }
165
166 @Override
167 public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
168 return new FixedSizeSortedMap<>(getSortedMap().subMap(fromKey, toKey));
169 }
170
171 @Override
172 public SortedMap<K, V> tailMap(final K fromKey) {
173 return new FixedSizeSortedMap<>(getSortedMap().tailMap(fromKey));
174 }
175
176 @Override
177 public Collection<V> values() {
178 return UnmodifiableCollection.unmodifiableCollection(map.values());
179 }
180
181
182
183
184
185
186
187 private void writeObject(final ObjectOutputStream out) throws IOException {
188 out.defaultWriteObject();
189 out.writeObject(map);
190 }
191
192 }