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:org.red5.io.utils.ConversionUtils.java

/**
 * Convert number to primitive wrapper like Boolean or Float
 * //from   www .  j a va  2  s .co m
 * @param num
 *            Number to conver
 * @param wrapper
 *            Primitive wrapper type
 * @return Converted object
 */
public static Object convertNumberToWrapper(Number num, Class<?> wrapper) {
    //XXX Paul: Using valueOf will reduce object creation
    if (wrapper.equals(String.class)) {
        return num.toString();
    } else if (wrapper.equals(Boolean.class)) {
        return Boolean.valueOf(num.intValue() == 1);
    } else if (wrapper.equals(Double.class)) {
        return Double.valueOf(num.doubleValue());
    } else if (wrapper.equals(Long.class)) {
        return Long.valueOf(num.longValue());
    } else if (wrapper.equals(Float.class)) {
        return Float.valueOf(num.floatValue());
    } else if (wrapper.equals(Integer.class)) {
        return Integer.valueOf(num.intValue());
    } else if (wrapper.equals(Short.class)) {
        return Short.valueOf(num.shortValue());
    } else if (wrapper.equals(Byte.class)) {
        return Byte.valueOf(num.byteValue());
    }
    throw new ConversionException(String.format("Unable to convert number to: %s", wrapper));
}

From source file:javadz.beanutils.locale.converters.ShortLocaleConverter.java

/**
 * Convert the specified locale-sensitive input object into an output object of the
 * specified type. This method will return values of type Short.
 *
 * @param value The input object to be converted
 * @param pattern The pattern is used for the convertion
 * @return The converted value/*w  ww .  j  a  va2  s  . c  o  m*/
 *
 * @exception org.apache.commons.beanutils.ConversionException if conversion cannot be performed
 *  successfully
 * @throws ParseException if an error occurs parsing a String to a Number
 * @since 1.8.0
 */
protected Object parse(Object value, String pattern) throws ParseException {

    Object result = super.parse(value, pattern);

    if (result == null || result instanceof Short) {
        return result;
    }

    Number parsed = (Number) result;
    if (parsed.longValue() != parsed.shortValue()) {
        throw new ConversionException("Supplied number is not of type Short: " + parsed.longValue());
    }

    // now returns property Short
    return new Short(parsed.shortValue());
}

From source file:MutableShort.java

/**
 * Adds a value./*from ww w  .  j  av  a 2  s  .c om*/
 * 
 * @param operand
 *            the value to add
 * @throws NullPointerException
 *             if the object is null
 *
 * @since Commons Lang 2.2
 */
public void add(Number operand) {
    this.value += operand.shortValue();
}

From source file:MutableShort.java

/**
 * Subtracts a value./*from  w  w w  .j a  v a2  s.  c  o  m*/
 * 
 * @param operand
 *            the value to add
 * @throws NullPointerException
 *             if the object is null
 *
 * @since Commons Lang 2.2
 */
public void subtract(Number operand) {
    this.value -= operand.shortValue();
}

From source file:MutableShort.java

/**
 * Constructs a new MutableShort with the specified value.
 * //  w  w  w  .  j  a  v  a  2  s  .c o  m
 * @param value
 *                  a value.
 * @throws NullPointerException
 *                  if the object is null
 */
public MutableShort(Number value) {
    super();
    this.value = value.shortValue();
}

From source file:net.jofm.format.NumberFormat.java

private Object convert(Number result, Class<?> destinationClazz) {
    if (destinationClazz.equals(BigDecimal.class)) {
        return new BigDecimal(result.doubleValue());
    }//from   ww w.  j ava2 s .  c o  m
    if (destinationClazz.equals(Short.class) || destinationClazz.equals(short.class)) {
        return new Short(result.shortValue());
    }
    if (destinationClazz.equals(Integer.class) || destinationClazz.equals(int.class)) {
        return new Integer(result.intValue());
    }
    if (destinationClazz.equals(Long.class) || destinationClazz.equals(long.class)) {
        return new Long(result.longValue());
    }
    if (destinationClazz.equals(Float.class) || destinationClazz.equals(float.class)) {
        return new Float(result.floatValue());
    }
    if (destinationClazz.equals(Double.class) || destinationClazz.equals(double.class)) {
        return new Double(result.doubleValue());
    }

    throw new FixedMappingException("Unable to parse the data to type " + destinationClazz.getName() + " using "
            + this.getClass().getName());
}

From source file:org.diorite.config.impl.ConfigPropertyValueImpl.java

@SuppressWarnings("unchecked")
@Override//w  ww .  ja v  a  2  s. c om
public void setRawValue(@Nullable T value) {
    Class<T> rawType = this.template.getRawType();
    Class<?> primitiveRawType = DioriteReflectionUtils.getPrimitive(rawType);
    if (!primitiveRawType.isPrimitive()) {
        if (value != null) {
            if (!rawType.isInstance(value)
                    && !DioriteReflectionUtils.getWrapperClass(rawType).isInstance(value)) {
                throw new IllegalArgumentException("Invalid object type: " + value + " in template property: "
                        + this.template.getName() + " (" + this.template.getGenericType() + ")");
            }
        } else {
            // keep collections not null if possible.
            try {
                if (Collection.class.isAssignableFrom(rawType)
                        || (Map.class.isAssignableFrom(rawType) && !Config.class.isAssignableFrom(rawType))) {
                    this.rawValue = YamlCollectionCreator.createCollection(rawType, 10);
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else if (value instanceof Number) {
        Number num = (Number) value;
        if (primitiveRawType == byte.class) {
            num = num.byteValue();
        }
        if (primitiveRawType == short.class) {
            num = num.shortValue();
        }
        if (primitiveRawType == int.class) {
            num = num.intValue();
        }
        if (primitiveRawType == long.class) {
            num = num.longValue();
        }
        if (primitiveRawType == float.class) {
            num = num.floatValue();
        }
        if (primitiveRawType == double.class) {
            num = num.doubleValue();
        }
        this.rawValue = (T) num;
        return;
    } else {
        throw new IllegalArgumentException("Invalid object type: " + value + " in template property: "
                + this.template.getName() + " (" + this.template.getGenericType() + ")");
    }
    this.rawValue = value;
}

From source file:org.kalypso.observation.result.TupleResultUtilities.java

public static void setNumberValue(final IRecord record, final IComponent component, final Number value) {
    final QName qname = component.getValueTypeName();
    if (XmlTypes.XS_DECIMAL.equals(qname))
        record.setValue(component, BigDecimal.valueOf(value.doubleValue()));
    else if (XmlTypes.XS_DOUBLE.equals(qname))
        record.setValue(component, Double.valueOf(value.doubleValue()));
    else if (XmlTypes.XS_FLOAT.equals(qname))
        record.setValue(component, Float.valueOf(value.floatValue()));
    else if (XmlTypes.XS_INT.equals(qname))
        record.setValue(component, Integer.valueOf(value.intValue()));
    else if (XmlTypes.XS_INTEGER.equals(qname))
        record.setValue(component, BigInteger.valueOf(value.longValue()));
    else if (XmlTypes.XS_LONG.equals(qname))
        record.setValue(component, Long.valueOf(value.longValue()));
    else if (XmlTypes.XS_SHORT.equals(qname))
        record.setValue(component, Short.valueOf(value.shortValue()));
    else/*from  ww  w.  j a  va 2 s  . c  o  m*/
        throw new UnsupportedOperationException();

}

From source file:net.sf.jasperreports.engine.data.JRAbstractTextDataSource.java

protected Object convertNumber(Number number, Class<?> valueClass) throws JRException {
    Number value = null;/*  w w  w  . ja v  a  2 s.  co m*/
    if (valueClass.equals(Byte.class)) {
        value = number.byteValue();
    } else if (valueClass.equals(Short.class)) {
        value = number.shortValue();
    } else if (valueClass.equals(Integer.class)) {
        value = number.intValue();
    } else if (valueClass.equals(Long.class)) {
        value = number.longValue();
    } else if (valueClass.equals(Float.class)) {
        value = number.floatValue();
    } else if (valueClass.equals(Double.class)) {
        value = 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(EXCEPTION_MESSAGE_KEY_UNKNOWN_NUMBER_TYPE, new Object[] { valueClass.getName() });
    }
    return value;
}

From source file:org.apache.tajo.storage.json.JsonLineDeserializer.java

/**
 *
 *
 * @param object/*w w w  . j  av a 2 s . co m*/
 * @param pathElements
 * @param depth
 * @param fieldIndex
 * @param output
 * @throws IOException
 */
private void getValue(JSONObject object, String fullPath, String[] pathElements, int depth, int fieldIndex,
        Tuple output) throws IOException {
    String fieldName = pathElements[depth];

    if (!object.containsKey(fieldName)) {
        output.put(fieldIndex, NullDatum.get());
    }

    switch (types.get(fullPath)) {
    case BOOLEAN:
        String boolStr = object.getAsString(fieldName);
        if (boolStr != null) {
            output.put(fieldIndex, DatumFactory.createBool(boolStr.equals("true")));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case CHAR:
        String charStr = object.getAsString(fieldName);
        if (charStr != null) {
            output.put(fieldIndex, DatumFactory.createChar(charStr));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case INT1:
    case INT2:
        Number int2Num = object.getAsNumber(fieldName);
        if (int2Num != null) {
            output.put(fieldIndex, DatumFactory.createInt2(int2Num.shortValue()));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case INT4:
        Number int4Num = object.getAsNumber(fieldName);
        if (int4Num != null) {
            output.put(fieldIndex, DatumFactory.createInt4(int4Num.intValue()));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case INT8:
        Number int8Num = object.getAsNumber(fieldName);
        if (int8Num != null) {
            output.put(fieldIndex, DatumFactory.createInt8(int8Num.longValue()));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case FLOAT4:
        Number float4Num = object.getAsNumber(fieldName);
        if (float4Num != null) {
            output.put(fieldIndex, DatumFactory.createFloat4(float4Num.floatValue()));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case FLOAT8:
        Number float8Num = object.getAsNumber(fieldName);
        if (float8Num != null) {
            output.put(fieldIndex, DatumFactory.createFloat8(float8Num.doubleValue()));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case TEXT:
        String textStr = object.getAsString(fieldName);
        if (textStr != null) {
            output.put(fieldIndex, DatumFactory.createText(textStr));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case TIMESTAMP:
        String timestampStr = object.getAsString(fieldName);
        if (timestampStr != null) {
            output.put(fieldIndex, DatumFactory.createTimestamp(timestampStr, timezone));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case TIME:
        String timeStr = object.getAsString(fieldName);
        if (timeStr != null) {
            output.put(fieldIndex, DatumFactory.createTime(timeStr));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case DATE:
        String dateStr = object.getAsString(fieldName);
        if (dateStr != null) {
            output.put(fieldIndex, DatumFactory.createDate(dateStr));
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;
    case BIT:
    case BINARY:
    case VARBINARY:
    case BLOB: {
        Object jsonObject = object.getAsString(fieldName);

        if (jsonObject == null) {
            output.put(fieldIndex, NullDatum.get());
            break;
        }

        output.put(fieldIndex, DatumFactory.createBlob(Base64.decodeBase64((String) jsonObject)));
        break;
    }

    case RECORD:
        JSONObject nestedObject = (JSONObject) object.get(fieldName);
        if (nestedObject != null) {
            getValue(nestedObject, fullPath + "/" + pathElements[depth + 1], pathElements, depth + 1,
                    fieldIndex, output);
        } else {
            output.put(fieldIndex, NullDatum.get());
        }
        break;

    case NULL_TYPE:
        output.put(fieldIndex, NullDatum.get());
        break;

    default:
        throw new TajoRuntimeException(
                new NotImplementedException("" + types.get(fullPath).name() + " for json"));
    }
}