Java Integer to intToBasicType(int i, Class clazz)

Here you can find the source of intToBasicType(int i, Class clazz)

Description

int To Basic Type

License

LGPL

Declaration

@SuppressWarnings("unchecked")
    public static <T> T intToBasicType(int i, Class<T> clazz) 

Method Source Code

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

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T intToBasicType(int i, Class<T> clazz) {
        if (clazz.equals(Boolean.class))
            return (T) Boolean.valueOf(intToBoolean(i));
        if (clazz.equals(Byte.class))
            return (T) intToByte(i);
        if (clazz.equals(Character.class))
            return (T) intToCharacter(i);
        if (clazz.equals(Short.class))
            return (T) intToShort(i);
        if (clazz.equals(Integer.class))
            return (T) Integer.valueOf(i);
        if (clazz.equals(Long.class))
            return (T) Long.valueOf(i);
        if (clazz.equals(Float.class))
            return (T) Float.valueOf(i);
        if (clazz.equals(Double.class))
            return (T) Double.valueOf(i);

        return null;
    }//from  w w  w.ja v a  2  s .c  o m

    public static boolean intToBoolean(int i) {
        return i != 0 ? true : false;
    }

    public static Byte intToByte(int i) {
        if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
            return (byte) i;
        }
        return null;
    }

    public static byte intToByte(int i, byte defaultValue) {
        if (i >= Byte.MIN_VALUE && i <= Byte.MAX_VALUE) {
            return (byte) i;
        }
        return defaultValue;
    }

    public static Character intToCharacter(int i) {
        if (i >= Character.MIN_VALUE && i <= Character.MAX_VALUE) {
            return (char) i;
        }
        return null;
    }

    public static Short intToShort(int i) {
        if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
            return (short) i;
        }
        return null;
    }

    public static short intToShort(int i, short defaultValue) {
        if (i >= Short.MIN_VALUE && i <= Short.MAX_VALUE) {
            return (short) i;
        }
        return defaultValue;
    }
}

Related

  1. intTo8HexString(int i)
  2. intToAlgebraicLoc(int loc)
  3. intToAlpha(Integer no)
  4. intToASN1(byte[] d, int idx, int val)
  5. intToBase32(int n)
  6. intToBcd(int src, int len, int flag)
  7. intToBigEndian(int value, byte[] array, int index)
  8. intToBigEndianByteArray(int in)
  9. intToBlue(int color)