Here you can find the source of zeroIfNullStrict(Integer i)
Integer
to an int
, mapping null
to 0
.
Parameter | Description |
---|---|
i | a parameter |
Parameter | Description |
---|---|
IllegalArgumentException | if <code>i</code> is <code>Integer(0)</code>. |
intValue()
of i
, if i != null
. 0
otherwise.
public static int zeroIfNullStrict(Integer i)
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by public class Main { /**/*ww w . j av a 2s . co m*/ * converts an <code>Integer</code> to an <code>int</code>, mapping <code>null</code> to <code>0</code>. * Integer(0) is not allowed for input. This strict version of "nullToZeroStrict" is the inverse of <code>zeroToNull</code>, * always ensuring that <code>LangUtil.equals(zeroToNull(nullToZeroStrict(i)), i)</code>. * @param i * @return the <code>intValue()</code> of <code>i</code>, if <code>i != null</code>. <code>0</code> otherwise. * @throws IllegalArgumentException if <code>i</code> is <code>Integer(0)</code>. * @postcondition (i == null) --> (result == 0) * @postcondition (i != null) && (i != 0) --> (result == i) */ public static int zeroIfNullStrict(Integer i) { final int result; if (i == null) { result = 0; } else { result = i; if (result == 0) { throw new IllegalArgumentException("langutils.integer.not.allowed.exception");//"Integer(0) ist nicht erlaubt."); } } // Note that "implies" doesn't work here: assert !(i == null) || (result == 0); assert !((i != null) && (i != 0)) || (result == i); return result; } }