[jdom-interest] attr.getXXXValue

Patrick Dowler Patrick.Dowler at nrc.ca
Wed Jul 19 15:13:33 PDT 2000


On Wed, 19 Jul 2000, Joseph Bowbeer wrote:
> I like both the getXXXValue() and the getXXXValue(default) methods
> of Attribute, provided that the 'default' version throws a
> conversion exception if the attribute string is in an invalid
> format.  (But what if the attribute string is ""?)

In the spirit of keeping the API simple, how is

	int i = attr.getIntContent();

any better than

	int i = Integer.parseInt( attr.getContent() );

The latter can throw a NumberFormatException. The former must 
do the same anyway since there is no return value that can signal an error.

> Since the decision of whether to represent something as content or
> as an attribute is often a matter of taste (i.e., arbitrary), I
> think it would also be convenient to provide getXXXContent methods
> for Element.  And getXXXContent(default) methods naturally
> follow...  But what rules would we use regarding the whitespace in
> the content (if any).

IMO, "default" vales tend to hide the syntax error... the implementation would
have to catch the exception and return the default value. It would be simple
to just use a utility method everywhere if that is actually what you want:

public int defaultInsteadOfException( String s, int default )
{
	try 
	{
		return Integer.parseInt(s);
	}
	catch (NumberFormatException ex) { }
	return default;
}

:-)

--

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list