vigna / fastutil

fastutil extends the Java™ Collections Framework by providing type-specific maps, sets, lists and queues.
Apache License 2.0
1.76k stars 196 forks source link

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

Open vigna opened 3 years ago

vigna commented 3 years ago

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

techsy730 commented 3 years ago

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

vigna commented 3 years ago

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.

techsy730 commented 3 years ago

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.