Java Boolean From toBoolean(Object obj)

Here you can find the source of toBoolean(Object obj)

Description

Converts an Object to a Boolean.

License

Open Source License

Parameter

Parameter Description
obj Object to convert to a Boolean object.

Return

null if obj is null, otherwise the Object converted to a Boolean.

Declaration

public static Boolean toBoolean(Object obj) 

Method Source Code

//package com.java2s;
/**//from   w w w.j av a2  s .c  o m
 * This file is protected by Copyright.
 * Please refer to the COPYRIGHT file distributed with this source distribution.
 *
 * This file is part of REDHAWK IDE.
 *
 * All rights reserved.  This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html.
 */

public class Main {
    /**
     * Converts an Object to a Boolean. Support Objects types are:<pre>
     *   1. Boolean   - returns obj
     *   2. String    - returns results of Boolean.valueOf((String) obj)
     *   3. null      - return null
     *   4. otherwise - returns Boolean.FALSE
     * </pre>
     * @param obj Object to convert to a Boolean object.
     * @return null if obj is null, otherwise the Object converted to a Boolean.
     */
    public static Boolean toBoolean(Object obj) {
        if (obj instanceof Boolean) { // most common case
            return (Boolean) obj;
        } else if (obj instanceof String) {
            return Boolean.valueOf((String) obj);
        } else if (obj == null) {
            return null;
        } else {
            return Boolean.FALSE;
        }
    }
}

Related

  1. toBoolean(Object obj)
  2. toBoolean(Object obj)
  3. toBoolean(Object obj)
  4. toBoolean(Object obj)
  5. toBoolean(Object obj)
  6. toBoolean(Object obj)
  7. toBoolean(Object object)
  8. toBoolean(Object object)
  9. toBoolean(Object object)