Skip to content

Commit b041c39

Browse files
Dmytro Ukhlovdukhlov
authored andcommitted
Refactor BuildReferenceMapAdapter
1 parent f836cdc commit b041c39

File tree

5 files changed

+351
-582
lines changed

5 files changed

+351
-582
lines changed

core/src/main/java/hudson/util/CopyOnWriteMap.java

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.LinkedHashMap;
3939
import java.util.Map;
4040
import java.util.NavigableMap;
41+
import java.util.NavigableSet;
4142
import java.util.Set;
4243
import java.util.SortedMap;
4344
import java.util.TreeMap;
@@ -211,34 +212,38 @@ public boolean canConvert(Class type) {
211212

212213
@Override
213214
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
214-
return new Hash((Map) super.unmarshal(reader, context));
215+
return new Hash<>((Map<?, ?>) super.unmarshal(reader, context));
215216
}
216217

217218
@Override
218219
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
219-
super.marshal(((Hash) source).core, writer, context);
220+
super.marshal(((Hash<?, ?>) source).core, writer, context);
220221
}
221222
}
222223
}
223224

224225
/**
225226
* {@link CopyOnWriteMap} backed by {@link TreeMap}.
226227
*/
227-
public static final class Tree<K, V> extends CopyOnWriteMap<K, V> implements SortedMap<K, V> {
228-
private final Comparator<K> comparator;
228+
public static final class Tree<K, V> extends CopyOnWriteMap<K, V> implements NavigableMap<K, V> {
229+
private final Comparator<? super K> comparator;
229230

230-
public Tree(Map<K, V> core, Comparator<K> comparator) {
231+
public Tree(Map<K, V> core, Comparator<? super K> comparator) {
231232
this(comparator);
232233
putAll(core);
233234
}
234235

235-
public Tree(Comparator<K> comparator) {
236+
public Tree(NavigableMap<K, V> m) {
237+
this(m, m.comparator());
238+
}
239+
240+
public Tree(Comparator<? super K> comparator) {
236241
super(new TreeMap<>(comparator));
237242
this.comparator = comparator;
238243
}
239244

240245
public Tree() {
241-
this(null);
246+
this(Collections.emptyNavigableMap());
242247
}
243248

244249
@Override
@@ -263,10 +268,96 @@ public NavigableMap<K, V> getView() {
263268
return (NavigableMap<K, V>) super.getView();
264269
}
265270

271+
@Override
272+
public Entry<K, V> lowerEntry(K key) {
273+
return getView().lowerEntry(key);
274+
}
275+
276+
@Override
277+
public K lowerKey(K key) {
278+
return getView().lowerKey(key);
279+
}
280+
281+
@Override
282+
public Entry<K, V> floorEntry(K key) {
283+
return getView().floorEntry(key);
284+
}
285+
286+
@Override
287+
public K floorKey(K key) {
288+
return getView().floorKey(key);
289+
}
290+
291+
@Override
292+
public Entry<K, V> ceilingEntry(K key) {
293+
return getView().ceilingEntry(key);
294+
}
295+
296+
@Override
297+
public K ceilingKey(K key) {
298+
return getView().ceilingKey(key);
299+
}
300+
301+
@Override
302+
public Entry<K, V> higherEntry(K key) {
303+
return getView().higherEntry(key);
304+
}
305+
306+
@Override
307+
public K higherKey(K key) {
308+
return getView().higherKey(key);
309+
}
310+
311+
@Override
312+
public Entry<K, V> firstEntry() {
313+
return getView().firstEntry();
314+
}
315+
316+
@Override
317+
public Entry<K, V> lastEntry() {
318+
return getView().lastEntry();
319+
}
320+
321+
@Override
322+
public Entry<K, V> pollFirstEntry() {
323+
throw new UnsupportedOperationException();
324+
}
325+
326+
@Override
327+
public Entry<K, V> pollLastEntry() {
328+
throw new UnsupportedOperationException();
329+
}
330+
331+
@Override
266332
public NavigableMap<K, V> descendingMap() {
267333
return getView().descendingMap();
268334
}
269335

336+
@Override
337+
public NavigableSet<K> navigableKeySet() {
338+
return getView().navigableKeySet();
339+
}
340+
341+
@Override
342+
public NavigableSet<K> descendingKeySet() {
343+
return getView().descendingKeySet();
344+
}
345+
346+
@Override
347+
public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
348+
return getView().subMap(fromKey, fromInclusive, toKey, toInclusive);
349+
}
350+
351+
@Override
352+
public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
353+
return getView().headMap(toKey, inclusive);
354+
}
355+
356+
@Override
357+
public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
358+
return getView().tailMap(fromKey, inclusive);
359+
}
360+
270361
@Override
271362
public Comparator<? super K> comparator() {
272363
return getView().comparator();
@@ -309,13 +400,12 @@ public boolean canConvert(Class type) {
309400

310401
@Override
311402
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
312-
TreeMap tm = (TreeMap) super.unmarshal(reader, context);
313-
return new Tree(tm, tm.comparator());
403+
return new Tree<>((TreeMap<?, ?>) super.unmarshal(reader, context));
314404
}
315405

316406
@Override
317407
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
318-
super.marshal(((Tree) source).core, writer, context);
408+
super.marshal(((Tree<?, ?>) source).core, writer, context);
319409
}
320410
}
321411
}

0 commit comments

Comments
 (0)