Assign if not null or if not set in Java
<p>I am really tired of doing all the <em>if null</em> checks, resp. I also want to have a more configurable scenario for this. Let me explain this by an example:</p>

<p>I have a <code>getter()</code> which may return <code>null</code> or <code>'0'</code> in both cases the resp. <code>setter()</code> should not be called passing the <code>getter()</code>.</p>

<p>So the implementation is</p>

<pre><code>if(getter() != null &amp;&amp; !getter().equals('0')) setter(getter()); 
</code></pre>

<p>this however really anoys me, especially if <code>getter()</code> and <code>setter()</code> are really long method calls and I have to introduce helper variables for this.</p>

<p>I am thinking about a method with parameter</p>

<pre><code>ifNotSet(getter(), setter(), new Object[null, '0']); 
</code></pre>

<p>which does exactly the same thing. Where the parameters to ifNotSet are</p>

<ul>
<li><strong>getter</strong> - the method to check if it does not equal one of the  <strong>conditions</strong></li>
<li><strong>setter</strong> - the method to call in the way <code>setter(getter)</code> if <strong>conditions</strong> does not apply</li>
<li><strong>conditions</strong> - the conditions which must not apply on evaluation of <code>getter()</code> for the method to be executed</li>
</ul>

<p>At first sight this does not seem to complicated, it however is! Is anyone aware of a solution to this problem or any kind of implementation?</p>

<p>Thanks!</p>

<p><strong>Update</strong></p>

<p>I've been working some more on the problem, after the feedback of you guys, and found out about the following</p>

<pre><code>    private boolean ns(Object[] condition, Object getter) {
    boolean ret = false;
    for (Object object : condition) {
        if(getter) equals or == ??
    }
    return true;
}

    Object[] cond = new Object[] { null, "0" };
    Object a;
if (ns(cond, a = getter()))setter(a);
</code></pre>

<p>Well, this seemed to be at least a solution if you have a lot of allocations to do. However, if you take a look at the <code>ns()</code> method... the question on the incoming conditions is, whether to use <code>==</code> or <code>equals</code> to do the comparison!?</p>
