Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[dali-dev] Re: IPersitentType.attributes() - wildcard parameterized type?


Hi Karen,

Okay, I see the reasoning behind the decision now. Seems like Generics sometimes complicate scenarios rather than make life easier :)

In looking at the code, it seems that one possible solution might be to instead make the CloneIterator utilize the wildcard, rather than the IPersistentType interface. So the CloneIterator constructor would look like:

public CloneIterator(Collection<? extends E> c) {
   this(c, Mutator.ReadOnly.<E>instance());
}

This then allows you to do the behavior you need of using a CloneIterator on subclassed types: Collection<JavaPersistentAttribute> atts = new ArrayList <JavaPersistentAttribute>(); CloneIterator<IPersistentAttribute> ci = new CloneIterator <IPersistentAttribute>(atts);

So the attributes() method specified by the interface could still be left pure:
public Iterator<IPersistentAttribute> attributes() {
   return new CloneIterator<IPersistentAttribute>(getAttributes());
}

And you could have another non-interface method on JavaPersistentType/XmlPersistentType for the internal classes accessing it directly (versus via the IPersistentType interface) that returns an Iterator of type-specific attributes:

public Iterator<JavaPersistentAttribute> javaAttributes() {
   return new CloneIterator<JavaPersistentAttribute>(getAttributes());
}

Then clients could use the clean API, and your internal Java-specific classes could use the specific javaAttributes().

I'm not a Generics expert either, so keep that in mind :) This was just one idea that seemed like it could work.

Thanks
Tom

Karen Moore wrote:

Hi Tom,

Funny you should ask, I just struggled with this very recently. In our model JavaPersistentType and XmlPersistentType both implement IPersistentType. They each have collections of more specific attributes JavaPersistentAttribute and XmlPersistentAttribute. The reason I changed the interface to have a wildcard-parameterized type was so that I could use a CloneIterator (our own utility class) in JavaPersistentType.attributes() to prevent concurrent modification problems we were having. I did not change allAttributes() because those iterators could contain xml and java PersistentAttributes due to inheritance hierarchies that could possibly span both java and orm.xml. I don't know that it is possible to use the wildcard-parameterized type in those situations. I agree this solution is not the best, we have compiler warnings in JavaPersistentType that we haven't yet fixed. Given that background and the fact that I am fairly new to generics, do you have any suggestions?

The method you are writing looks very similar to some of our interface methods. You might want to take a look at IEntity.primaryKeyColumnName() and IEntity.primaryKeyAttributeName().
thanks,
Karen



Back to the top