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:com.scit.sling.test.MockValueMap.java

@SuppressWarnings("unchecked")
private <T> T convertType(Object o, Class<T> type) {
    if (o == null) {
        return null;
    }/*w w w .  j  a  v a  2  s  .co  m*/
    if (o.getClass().isArray() && type.isArray()) {
        if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) {
            return (T) o;
        } else {
            // we need to convert the elements in the array
            Object array = Array.newInstance(type.getComponentType(), Array.getLength(o));
            for (int i = 0; i < Array.getLength(o); i++) {
                Array.set(array, i, convertType(Array.get(o, i), type.getComponentType()));
            }
            return (T) array;
        }
    }
    if (o.getClass().isAssignableFrom(type)) {
        return (T) o;
    }
    if (String.class.isAssignableFrom(type)) {
        // Format dates
        if (o instanceof Calendar) {
            return (T) formatDate((Calendar) o);
        } else if (o instanceof Date) {
            return (T) formatDate((Date) o);
        } else if (o instanceof DateTime) {
            return (T) formatDate((DateTime) o);
        }
        return (T) String.valueOf(o);
    } else if (o instanceof DateTime) {
        DateTime dt = (DateTime) o;
        if (Calendar.class.isAssignableFrom(type)) {
            return (T) dt.toCalendar(Locale.getDefault());
        } else if (Date.class.isAssignableFrom(type)) {
            return (T) dt.toDate();
        } else if (Number.class.isAssignableFrom(type)) {
            return convertType(dt.getMillis(), type);
        }
    } else if (o instanceof Number && Number.class.isAssignableFrom(type)) {
        if (Byte.class.isAssignableFrom(type)) {
            return (T) (Byte) ((Number) o).byteValue();
        } else if (Double.class.isAssignableFrom(type)) {
            return (T) (Double) ((Number) o).doubleValue();
        } else if (Float.class.isAssignableFrom(type)) {
            return (T) (Float) ((Number) o).floatValue();
        } else if (Integer.class.isAssignableFrom(type)) {
            return (T) (Integer) ((Number) o).intValue();
        } else if (Long.class.isAssignableFrom(type)) {
            return (T) (Long) ((Number) o).longValue();
        } else if (Short.class.isAssignableFrom(type)) {
            return (T) (Short) ((Number) o).shortValue();
        } else if (BigDecimal.class.isAssignableFrom(type)) {
            return (T) new BigDecimal(o.toString());
        }
    } else if (o instanceof Number && type.isPrimitive()) {
        final Number num = (Number) o;
        if (type == byte.class) {
            return (T) new Byte(num.byteValue());
        } else if (type == double.class) {
            return (T) new Double(num.doubleValue());
        } else if (type == float.class) {
            return (T) new Float(num.floatValue());
        } else if (type == int.class) {
            return (T) new Integer(num.intValue());
        } else if (type == long.class) {
            return (T) new Long(num.longValue());
        } else if (type == short.class) {
            return (T) new Short(num.shortValue());
        }
    } else if (o instanceof String && Number.class.isAssignableFrom(type)) {
        if (Byte.class.isAssignableFrom(type)) {
            return (T) new Byte((String) o);
        } else if (Double.class.isAssignableFrom(type)) {
            return (T) new Double((String) o);
        } else if (Float.class.isAssignableFrom(type)) {
            return (T) new Float((String) o);
        } else if (Integer.class.isAssignableFrom(type)) {
            return (T) new Integer((String) o);
        } else if (Long.class.isAssignableFrom(type)) {
            return (T) new Long((String) o);
        } else if (Short.class.isAssignableFrom(type)) {
            return (T) new Short((String) o);
        } else if (BigDecimal.class.isAssignableFrom(type)) {
            return (T) new BigDecimal((String) o);
        }
    }
    throw new NotImplementedException(
            "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName());
}

From source file:moe.encode.airblock.commands.arguments.types.PrimitiveParser.java

@Override
public Object convert(Executor executor, ArgumentConverter parser, Type type, String value) {
    Class<?> cls = ReflectionUtils.toClass(type);
    if (ClassUtils.isPrimitiveWrapper(cls))
        cls = ClassUtils.wrapperToPrimitive(cls);

    if (cls.equals(boolean.class))
        return this.isTrue(executor, value);
    else if (cls.equals(char.class)) {
        if (value.length() > 0)
            throw new NumberFormatException("Character arguments cannot be longer than one characters");
        return value.charAt(0);
    }//from w w  w  . ja v a 2 s .co  m

    // Get the locale of the user and get a number-format according to it.
    LocaleResolver resolver = TranslationManager.getResolver(executor);
    Locale locale;
    if (resolver != null)
        locale = resolver.getLocale();
    else
        locale = Locale.ENGLISH;

    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    nf.setGroupingUsed(true);

    // Parse the value.
    Number result;
    try {
        result = nf.parse(value);
    } catch (ParseException e) {
        NumberFormatException nfe = new NumberFormatException("Invalid number");
        nfe.initCause(e);
        throw nfe;
    }

    // Returns the value in the correct type.
    if (cls.equals(int.class))
        return result.intValue();
    else if (cls.equals(float.class))
        return result.floatValue();
    else if (cls.equals(double.class))
        return result.doubleValue();
    else if (cls.equals(byte.class))
        return result.byteValue();
    else if (cls.equals(short.class))
        return result.shortValue();
    else if (cls.equals(long.class))
        return result.longValue();

    throw new NumberFormatException("Unknown primitive type.");
}

From source file:com.itelis.worker.dev.template.service.JRXmlDataSource.java

protected Object convertNumber(Number number, Class valueClass) throws JRException {
    Number value = null;//from  w ww .  java 2  s  . c om
    if (valueClass.equals(Byte.class)) {
        value = new Byte(number.byteValue());
    } else if (valueClass.equals(Short.class)) {
        value = new Short(number.shortValue());
    } else if (valueClass.equals(Integer.class)) {
        value = new Integer(number.intValue());
    } else if (valueClass.equals(Long.class)) {
        value = new Long(number.longValue());
    } else if (valueClass.equals(Float.class)) {
        value = new Float(number.floatValue());
    } else if (valueClass.equals(Double.class)) {
        value = new Double(number.doubleValue());
    } else if (valueClass.equals(BigInteger.class)) {
        value = BigInteger.valueOf(number.longValue());
    } else if (valueClass.equals(BigDecimal.class)) {
        value = new BigDecimal(Double.toString(number.doubleValue()));
    } else {
        throw new JRException("Unknown number class " + valueClass.getName());
    }
    return value;
}

From source file:cz.cvut.kbss.jsonld.jackson.serialization.JacksonJsonWriter.java

@Override
public void writeNumber(Number number) throws IOException {
    if (number instanceof Integer) {
        jsonGenerator.writeNumber(number.intValue());
    } else if (number instanceof Long) {
        jsonGenerator.writeNumber(number.longValue());
    } else if (number instanceof Float) {
        jsonGenerator.writeNumber(number.floatValue());
    } else if (number instanceof Double) {
        jsonGenerator.writeNumber(number.doubleValue());
    } else if (number instanceof BigInteger) {
        jsonGenerator.writeNumber((BigInteger) number);
    } else if (number instanceof BigDecimal) {
        jsonGenerator.writeNumber((BigDecimal) number);
    } else if (number instanceof Short) {
        jsonGenerator.writeNumber(number.shortValue());
    } else if (number instanceof Byte) {
        jsonGenerator.writeNumber(number.byteValue());
    } else {/* ww w . j a v a  2s  . c  o  m*/
        throw new IllegalArgumentException(
                "Unable to write number " + number + " of type " + number.getClass());
    }
}

From source file:Unsigned.java

/**
 * Parse a binary number into a Number object. If up to 8 bits are parsed,
 * returns a Byte. If more than 8 and up to 16 bits are parsed, return a
 * Short. If more than 16 and up to 32 bits are parsed, return an Integer.
 * If more than 32 and up to 64 bits are parsed, return a Long.
 * /*  w  w w .j av  a2 s .c o  m*/
 * @param text
 *            a binary number
 * @param parsePosition
 *            position to start parsing from
 * @return return an integer form of Number object if parse is successful;
 *         <CODE>null</CODE> otherwise
 * 
 * @since 1.0
 */
public Number parse(String text, ParsePosition parsePosition) {
    boolean skipWhitespace = true;
    int startIndex, bits;

    // remove whitespace
    StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex());
    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
    }
    parsePosition.setIndex(iter.getIndex());

    startIndex = parsePosition.getIndex();
    Number result = (Number) parseObject(text, parsePosition);

    if (result == null) {
        return (result);
    }

    bits = parsePosition.getIndex() - startIndex;
    if (bits <= 8) {
        result = new Byte(result.byteValue());
    } else if (bits <= 16) {
        result = new Short(result.shortValue());
    } else if (bits <= 32) {
        result = new Integer(result.intValue());
    } else if (bits <= 64) {
        result = new Long(result.longValue());
    }
    return (result);
}

From source file:HexFormat.java

/**
 * Parse a hex number into a Number object. Hexadecimal numbers may be
 * indicated with a leading character designation of '0x'. If up to 1 byte
 * is parsed, returns a Byte. If more than 1 and up to 2 bytes are parsed,
 * return a Short. If more than 2 and up to 4 bytes are parsed, return an
 * Integer. If more than 4 and up to 8 bytes are parsed, return a Long.
 * /*w  w  w.ja  v  a2 s . c o  m*/
 * @param text
 *            a hexadecimal number
 * @param parsePosition
 *            position to start parsing from
 * @return return an integer form of Number object if parse is successful;
 *         <CODE>null</CODE> otherwise
 * 
 * @since 1.0
 */
public Number parse(String text, ParsePosition parsePosition) {
    boolean skipWhitespace = true;
    int startIndex, nibbles;

    // remove whitespace
    StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex());
    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        break;
    }

    // skip a leading hex designation of the characters '0x'
    if (text.regionMatches(iter.getIndex(), "0x", 0, 2)) {
        parsePosition.setIndex(iter.getIndex() + 2);
    } else {
        parsePosition.setIndex(iter.getIndex());
    }

    startIndex = parsePosition.getIndex();
    Number result = (Number) parseObject(text, parsePosition);

    if (result == null) {
        return (result);
    }

    nibbles = parsePosition.getIndex() - startIndex;
    if (nibbles <= 2) {
        result = new Byte(result.byteValue());
    } else if (nibbles <= 4) {
        result = new Short(result.shortValue());
    } else if (nibbles <= 8) {
        result = new Integer(result.intValue());
    } else if (nibbles <= 16) {
        result = new Long(result.longValue());
    }
    return (result);
}

From source file:org.apache.calcite.runtime.SqlFunctions.java

public static short toShort(Number number) {
    return number.shortValue();
}

From source file:org.openconcerto.sql.model.graph.DatabaseGraph.java

private Rule getRule(final Number n, final SQLSystem sys) {
    final Rule res = Rule.fromShort(n.shortValue());
    // MS SQL incorrectly report RESTRICT as it doesn't support it
    return sys == SQLSystem.MSSQL && Rule.RESTRICT.equals(res) ? Rule.NO_ACTION : res;
}

From source file:com.facebook.stetho.json.ObjectMapper.java

private Object getValueForField(Field field, Object value) throws JSONException {
    try {/*from  w w  w  .  j  a  va 2s .  c  om*/
        if (value != null) {
            if (value == JSONObject.NULL) {
                return null;
            }
            if (value.getClass() == field.getType()) {
                return value;
            }
            if (value instanceof JSONObject) {
                return convertValue(value, field.getType());
            } else {
                if (field.getType().isEnum()) {
                    return getEnumValue((String) value, field.getType().asSubclass(Enum.class));
                } else if (value instanceof JSONArray) {
                    return convertArrayToList(field, (JSONArray) value);
                } else if (value instanceof Number) {
                    // Need to convert value to Number This happens because json treats 1 as an Integer even
                    // if the field is supposed to be a Long
                    Number numberValue = (Number) value;
                    Class<?> clazz = field.getType();
                    if (clazz == Integer.class || clazz == int.class) {
                        return numberValue.intValue();
                    } else if (clazz == Long.class || clazz == long.class) {
                        return numberValue.longValue();
                    } else if (clazz == Double.class || clazz == double.class) {
                        return numberValue.doubleValue();
                    } else if (clazz == Float.class || clazz == float.class) {
                        return numberValue.floatValue();
                    } else if (clazz == Byte.class || clazz == byte.class) {
                        return numberValue.byteValue();
                    } else if (clazz == Short.class || clazz == short.class) {
                        return numberValue.shortValue();
                    } else {
                        throw new IllegalArgumentException("Not setup to handle class " + clazz.getName());
                    }
                }
            }
        }
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException("Unable to set value for field " + field.getName(), e);
    }
    return value;
}

From source file:com.jkoolcloud.tnt4j.streams.utils.Utils.java

/**
 * Casts provided number value to desired number type.
 *
 * @param num//  ww  w  . j a  va  2  s  . c om
 *            number value to cast
 * @param clazz
 *            number class to cast number to
 * @param <T>
 *            desired number type
 * @return number value cast to desired numeric type
 */
@SuppressWarnings("unchecked")
public static <T extends Number> T castNumber(Number num, Class<T> clazz) {
    Number cNum = 0;

    if (clazz.isAssignableFrom(Long.class)) {
        cNum = num.longValue();
    } else if (clazz.isAssignableFrom(Integer.class)) {
        cNum = num.intValue();
    } else if (clazz.isAssignableFrom(Byte.class)) {
        cNum = num.byteValue();
    } else if (clazz.isAssignableFrom(Float.class)) {
        cNum = num.floatValue();
    } else if (clazz.isAssignableFrom(Double.class)) {
        cNum = num.doubleValue();
    } else if (clazz.isAssignableFrom(Short.class)) {
        cNum = num.shortValue();
    }

    return (T) cNum;
}