Java Long to longToBasicType(long l, Class clazz)

Here you can find the source of longToBasicType(long l, Class clazz)

Description

long To Basic Type

License

LGPL

Declaration

@SuppressWarnings("unchecked")
    public static <T> T longToBasicType(long l, Class<T> clazz) 

Method Source Code

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

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T longToBasicType(long l, Class<T> clazz) {
        if (clazz.equals(Boolean.class))
            return (T) Boolean.valueOf(longToBoolean(l));
        if (clazz.equals(Byte.class))
            return (T) longToByte(l);
        if (clazz.equals(Character.class))
            return (T) longToCharacter(l);
        if (clazz.equals(Short.class))
            return (T) longToShort(l);
        if (clazz.equals(Integer.class))
            return (T) longToInteger(l);
        if (clazz.equals(Long.class))
            return (T) Long.valueOf(l);
        if (clazz.equals(Float.class))
            return (T) Float.valueOf(l);
        if (clazz.equals(Double.class))
            return (T) Double.valueOf(l);
        if (clazz.equals(String.class))
            return (T) String.valueOf(l);

        return null;
    }/*from   w w  w  .  j av a2s  . co m*/

    public static boolean longToBoolean(long l) {
        return l != 0 ? true : false;
    }

    public static Byte longToByte(long l) {
        if (l >= Byte.MIN_VALUE && l <= Byte.MAX_VALUE) {
            return (byte) l;
        }
        return null;
    }

    public static byte longToByte(long l, byte defaultValue) {
        if (l >= Byte.MIN_VALUE && l <= Byte.MAX_VALUE) {
            return (byte) l;
        }
        return defaultValue;
    }

    public static Character longToCharacter(long l) {
        if (l >= Character.MIN_VALUE && l <= Character.MAX_VALUE) {
            return (char) l;
        }
        return null;
    }

    public static Short longToShort(long l) {
        if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE) {
            return (short) l;
        }
        return null;
    }

    public static short longToShort(long l, short defaultValue) {
        if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE) {
            return (short) l;
        }
        return defaultValue;
    }

    public static Integer longToInteger(long l) {
        if (l >= Integer.MIN_VALUE && l <= Integer.MAX_VALUE) {
            return (int) l;
        }
        return null;
    }
}

Related

  1. longTo36Str(String str)
  2. longTo4ByteArray(byte[] bytes, int offset, long value)
  3. longTo4LengthBytes(long num)
  4. LongToAscii(long number, byte[] buf, int offset, int length)
  5. longToBaseCode(char[] target, int targetOffset, long value, int base, int lengthLimit, boolean fillZeros, boolean upperCase)
  6. longToBcd(long num, int size)
  7. longToBcd(long src, int len, int flag)
  8. longToBigEndian(long value, byte[] in, int offset)
  9. longToBinary(long num)