2

Possible Duplicate:
How to get the first non-null value in Java?

I have the following operation

function1(function2(param1,param2));

if function2 returns null I would like it to be replaced with a zero. I could do this a number of ways but is there some shortcut notation for this? For example, is there anything along the lines of this

function1(function2(param1,param2)==null || "0")

Note: function 2 returns a string.

Community
  • 1
  • 1
Usman Mutawakil
  • 4,993
  • 9
  • 43
  • 80
  • By "shortcut," do you mean that you don't want to call function2 twice and you also don't want to use an intermediate variable? If so, you are out of luck. – James McLeod Sep 26 '12 at 21:01
  • What do you want the type of the expression to be? (i.e., the value to be passed into function1). 0 is an int, which is a primitive type (and cannot be null), but function2() apparently returns a reference. – Mike Harris Sep 26 '12 at 21:02
  • @JamesMcLeod By shortcut I mean that I dont want to assign function2 to a variable and then pass it to an "if" statement before passing it to function1 – Usman Mutawakil Sep 26 '12 at 21:04
  • @MikeHarris Good point. Sorry. function2 returns a string. I should have wrote "0" – Usman Mutawakil Sep 26 '12 at 21:07
  • Sorry guys I had function1 written twice. I just removed it. – Usman Mutawakil Sep 26 '12 at 21:09

4 Answers4

4

Before switching to Guava, I found myself writing the function below for every project worked on. It's probably already been implemented in a library, somewhere.

<T> public static T coalesce(T... elements){
    for(T element : elements) {
        if (element != null) return element;
    }
    throw new NoSuchElementException();
}

// Usage:

function1(coalesce(function2(param1,param2), "default value"));

It's nice because there's no duplication of code, and you don't have to choose between introducing a temporary variable or performing the same call twice (like the conditional operator thingie previously suggested). It's not nice because, well, it doesn't read very fluently.

If you're using Guava (which you should be), you can use Optional to avoid null, which also has the benefit of making your API much clearer and less prone to NullPointerExceptions.

void function1(String arg) {}
Optional<String> function2(){ /* insert code*/ }

// Usage:
function1(function2().or("default value"));
gustafc
  • 28,465
  • 7
  • 73
  • 99
3

Sure, there is a ternary operator that does almost exactly that:

<expression> ? <value if expression is true> : <value if expression is false>

so,

String result = function2(param1,param2);
function1(result != null ? result : "0");

would do what you want.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • Doesn't this call function2 twice? – gtgaxiola Sep 26 '12 at 21:00
  • @gtgaxiola: Changed it to save the result first. – Keppil Sep 26 '12 at 21:02
  • I think saving the result contradicts the desire for brevity; i.e. it can't be done within an expression. – Mark Peters Sep 26 '12 at 21:03
  • @MarkPeters: I'm not entirely sure, but I believe that you could put it on one line, and the JVM would do this optimization for you if `function2` isn't too complicated. – Keppil Sep 26 '12 at 21:05
  • @Keppil: How do you mean "in one line"? With two explicit calls to `function2`? Obviously the general case can't be "optimized" to only make one call, because it might have side effects (although not if it actually is a pure "function"). Only calling it once might have completely different semantics. – Mark Peters Sep 26 '12 at 21:07
  • @MarkPeters: Of course it won't work for every situation, but the JVM does a lot of magic nowadays, so if there are no side effects I believe the JVM will often do this. The safer (and more readable) way will be to use 2 lines though. – Keppil Sep 26 '12 at 21:08
  • I'm not worried about the performance impact, which is all you'd be gaining by only calling it once. I'm just calling you out on saying that the ternary operator "does exactly that" when it clearly doesn't do the same thing that the asker wants. – Mark Peters Sep 26 '12 at 21:11
1

how about

function1(function2(param1,param2) != null? function2(param1, param2): 0);

btw, you can only use this if function2 doesn't make any permanent changes to any data because it's called twice and if it does, then you might get a different answer the second time.

Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
0

Use expression condition ? with assignment

Integer value = function1((value=function2(param1,param2)) != null? value: 0);
Roman C
  • 49,761
  • 33
  • 66
  • 176