Here you can find the source of zeroIfNull(Integer i)
Integer
to an int
, mapping null
to 0
and mapping Integer(0)
to 0
also.
Parameter | Description |
---|---|
i | a parameter |
intValue()
of i
, if i != null
. 0
otherwise.
public static int zeroIfNull(Integer i)
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by public class Main { /**/*from w w w . j a v a 2 s . c o m*/ * converts an <code>Integer</code> to an <code>int</code>, mapping <code>null</code> to <code>0</code> and * mapping <code>Integer(0)</code> to <code>0</code> also. * Note that this method/function is not bijective, that is, there is no inverse function. Thus, in most cases, * you may want to use the strict version <code>nullToZeroStrict</code> instead. * @param i * @return the <code>intValue()</code> of <code>i</code>, if <code>i != null</code>. <code>0</code> otherwise. * @postcondition (i == null) --> (result == 0) * @postcondition (i != null) --> (result == i) */ public static int zeroIfNull(Integer i) { final int result = (i == null) ? 0 : i; // Note that "implies" doesn't work here: assert !(i == null) || (result == 0); assert !(i != null) || (result == i); return result; } }