Writing a synchronized thread-safety wrapper for NavigableMap
- by polygenelubricants
java.util.Collections currently provide the following utility methods for creating synchronized wrapper for various collection interfaces:
synchronizedCollection(Collection<T> c)
synchronizedList(List<T> list)
synchronizedMap(Map<K,V> m)
synchronizedSet(Set<T> s)
synchronizedSortedMap(SortedMap<K,V> m)
synchronizedSortedSet(SortedSet<T> s)
Analogously, it also has 6 unmodifiedXXX overloads.
The glaring omission here are the utility methods for NavigableMap<K,V>. It's true that it extends SortedMap, but so does SortedSet extends Set, and Set extends Collection, and Collections have dedicated utility methods for SortedSet and Set. Presumably NavigableMap is a useful abstraction, or else it wouldn't have been there in the first place, and yet there are no utility methods for it.
So the questions are:
Is there a specific reason why Collections doesn't provide utility methods for NavigableMap?
How would you write your own synchronized wrapper for NavigableMap?
Glancing at the source code for OpenJDK version of Collections.java seems to suggest that this is just a "mechanical" process
Is it true that in general you can add synchronized thread-safetiness feature like this?
If it's such a mechanical process, can it be automated? (Eclipse plug-in, etc)
Is this code repetition necessary, or could it have been avoided by a different OOP design pattern?