Example usage for java.lang Number shortValue

List of usage examples for java.lang Number shortValue

Introduction

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

Prototype

public short shortValue() 

Source Link

Document

Returns the value of the specified number as a short .

Usage

From source file:Main.java

public static void main(String[] args) {

    // get a number as float
    Number x = new Float(123456f);

    // get a number as double
    Number y = new Double(9876);

    // print their value as short
    System.out.println("x as float :" + x + ", x as short:" + x.shortValue());
    System.out.println("y as double:" + y + ", y as short:" + y.shortValue());

}

From source file:Main.java

public static short[] toShortArray(Collection<? extends Number> c) {
    short arr[] = new short[c.size()];
    int i = 0;/*from  w w w  . j  av a  2 s .  co  m*/
    for (Number item : c) {
        arr[i++] = item.shortValue();
    }
    return arr;
}

From source file:Main.java

public static short[] toShortArray(Collection<? extends Number> c) {
    short arr[] = new short[c.size()];
    int i = 0;/*from   w  w  w  . jav a  2s.  c om*/
    for (Number item : c)
        arr[i++] = item.shortValue();
    return arr;
}

From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java

public static short[] convertToShortArray(final Object array) {
    if (array == null) {
        return null;
    }/*from  ww w.  j av a2  s.  c o m*/
    if (array instanceof short[]) {
        return (short[]) array;
    }
    if (array instanceof Short[]) {
        return ArrayUtils.toPrimitive((Short[]) array);
    }
    final short[] newArray = new short[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Number val = (Number) Array.get(array, i);
        newArray[i] = val.shortValue();
    }
    return newArray;
}

From source file:net.dontdrinkandroot.utils.lang.math.NumberUtils.java

/**
 * Get the null safe shortValue of a Number. Defaults to 0.
 * /*  ww  w .  ja  v a  2s  .  c  om*/
 * @param number
 *            The Number to convert.
 * @return The null safe shortValue. Defaults to 0.
 */
public static short shortValue(final Number number) {

    if (number == null) {
        return 0;
    }

    return number.shortValue();
}

From source file:gedi.util.MathUtils.java

/**
 * Throws an exception if n is either a real or to big to be represented by a byte.
 * @param n//from w  w  w  .ja va2  s . co m
 * @return
 */
public static short shortValueExact(Number n) {
    if (n instanceof Short || n instanceof Byte)
        return n.shortValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        if (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE)
            return (short) l;
    }
    throw new NumberFormatException();
}

From source file:io.fabric8.jolokia.support.JolokiaHelpers.java

public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException {
    if (clazz.isArray()) {
        if (value instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) value;
            Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size());
            int idx = 0;
            for (Object element : jsonArray) {
                Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element));
            }//  w w w.ja v a  2  s  .  c o m
            return javaArray;
        } else {
            return null;
        }
    } else if (String.class.equals(clazz)) {
        return (value != null) ? value.toString() : null;
    } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
        Number number = asNumber(value);
        return number != null ? number.byteValue() : null;
    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
        Number number = asNumber(value);
        return number != null ? number.shortValue() : null;
    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
        Number number = asNumber(value);
        return number != null ? number.intValue() : null;
    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
        Number number = asNumber(value);
        return number != null ? number.longValue() : null;
    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
        Number number = asNumber(value);
        return number != null ? number.floatValue() : null;
    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
        Number number = asNumber(value);
        return number != null ? number.doubleValue() : null;
    } else if (value instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) value;
        if (!JSONObject.class.isAssignableFrom(clazz)) {
            String json = jsonObject.toJSONString();
            return getObjectMapper().readerFor(clazz).readValue(json);
        }
    }
    return value;
}

From source file:com.healthmarketscience.jackcess.query.QueryTest.java

private static Row newRow(Byte attr, String expr, Number flagNum, Number extraNum, String name1, String name2) {
    Short flag = ((flagNum != null) ? flagNum.shortValue() : null);
    Integer extra = ((extraNum != null) ? extraNum.intValue() : null);
    return new Row(null, attr, expr, flag, extra, name1, name2, null, null);
}

From source file:Main.java

/**
 * This method converts a given number into a target class. This method does not change the value (except when
 * explicitly casting to a more general type, e.g. from double to int), just the internal type representation. While
 * this is unnecessary while using normal java code, reflection based access to method parameters is a bit more
 * difficult. As far as possible, this method will prevent the ArgumentMismatch error when passing numbers as
 * parameters.//from  w  w  w  . j a  v  a2  s .  c om
 * <p/>
 * If the value can not be converted to the given target class, it will be returned unchanged.
 *
 * @param targetClass Class to which the number should be converted, if possible.
 * @param value       Number value to convert.
 * @return 'value' converted to an instance of 'targetClass'.
 */
public static Object convertNumber(final Class targetClass, final Number value) {
    if (targetClass.equals(Double.class) || targetClass.equals(Double.TYPE))
        return value.doubleValue();
    if (targetClass.equals(Integer.class) || targetClass.equals(Integer.TYPE))
        return value.intValue();
    if (targetClass.equals(Long.class) || targetClass.equals(Long.TYPE))
        return value.longValue();
    if (targetClass.equals(Short.class) || targetClass.equals(Short.TYPE))
        return value.shortValue();
    if (targetClass.equals(Byte.class) || targetClass.equals(Byte.TYPE))
        return value.byteValue();
    if (targetClass.equals(Character.class) || targetClass.equals(Character.TYPE))
        return value.intValue();
    if (targetClass.equals(Float.class) || targetClass.equals(Float.TYPE))
        return value.floatValue();
    return value;
}

From source file:NumberUtils.java

/**
 * Converts the given number to a <code>Short</code> (by using <code>shortValue()</code>).
 *
 * @param number//from   ww w .  jav a2  s.c o  m
 * @return java.lang.Short
 * @throws IllegalArgumentException The given number is 'not a number' or infinite.
 */
public static Short toShort(Number number) throws IllegalArgumentException {
    if (number == null || number instanceof Short)
        return (Short) number;
    if (isNaN(number) || isInfinite(number))
        throw new IllegalArgumentException("Argument must not be NaN or infinite.");
    return new Short(number.shortValue());
}