Java Integer Create toInteger(boolean bool)

Here you can find the source of toInteger(boolean bool)

Description

Converts a primitive boolean to a primitive integer by assuming 0 is false and everything else (1) is true.

License

Apache License

Parameter

Parameter Description
bool a parameter

Return

0 if the parameter is false, 1 otherwise.

Declaration

public static int toInteger(boolean bool) 

Method Source Code

//package com.java2s;
/*//from w  w w . j a v  a2s  .  co  m
 * Copyright 2011-2018 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static final String YES = "yes";
    public static final String NO = "no";

    /**
     * Converts a primitive boolean to a primitive integer by assuming 0 is false and everything else (1) is true.
     * 
     * @param bool
     * @return 0 if the parameter is false, 1 otherwise.
     */
    public static int toInteger(boolean bool) {
        return bool ? 1 : 0;
    }

    /**
     * Converts a {@link Boolean} object to an {@link Integer} by assuming 0 is false and everything else (1) is true. If the parameter is
     * <code>null</code> then it returns <code>null</code>.
     * 
     * @param bool
     * @return <code>null</code> if the parameter is <code>null</code>, 0 if false, 1 if true.
     */
    public static Integer toInteger(Boolean bool) {
        if (null == bool) {
            return null;
        }
        return bool.booleanValue() ? Integer.valueOf(1) : Integer.valueOf(0);
    }

    /**
     * Converts a primitive integer to boolean by assuming 0 is false and everything else (1) is true.
     * 
     * @param number
     * @return <code>true</code> or <code>false</code> based on the input
     */
    public static boolean valueOf(int number) {
        return 0 == number ? false : true;
    }

    /**
     * Converts an {@link Integer} object to {@link Boolean} by assuming 0 is false and everything else (1) is true. If the parameter is
     * <code>null</code> then it returns <code>null</code>.
     * 
     * @param value
     * @return <code>null</code> if the parameter is <code>null</code>, <code>true</code> or <code>false</code> otherwise.
     */
    public static Boolean valueOf(Integer value) {
        if (null == value) {
            return null;
        }
        return 0 == value.intValue() ? Boolean.FALSE : Boolean.TRUE;
    }

    /**
     * Converts a {@link String} object to {@link Boolean} by assuming the following mappings:
     * <p>
     * '0' -> false, 'no' -> false, '1' -> true, 'yes' -> true
     * </p>
     * Equality is checked in a case insensitive manner. If the parameter is <code>null</code> or something else than ['0', '1', 'yes', 'no'] then it
     * returns <code>null</code>.
     * 
     * @param value
     * @return <code>null</code> if the parameter is <code>null</code> or the value is not in '0', '1', 'yes', 'no', <code>true</code> or
     *         <code>false</code> otherwise.
     */
    public static Boolean valueOf(String value) {

        if (null == value) {
            return null;
        }

        if ("1".equals(value) || YES.equalsIgnoreCase(value)) {
            return Boolean.TRUE;
        } else if ("0".equals(value) || NO.equalsIgnoreCase(value)) {
            return Boolean.FALSE;
        }

        return null;
    }
}

Related

  1. toIntDecoded(String s)
  2. toIntDefaultIfNull(Integer configured, int theDefault)
  3. toInteger(boolean b)
  4. toInteger(boolean bool)
  5. toInteger(boolean bool)
  6. toInteger(byte b1, byte b2, byte b3, byte b4)
  7. toInteger(byte[] b)
  8. toInteger(byte[] b)
  9. toInteger(byte[] bytes)