Generics Cast Question / some guidance
Hi guys I have a question:
Using Generics. Is there a cost in casting an object of say class Integer to an object of class Integer (again)?
Or can the framework tell and not cast at all.
This is why I ask
I have some model classes (getter setters nothing else) in hierarchy.
SomeEntity extends BaseEntity. ok?
So... I have a store method that uses generics in this way
public <Eextends BaseEntity>void store(E entity);
if inside the code I'll have to put something like.
save((SomeEntity)BaseEntitty);
when the call represents for SomeEntitty types would I have cast call?
You see, I am trying to avoid Casting as much as I can.
I have to translate an entity to an xml node, then append the node to a parent node, thus persisting it.
I created some XmlTranslators. Each for each different type of entity.
I have a Switch statement that evaluates the entity.entityType (an enumerator type) and in each translation I would have to cast to the specific model although it is passed through the generics methods.
Am I doing this the right way? or just the complicated way. My goal is to reduce code in store methods and avoid as much casting as I can.
Another example, I guess a better one would be the getEntity.
public <Eextends Entity> E getEntity(int id){
returnnull;
}
Lets suppose I want a Person. I would type
Person p = storage.getEntity(101);
That thanks to generics.
Now inside the body I will have to put the necessary statements to get the Node from the XML DOM document corresponding to the 101 id. And then use a translator to get the data from the Node to a new and empty instance of Person.
This translators has the same syntax
publicinterface EntityTranslator <Eextends Entity, T>{
public T translateEntity(E entity);
public E translate(T object);
}
I think in trying to do all I got a little lost. If some one could share his/her views on the matter and maybe show me a better practice, I would appreciate it very much.
f(t)

