Example usage for java.lang Number byteValue

List of usage examples for java.lang Number byteValue

Introduction

In this page you can find the example usage for java.lang Number byteValue.

Prototype

public byte byteValue() 

Source Link

Document

Returns the value of the specified number as a byte .

Usage

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a byte primitive.
 * // www.  j  av  a  2 s  . co  m
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Byte value.
 */
public static Byte formatByte(String value, Locale locale) {
    Byte result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) {
                result = new Byte(num.byteValue());
            }
        }
    }

    return result;
}

From source file:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 * @param number the number to convert/*from  w w  w  .ja v a  2 s.  c  om*/
 * @param targetClass the target class to convert to
 * @return the converted number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class<?> targetClass)
        throws IllegalArgumentException {

    //   Assert.notNull(number, "Number must not be null");
    //   Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Byte(number.byteValue());
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Short(number.shortValue());
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Integer(number.intValue());
    } else if (targetClass.equals(Long.class)) {
        return new Long(number.longValue());
    } else if (targetClass.equals(Float.class)) {
        return new Float(number.floatValue());
    } else if (targetClass.equals(Double.class)) {
        return new Double(number.doubleValue());
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:org.batoo.jpa.common.reflect.ReflectHelper.java

/**
 * Converts the number into number Type/*from  ww w. j  ava2s.c om*/
 * 
 * @param value
 *            the number value
 * @param numberType
 *            the number type
 * @return the converted number value
 * 
 * @since $version
 * @author hceylan
 */
public static Number convertNumber(Number value, Class<?> numberType) {
    if (numberType == Integer.class) {
        return value.intValue();
    }

    if (numberType == Long.class) {
        return value.longValue();
    }

    if (numberType == Short.class) {
        return value.shortValue();
    }

    if (numberType == Byte.class) {
        return value.byteValue();
    }

    if (numberType == Float.class) {
        return value.floatValue();
    }

    if (numberType == Double.class) {
        return value.doubleValue();
    }

    throw new IllegalArgumentException(numberType + " not supported");
}

From source file:org.briljantframework.data.vector.Convert.java

@SuppressWarnings("unchecked")
private static <T> T convertNumber(Class<T> cls, Number num) {
    if (cls.equals(Double.class) || cls.equals(Double.TYPE)) {
        return (T) (Double) num.doubleValue();
    } else if (cls.equals(Float.class) || cls.equals(Float.TYPE)) {
        return (T) (Float) num.floatValue();
    } else if (cls.equals(Long.class) || cls.equals(Long.TYPE)) {
        return (T) (Long) num.longValue();
    } else if (cls.equals(Integer.class) || cls.equals(Integer.TYPE)) {
        return (T) (Integer) num.intValue();
    } else if (cls.equals(Short.class) || cls.equals(Short.TYPE)) {
        return (T) (Short) num.shortValue();
    } else if (cls.equals(Byte.class) || cls.equals(Byte.TYPE)) {
        return (T) (Byte) num.byteValue();
    } else if (Complex.class.equals(cls)) {
        return cls.cast(Complex.valueOf(num.doubleValue()));
    } else if (Logical.class.equals(cls)) {
        return cls.cast(num.intValue() == 1 ? Logical.TRUE : Logical.FALSE);
    } else if (Boolean.class.equals(cls)) {
        return cls.cast(num.intValue() == 1);
    } else {/*from   w w w .j a  v a  2  s .c o m*/
        return Na.of(cls);
    }
}

From source file:org.crazydog.util.spring.NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 *
 * @param number      the number to convert
 * @param targetClass the target class to convert to
 * @return the converted number/* w  w w  .ja va 2s.c om*/
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see Byte
 * @see Short
 * @see Integer
 * @see Long
 * @see BigInteger
 * @see Float
 * @see Double
 * @see BigDecimal
 */
@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {

    org.springframework.util.Assert.notNull(number, "Number must not be null");
    org.springframework.util.Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (Byte.class == targetClass) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Byte(number.byteValue());
    } else if (Short.class == targetClass) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (Integer.class == targetClass) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (Long.class == targetClass) {
        BigInteger bigInt = null;
        if (number instanceof BigInteger) {
            bigInt = (BigInteger) number;
        } else if (number instanceof BigDecimal) {
            bigInt = ((BigDecimal) number).toBigInteger();
        }
        // Effectively analogous to JDK 8's BigInteger.longValueExact()
        if (bigInt != null && (bigInt.compareTo(LONG_MIN) < 0 || bigInt.compareTo(LONG_MAX) > 0)) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Long(number.longValue());
    } else if (BigInteger.class == targetClass) {
        if (number instanceof BigDecimal) {
            // do not lose precision - use BigDecimal's own conversion
            return (T) ((BigDecimal) number).toBigInteger();
        } else {
            // original value is not a Big* number - use standard long conversion
            return (T) BigInteger.valueOf(number.longValue());
        }
    } else if (Float.class == targetClass) {
        return (T) new Float(number.floatValue());
    } else if (Double.class == targetClass) {
        return (T) new Double(number.doubleValue());
    } else if (BigDecimal.class == targetClass) {
        // always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return (T) new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java

private static <T> T fromNumber(final Object columnObject, final Class<T> resultingClass) throws SQLException {
    final Number n = Number.class.cast(columnObject);
    if (resultingClass == BigDecimal.class) {
        return resultingClass.cast(new BigDecimal(n.toString()));
    }//from ww w.  j a va  2  s . co m
    if (resultingClass == BigInteger.class) {
        return resultingClass.cast(new BigInteger(n.toString()));
    }
    if (resultingClass == Byte.class) {
        return resultingClass.cast(new Byte(n.byteValue()));
    }
    if (resultingClass == Double.class) {
        return resultingClass.cast(new Double(n.doubleValue()));
    }
    if (resultingClass == Float.class) {
        return resultingClass.cast(new Float(n.floatValue()));
    }
    if (resultingClass == Integer.class) {
        return resultingClass.cast(new Integer(n.intValue()));
    }
    if (resultingClass == Long.class) {
        return resultingClass.cast(new Long(n.longValue()));
    }
    if (resultingClass == Short.class) {
        return resultingClass.cast(new Short(n.shortValue()));
    }
    if (resultingClass == String.class) {
        return resultingClass.cast(n.toString());
    }
    if (resultingClass == Boolean.class) {
        if (n.byteValue() == 0) {
            return resultingClass.cast(Boolean.FALSE);
        }
        if (n.byteValue() == 1) {
            return resultingClass.cast(Boolean.TRUE);
        }
    }
    if (resultingClass == byte[].class) {
        return resultingClass.cast(n.toString().getBytes());
    }
    if (resultingClass == Blob.class) {
        return resultingClass.cast(new SerialBlob(n.toString().getBytes()));
    }
    if (resultingClass == Clob.class) {
        return resultingClass.cast(new SerialClob(n.toString().toCharArray()));
    }
    return null;
}

From source file:demo.config.PropertyConverter.java

/**
 * Convert the specified object into a Byte.
 * // w ww  .j  a  v  a2  s .  c o  m
 * @param value
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to a byte
 */
public static Byte toByte(Object value) throws ConversionException {
    Number n = toNumber(value, Byte.class);
    if (n instanceof Byte) {
        return (Byte) n;
    } else {
        return new Byte(n.byteValue());
    }
}

From source file:com.sun.faces.el.impl.Coercions.java

/**
 * Coerces a Number to the given primitive number class
 *//*from w  w w . j  a  v a 2s.  c  o  m*/
static Number coerceToPrimitiveNumber(Number pValue, Class pClass) throws ElException {
    if (pClass == Byte.class || pClass == Byte.TYPE) {
        return PrimitiveObjects.getByte(pValue.byteValue());
    } else if (pClass == Short.class || pClass == Short.TYPE) {
        return PrimitiveObjects.getShort(pValue.shortValue());
    } else if (pClass == Integer.class || pClass == Integer.TYPE) {
        return PrimitiveObjects.getInteger(pValue.intValue());
    } else if (pClass == Long.class || pClass == Long.TYPE) {
        return PrimitiveObjects.getLong(pValue.longValue());
    } else if (pClass == Float.class || pClass == Float.TYPE) {
        return PrimitiveObjects.getFloat(pValue.floatValue());
    } else if (pClass == Double.class || pClass == Double.TYPE) {
        return PrimitiveObjects.getDouble(pValue.doubleValue());
    } else if (pClass == BigInteger.class) {
        if (pValue instanceof BigDecimal)
            return ((BigDecimal) pValue).toBigInteger();
        else
            return BigInteger.valueOf(pValue.longValue());
    } else if (pClass == BigDecimal.class) {
        if (pValue instanceof BigInteger)
            return new BigDecimal((BigInteger) pValue);
        else
            return new BigDecimal(pValue.doubleValue());
    } else {
        return PrimitiveObjects.getInteger(0);
    }
}

From source file:routines.system.BigDataParserUtils.java

/**
 * Parse anything to Byte/*from   w w w  .ja v a 2 s . co m*/
 *
 * @param input
 * @return
 */
public static Byte parseTo_Byte(Number input) {
    if (input == null) {
        return null;
    }
    return input.byteValue();
}

From source file:routines.system.BigDataParserUtils.java

/**
 * Parse anything to byte/*from   w  w w .  j  av  a2s.co m*/
 *
 * @param input
 * @return
 */
public static byte parseTo_byte(Number input) {
    if (input == null) {
        return defaultValueByte;
    }
    return input.byteValue();
}