Java Boolean Convert to booleanToBasicType(boolean b, Class clazz)

Here you can find the source of booleanToBasicType(boolean b, Class clazz)

Description

boolean To Basic Type

License

LGPL

Declaration

@SuppressWarnings("unchecked")
    public static <T> T booleanToBasicType(boolean b, Class<T> clazz) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T booleanToBasicType(boolean b, Class<T> clazz) {
        if (clazz.equals(Boolean.class))
            return (T) Boolean.valueOf(b);
        if (clazz.equals(Byte.class))
            return (T) Byte.valueOf(booleanToByte(b));
        if (clazz.equals(Character.class))
            return (T) Character.valueOf(booleanToChar(b));
        if (clazz.equals(Short.class))
            return (T) Short.valueOf(booleanToShort(b));
        if (clazz.equals(Integer.class))
            return (T) Integer.valueOf(booleanToInt(b));
        if (clazz.equals(Long.class))
            return (T) Long.valueOf(booleanToLong(b));
        if (clazz.equals(Float.class))
            return (T) Float.valueOf(booleanToFloat(b));
        if (clazz.equals(Double.class))
            return (T) Double.valueOf(booleanToDouble(b));
        if (clazz.equals(String.class))
            return (T) String.valueOf(b);

        return null;
    }//from w  w  w .  j  av a 2 s.c o  m

    public static byte booleanToByte(boolean b) {
        return b ? (byte) 1 : 0;
    }

    public static char booleanToChar(boolean b) {
        return b ? (char) 1 : 0;
    }

    public static short booleanToShort(boolean b) {
        return b ? (short) 1 : 0;
    }

    public static int booleanToInt(boolean b) {
        return b ? 1 : 0;
    }

    public static long booleanToLong(boolean b) {
        return b ? 1L : 0L;
    }

    public static float booleanToFloat(boolean b) {
        return b ? 1f : 0f;
    }

    public static double booleanToDouble(boolean b) {
        return b ? 1. : 0.;
    }
}

Related

  1. booleanArrayToInt(boolean[] in)
  2. booleanArrayToIntIndexes(boolean[] arrb)
  3. booleanArrayToString(boolean[] array)
  4. booleanArrayToString(boolean[] b, String delim)
  5. booleanArrayToString(boolean[] ba)
  6. booleanToDouble(boolean b)
  7. booleanToStr(boolean bool)
  8. booleanToTinyInt(boolean b)
  9. booleanToYesNo(Boolean b)