[jdom-interest] Another suggestion for the Element class
Patrick Dowler
Patrick.Dowler at nrc.ca
Wed Nov 15 11:07:03 PST 2000
On Wed, 15 Nov 2000, Elliotte Rusty Harold wrote:
> At 2:02 AM -0800 11/14/00, Jason Hunter wrote:
> >> Having to use String.valueOf repeatedly can be tedious (although it is
> >> certainly workable). My recommendation would be to add some
> >> convenience
> >> methods to handle conversion of primitive types into Strings:
> >>
> >> public Element setText(int iTextValue);
> >> public Element setText(long lTextValue);
> >> ...
> >>
> >> The method bodies would follow this format:
> >> public Element setText(int iTextValue) {
> >> return setText(String.valueOf(iTextValue));
> >> }
> >>
> >> This would allow quick conversion of primitive types without requiring
> >> the programmer to do the String conversions manually every time.
> >> Comments?
>
> I would prefer not to clutter the API with these extra methods.
> Smaller APIs are more beautiful, IMHO. As you point out, doing
> without them is trivial. If you really find you need them, then it's
> straight-forward to write a subclass of Element that provides them
> that you can use in your own code and the rest of us can be
> blissfully ignorant of.
There are several options:
The static method in a utility class:
JDOMUtil.addAttribute( e, "hi", i);
which is slightly worse than overloading:
e.addAttribute( "hi", i );
and arguably no better than the direct:
e.addAttribute( "hi", Integer.toString(i) );
or the direct but lazy:
e.addAttribute( "hi", ""+i ); // only marginally unsightly :-)
It certainly doesn't warrant making the API larger.
--
Patrick Dowler
Canadian Astronomy Data Centre
More information about the jdom-interest
mailing list