/*******************************************************************************
 * Copyright 2019 2Fi Business Solutions Ltd.
 *
 * This code is part of the Core project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.core.utils;

public class BooleanUtils extends org.apache.commons.lang3.BooleanUtils {

	/**
	 * <p>
	 * For the case of {@code Number}, only if the value is {@code 1} will return {@code true}. Otherwise, {@code false} is returned.
	 * </p>
	 * <p>
	 * For the case of {@code String}, {@code 'true'}, {@code 'on'} or {@code 'yes'} (case insensitive) will return {@code true}. Otherwise, {@code false} is
	 * returned.
	 * </p>
	 * <p>
	 * For the case of {@code Boolean}, only if the {@code Boolean} value is {@code true} will return {@code true}, handling {@code null} by returning
	 * {@code false}.
	 * </p>
	 * <p>
	 * For any other cases including {@code null} will return {@code false}.
	 * </p>
	 */
	public static boolean isTrue(Object obj) {
		if (obj instanceof Number) {
			return ((Number) obj).intValue() == 1;
		} else if (obj instanceof String) {
			return toBoolean((String) obj);
		} else {
			return Boolean.TRUE.equals(obj);
		}
	}

	/**
	 * <p>
	 * For the case of {@code Number}, only if the value is {@code 0} will return {@code true}. Otherwise, {@code false} is returned.
	 * </p>
	 * <p>
	 * For the case of {@code String}, {@code true} is returned unless the value is {@code 'true'}, {@code 'on'} or {@code 'yes'} (case insensitive) which will
	 * return {@code false}.
	 * </p>
	 * <p>
	 * For the case of {@code Boolean}, only if the {@code Boolean} value is {@code false} will return {@code true}, handling {@code null} by returning
	 * {@code false}.
	 * </p>
	 * <p>
	 * For any other cases including {@code null} will return {@code false}.
	 * </p>
	 */
	public static boolean isFalse(Object obj) {
		if (obj instanceof Number) {
			return ((Number) obj).intValue() == 0;
		} else if (obj instanceof String) {
			return !toBoolean((String) obj);
		} else {
			return Boolean.FALSE.equals(obj);
		}
	}

}
