fastutil icon indicating copy to clipboard operation
fastutil copied to clipboard

Unmodifiable map wrappers to not shield from Entry.setValue()

Open vigna opened this issue 3 years ago • 3 comments

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).

vigna avatar Mar 18 '21 10:03 vigna

Why doesn't giving the raw entry/keySet to unmodifiableSet and then returning that work? Same for values and unmodifiableCollection.

techsy730 avatar Apr 23 '21 19:04 techsy730

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.

vigna avatar Apr 24 '21 07:04 vigna

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 Entrys)

Would require some care, but we won't have to implement a whole wrapper from scratch.

techsy730 avatar Apr 28 '21 16:04 techsy730