Java Number Value Of valueOf(int number)

Here you can find the source of valueOf(int number)

Description

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

License

Apache License

Parameter

Parameter Description
number a parameter

Return

true or false based on the input

Declaration

public static boolean valueOf(int number) 

Method Source Code

//package com.java2s;
/*/*from w  w w . j  a va  2s. com*/
 * 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 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. valueOf(Class enumType, int ordinal)
  2. valueOf(final Boolean value)
  3. valueOf(float f)
  4. valueOf(int i)
  5. valueOf(int i)
  6. valueOf(int[] source)
  7. valueOf(Integer value)
  8. valueOf(Integer value)
  9. valueOf(Short value)