Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » hashcode and Map (non mutable case)
hashcode and Map (non mutable case) [message #942521] Sat, 13 October 2012 12:01 Go to previous message
David Michonneau is currently offline David Michonneau
Messages: 81
Registered: July 2009
Member
I'm coming back with yet another question about using EObject as keys in Maps. I have read previous posts, javadoc, etc... So I know it is not supported to override hashcode()/equals(). But, this remains a major annoyance, and I agree that it is a bad idea to use mutable objects as keys in HashMaps.

The point is that I use EMF to generate all my model classes, mutable and non-mutable ones. It doesn't seem there is any good reason to prevent the mutable ones to be keys in maps (and especially EMF generated maps, after all those are EMF specialized map classes).

So here is my question: I model a non-mutable class, let's call it Identifier, with two attributes:
- String name
- String path

The two attributes must be set at the construction of Identifier, so I only create instances through a new factory method createIdentifier(name, path). Nobody ever calls setter (or assume I remove setter methods).

Now, given those hypotheses, can I override hashcode/equals to put this Identifier object into an EMap (modeled in the ecore)? Will it cause any problem? If so, can we envision an enhancement to model special immutable objects that will have a generated hashcode/equals (by default hashcode of attributes), that could be overriden? I guess all EMF code (lists, copier,...) would be able to detect those immutable objects, and process them accordingly. Moreover, it would generate smart factory methods for those with all its attributes as arguments.

Thanks,

David
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic:How to populate a Map of List ?
Next Topic:[Teneo] OneToOne turns into ManyToOne
Goto Forum:
  


Current Time: Wed May 22 18:20:50 EDT 2013

Powered by FUDForum. Page generated in 0.01932 seconds