I'd like to add the following methods to StringUtils:

String defaultString(Object obj) - behaves the same as
defaultString(String), invoking toString() on the obj to return the
string, or blank if obj is null
String defaultString(Object obj, String defaultString) - same as
defaultString(String, String), invoking toString() on the obj to return
the string, or the defaultString value if obj is null

A specific use case: I do lots of debug logging, many times logging
results from various method calls, some of which may not be null-safe.
In cases where a method return could be null, I need to check the return
for null and replace what would be logged with some placeholder string
(i.e., "(null)". So the following code would be replace:

debugLog.debug( "MyClass.myMethod() = " + ((MyClass.myMethod() == null)
? "(null)" : MyClass.myMethod().toString() + ")" );

with

debugLog.info( "MyClass.myMethod() = " +
StringUtils.defaultString(MyClass.myMethod(), "(null"));

I could use StringUtils.defaultString now if the result were a string,
but for non-String objects where is no equivalent. I don't want to use
StringUtils.defaultString(MyClass.myMethod().toString(), "(null)")
because I'll get an NPE on the .toString() and defaultString will never
have a chance.

Would this hold up the imminent release? If so I'll defer to afterward.

Steven Caswell
[EMAIL PROTECTED]




--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to