The best way to test if a returned string is null in Java
I have a function that concatenate a set of strings like this:
StringBuffer sb = new StringBuffer();
sb.append(fct1());
sb.append(fct2());
sb.append(fct3());
Where fct1(), fct2() and fct3() should return a String. The problem is that I must test the returned 开发者_开发百科values like this :
sb.append(fct1() == null ? "" : fct1());
because I get an exception if the value is null.
The problem is that I have many instructions like this and, above all, I can't modify these functions that return the strings(fct1, fct2 and fct3).
Is there a solution that will "sanitize" automatically my strings?
Thank you.
PS: I created a function that can do it:
public String testNullity(String aString){
aString == null ? "" : aString;
}
so that I can call it like this:
sb.append(testNullity(fct1));
sb.append(testNullity(fct2));
...
Another alternative might be
public class SafeStringBuilder {
private StringBuilder builder = new StringBuilder();
public SafeStringBuilder append(String s) {
if (s != null)
builder.append(s);
return this;
}
}
If you don't mind introducing a dependency, use Guava's Joiner
instead of StringBuffer
:
Joiner j = Joiner.on("").skipNulls();
StringBuilder sb = new StringBuilder();
j.appendTo(sb, fct1());
j.appendTo(sb, fct2());
j.appendTo(sb, fct3());
String result = sb.toString();
// or even just
Joiner j = Joiner.on("").skipNulls();
String result = j.join(fct1(), fct2(), fct3());
N.B. In general, unless you need StringBuffer
's thread safety, you should use StringBuilder
instead. Same API, better performance.
There is nothing whatsoever you can do to make this solution simpler except shortening the method name. (There might be a solution using aspect-oriented programming, but on the whole I don't really consider that simpler.)
Unfortunately your solution with testNullity()
is the best you can get with Java (consider better naming though). In fact, there is already a method that does that: StringUtils.html#defaultString.
You can create your own wrapper around StringBuffer
:
class MyStringBuffer {
StringBuffer _sb = new StringBuffer();
public boolean append(String s) {
_sb.append(s==null ? "" : s);
return s == null;
}
public String toString() { return _sb.toString(); }
}
There is no such function in the standard API, though some methods (which do other things) have it built in.
For example, System.getProperty() has a variant which takes a default value, and if it can't find the given property, it will not return null
, but the given default value. You might think of providing your fct*
methods with such a "default" argument, if it makes sence.
I think C# has a ??
operator which does about what you want (you would call sb.append(fct2() ?? "")
), but I suppose Java will not add any new operators soon.
A better variant of your checking function would be this:
public void appendIfNotNull(StringBuffer sb, String s) {
if(s != null) {
sb.append(s);
}
}
This avoids the superfluous append call with an empty string if there is nothing to append.
精彩评论