Daniel Rall wrote:
> 
> I've noticed that it's often the case that getString(String key) gets
> called on a key whose associated value is a list (usually due to a
> misconfigured properties file).  I was thinking that in such a case
> behavior other than a ClassCastException may be desirable.  In ideal
> code, such a situation might be caught and handled gracefully.
> Unfortunately, ClassCastException is one of those Exceptions that can
> be thrown almost anywhere, so it often is dealt with as well as it
> could be.
> 
> Two reasonable behaviors that come to mind include either returning
> the first item in the list, or returning the entire list as it was
> originally specified in the properties file.  I've implemented the
> second possiblity, but want feedback on this change from others.
> 

This is an issue that also bugged me, but I never could decide on what
made sense.  I think its great that you are going after this, but I (at
this moment) disagree with you and Ilkka that it should return all the
data as a comma delineated string.  I say 'at this moment' because I
keep going back and forth on this one, but forced to a decision, I am
starting to believe the following :

I agree that implicitly forcing a ClassCastException is too surprising a
result.  It certainly was for me when it happened.

However, returning a comma-separated list of values is somewhat useless
if you have comma's in the data, which is legit :

property.foobar = jim\,bob, foo\,bar

should be an array consisting of jim,bob  and foo,bar

but you would now return

jim,bob,foo,bar

so while I agree with Ilkka's contention that the API has no right to
filter out information, it has no right to alter it either.

So what do we do?

I would argue that an application knows if something is multivalued. 
For example, the FileResourceLoader knows that the path is a
multi-valued property, so it asks for the Vector via getVector().  If
there is a single value, so what - it iterates over a list of 1 element.

In the case where the application is expecting a scalar, and the
Configuration data is multi-valued such that it would return a Vector, I
think returning *only* the first value is the right way to go.

Because if it now has the potential to return a CSV string, then the
application has to prepare for this by doing the same analyis and
processing on the CVS string that Configuration did in the first place -
it might as well just call getVector()

So, if it returns a CSV String, it effectively removes the benefit of
Configuration, and all apps will just call getVector() to avoid doing
the work.

So I vote for returning just the first element.

There is precedent for this : look at javax.servlet.ServletRequest  ->
there are two parameter getting methods :

String getParameter( String )

String[] getParameterValues( String )

and getParameter() will return the first on the list for a multi-valued
parameter....

geir

 
-- 
Geir Magnusson Jr.                           [EMAIL PROTECTED]
System and Software Consulting

Developing for the web?  See http://jakarta.apache.org/velocity/

Reply via email to