[jdom-interest] Element Reference from Attribute

James Strachan james at metastuff.com
Mon Nov 20 04:01:31 PST 2000

----- Original Message -----
From: "Jason Hunter" <jhunter at acm.org>
> I've been thinking about this, and it raises an interesting issue.  What
> should the process be to move attributes from one element to another?
> Right now one could have the same Attribute object added to two
> different elements, because there's no way for the API to know if an
> Attribute is "owned" already or not.
> The problem with this is that setting the value on the new attribute
> would also set it on the old attribute at the same time, since they're
> the same object.
> Solutions are to:
> 1) Leave it alone with this behavior.
> 2) Clone any attribute added to an Element.  (No good, because
> addAttribute(new Attribute(...)) would do the clone unnecessarily.)
> 3) Make Attribute immutable.  Remove all set methods.  Then the
> Attribute may be added twice, but no one will care.

I do like immutable objects when they are applicable for 'value' objects.
String / Integer / Number / Date et al. They can be easily reused everywhere
without any worry of the value being changed under the covers. They can
easily be 'interned' too to share 'value singletons' to lower memory usage.

However if an attribute needs to store its parent as instance data then it
doesn't make sense to share an attribute instance, so making them immutable
doesn't have much gain.

> 4) Make Attribute have a parent and use the parent variable to enforce
> the same rules for moving attribs as we have for Element.

Treating Attributes similar to Elements with respect to parentage does
appear to be the 'right thing to do'. The extra memory overhead may be an
issue for some people though - maybe we need a derived Attribute
implementation that handles parenting? e.g. OwnedAttribute?

> The same issues exist for Comment, PI, and perhaps others.  Thoughts?

Again, the extra memory overhead may be an issue. Should it be optional?


James Strachan
email: james at metastuff.com
web: http://www.metastuff.com

If you are not the addressee of this confidential e-mail and any
attachments, please delete it and inform the sender; unauthorised
redistribution or publication is prohibited. Views expressed are those of
the author and do not necessarily represent those of Citria Limited.

More information about the jdom-interest mailing list