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
90 protected FixedSizeSortedMap(final SortedMap<K, V> map) {
91 super(map);
92 }
93
94
95
96
97
98
99 protected SortedMap<K, V> getSortedMap() {
100 return (SortedMap<K, V>) map;
101 }
102
103
104
105
106
107
108
109
110 private void writeObject(final ObjectOutputStream out) throws IOException {
111 out.defaultWriteObject();
112 out.writeObject(map);
113 }
114
115
116
117
118
119
120
121
122 @SuppressWarnings("unchecked")
123 private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
124 in.defaultReadObject();
125 map = (Map<K, V>) in.readObject();
126 }
127
128
129 @Override
130 public V put(final K key, final V value) {
131 if (map.containsKey(key) == false) {
132 throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
133 }
134 return map.put(key, value);
135 }
136
137 @Override
138 public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
139 if (CollectionUtils.isSubCollection(mapToCopy.keySet(), keySet())) {
140 throw new IllegalArgumentException("Cannot put new key/value pair - Map is fixed size");
141 }
142 map.putAll(mapToCopy);
143 }
144
145 @Override
146 public void clear() {
147 throw new UnsupportedOperationException("Map is fixed size");
148 }
149
150 @Override
151 public V remove(final Object key) {
152 throw new UnsupportedOperationException("Map is fixed size");
153 }
154
155 @Override
156 public Set<Map.Entry<K, V>> entrySet() {
157 return UnmodifiableSet.unmodifiableSet(map.entrySet());
158 }
159
160 @Override
161 public Set<K> keySet() {
162 return UnmodifiableSet.unmodifiableSet(map.keySet());
163 }
164
165 @Override
166 public Collection<V> values() {
167 return UnmodifiableCollection.unmodifiableCollection(map.values());
168 }
169
170
171 @Override
172 public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
173 return new FixedSizeSortedMap<>(getSortedMap().subMap(fromKey, toKey));
174 }
175
176 @Override
177 public SortedMap<K, V> headMap(final K toKey) {
178 return new FixedSizeSortedMap<>(getSortedMap().headMap(toKey));
179 }
180
181 @Override
182 public SortedMap<K, V> tailMap(final K fromKey) {
183 return new FixedSizeSortedMap<>(getSortedMap().tailMap(fromKey));
184 }
185
186 @Override
187 public boolean isFull() {
188 return true;
189 }
190
191 @Override
192 public int maxSize() {
193 return size();
194 }
195
196 }