Java Object to Boolean asBoolean(Object value)

Here you can find the source of asBoolean(Object value)

Description

convert a object value to boolean value.

License

Open Source License

Parameter

Parameter Description
value a parameter

Declaration

public static boolean asBoolean(Object value) 

Method Source Code

//package com.java2s;
/*/* w  w  w.j a v a 2  s.c  o m*/
 * Copyright 2012 The Solmix Project
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */

public class Main {
    /**
     * convert a object value to boolean value.
     * 
     * @param value
     * @return
     */
    public static boolean asBoolean(Object value) {
        if (value instanceof Boolean)
            return ((Boolean) value).booleanValue();
        if (value instanceof Number) {
            return ((Number) value).intValue() > 0;
        } else {
            String boolString = (String) value;
            return boolString != null && !boolString.equals("") && !boolString.equalsIgnoreCase("false");
        }
    }

    /**
     * Return the boolean value. if value is null return false.
     * 
     * @param value
     * @return
     */
    public static boolean booleanValue(Boolean value) {
        if (value == null)
            return false;
        else
            return value.booleanValue();
    }

    /**
     * If not equal return true.else return false. used {@link #isNotEqual(Object, Object)}
     * 
     * @param <T>
     * @param actual
     * @param expect
     * @return
     */
    public static boolean booleanValue(String booleanValue) {
        if ("true".equalsIgnoreCase(booleanValue))
            return true;
        else
            return false;
    }
}

Related

  1. asBoolean(Object obj)
  2. asBoolean(Object obj)
  3. asBoolean(Object object)
  4. asBoolean(Object value)
  5. asBoolean(Object value)
  6. castBoolean(Object inObject)
  7. castBoolean(Object o)