fastutil
fastutil copied to clipboard
Unmodifiable map wrappers to not shield from Entry.setValue()
The current way entrySet() is handled in unmodifiable wrapper is wrong—users can still modify the map using Entry.setValue(). An entirely custom unmodifiable wrapper is necessary (as the JDK does).
Why doesn't giving the raw entry/keySet to unmodifiableSet and then returning that work? Same for values and unmodifiableCollection.
Because making an entry set unmodifiable simply means that you cannot add or remove entries. It does not prevent you from reading an entry and invoking setValue(), thus modifying the underlying collection. Key sets and value collections do not have this problem.
Ah, I see.
I think It can be done without defining a new composition class from scratch. Just make a (package private) variantion of unmodifiableSet
that will also wrap the Entry
values. (Best approach would probably be a subclass of UnmodifiableSet
, and override any ways of getting values to instead return the wrapper Entry
s)
Would require some care, but we won't have to implement a whole wrapper from scratch.