Java Zero Format zeroIfNullStrict(Integer i)

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

Description

converts an Integer to an int, mapping null to 0.

License

Open Source License

Parameter

Parameter Description
i a parameter

Exception

Parameter Description
IllegalArgumentException if <code>i</code> is <code>Integer(0)</code>.

Return

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

Declaration

public static int zeroIfNullStrict(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 {
    /**/*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;
    }
}

Related

  1. zeroFillString(String originalString, int numZeros)
  2. zeroFormat(Integer source)
  3. zeroFormattedStr(int number, int length)
  4. zeroIfNull(Integer i)
  5. zeroIfNull(Integer i)
  6. zeroInterval(byte[] x, int start, int end)
  7. zeroLowerBits(long bits, int nBits)
  8. zeroMatrix(int M, int N, double mat[][])
  9. zeroObjects()