inline - Java - Check Not Null/Empty else assign default value -
i trying simplify following code.
the basic steps code should carry out follows:
- assign string default value
- run method
- if method returns null/empty string leave string default
- if method returns valid string set string result
a simple example be:
string temp = system.getproperty("xyz"); string result = "default"; if(temp != null && !temp.isempty()){ result = temp; }
i have made attemp using ternary operator:
string temp; string result = isnotnullorempty(temp = system.getproperty("xyz")) ? temp : "default";
the isnotnullorempty() method
private static boolean isnotnullorempty(string str){ return (str != null && !str.isempty()); }
is possible of in-line? know this:
string result = isnotnullorempty(system.getproperty("xyz")) ? system.getproperty("xyz") : "default";
but calling same method twice. something (which doesn't work):
string result = isnotnullorempty(string temp = system.getproperty("xyz")) ? temp : "default";
i initialize 'temp' string within same line. possible? or should doing?
thank suggestions.
tim
i know question old, generics 1 can add more generalized method work types.
public <t> t getvalueordefault(t value, t defaultvalue) { return value == null ? defaultvalue : value; }
Comments
Post a Comment