inline - Java - Check Not Null/Empty else assign default value -


i trying simplify following code.

the basic steps code should carry out follows:

  1. assign string default value
  2. run method
  3. if method returns null/empty string leave string default
  4. 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

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -