Java Zero Format zeroIfNull(Integer i)

Here you can find the source of zeroIfNull(Integer i)

Description

converts an Integer to an int, mapping null to 0 and mapping Integer(0) to 0 also.

License

Open Source License

Parameter

Parameter Description
i a parameter

Return

the intValue() of i, if i != null. 0 otherwise.

Declaration

public static int zeroIfNull(Integer i) 

Method Source Code

//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;
    }
}

Related

  1. zeroFill(int value, int fieldWidth)
  2. zerofill(int x, int desiredWidth)
  3. zeroFillString(String originalString, int numZeros)
  4. zeroFormat(Integer source)
  5. zeroFormattedStr(int number, int length)
  6. zeroIfNull(Integer i)
  7. zeroIfNullStrict(Integer i)
  8. zeroInterval(byte[] x, int start, int end)
  9. zeroLowerBits(long bits, int nBits)