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.krawler.br.nodes.Activity.java

/**
 * converts the given object value to the given primitive type's wrapper class
 * if possible (if it is a <tt>Number</tt>)
 *
 * @param type the premitive type name, may be:
 * <ul>/*from w ww  .j  a va  2  s. c o  m*/
 * <li><tt>byte</tt>
 * <li><tt>char</tt>
 * <li><tt>short</tt>
 * <li><tt>int</tt>
 * <li><tt>long</tt>
 * <li><tt>float</tt>
 * <li><tt>double</tt>
 * </ul>
 * @param value the original value
 * @return the converted value
 */
private Object convert(String type, Object value) {
    Object val = value;

    if (value instanceof Number) {
        Number num = (Number) value;

        if (Byte.class.getCanonicalName().equals(type))
            val = new Byte(num.byteValue());
        else if (Character.class.getCanonicalName().equals(type))
            val = new Character((char) num.intValue());
        else if (Short.class.getCanonicalName().equals(type))
            val = new Short(num.shortValue());
        else if (Integer.class.getCanonicalName().equals(type))
            val = new Integer(num.intValue());
        else if (Long.class.getCanonicalName().equals(type))
            val = new Long(num.longValue());
        else if (Float.class.getCanonicalName().equals(type))
            val = new Float(num.floatValue());
        else if (Double.class.getCanonicalName().equals(type))
            val = new Double(num.doubleValue());
    }
    return val;
}

From source file:org.qi4j.runtime.types.NumberType.java

public Object fromJSON(Object json, Module module) {
    if (json == JSONObject.NULL) {
        return null;
    }/*from www  .  jav  a  2 s . c  om*/
    if (type.isClass(BigDecimal.class)) {
        return new BigDecimal("" + json);
    } else if (type.isClass(BigInteger.class)) {
        return new BigInteger("" + json);
    }
    Number number = (Number) json;

    if (type.isClass(Integer.class)) {
        return number.intValue();
    } else if (type.isClass(Long.class)) {
        return number.longValue();
    } else if (type.isClass(Double.class)) {
        return number.doubleValue();
    } else if (type.isClass(Float.class)) {
        return number.floatValue();
    } else if (type.isClass(Short.class)) {
        return number.shortValue();
    } else if (type.isClass(Byte.class)) {
        return number.byteValue();
    }

    throw new IllegalStateException("Unknown number type:" + type);
}

From source file:org.apache.sqoop.mapreduce.hcat.SqoopHCatExportHelper.java

private Object convertNumberTypes(Object val, String javaColType) {
    Number n = (Number) val;
    if (javaColType.equals(BYTE_TYPE)) {
        return n.byteValue();
    } else if (javaColType.equals(SHORT_TYPE)) {
        return n.shortValue();
    } else if (javaColType.equals(INTEGER_TYPE)) {
        return n.intValue();
    } else if (javaColType.equals(LONG_TYPE)) {
        return n.longValue();
    } else if (javaColType.equals(FLOAT_TYPE)) {
        return n.floatValue();
    } else if (javaColType.equals(DOUBLE_TYPE)) {
        return n.doubleValue();
    } else if (javaColType.equals(BIG_DECIMAL_TYPE)) {
        return new BigDecimal(n.doubleValue());
    } else if (javaColType.equals(BOOLEAN_TYPE)) {
        return n.byteValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
    } else if (javaColType.equals(STRING_TYPE)) {
        return n.toString();
    }//from w ww  . java 2 s  .  c  o  m
    return null;
}

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  w w w  . jav a 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:net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJInt16Editor.java

public Short getInt16Value() {
    final String S_ProcName = "getInt16Value";
    Short retval;// w ww .ja  v a 2 s. c  om
    String text = getText();
    if ((text == null) || (text.length() <= 0)) {
        retval = null;
    } else {
        if (!isEditValid()) {
            throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName,
                    "Field is not valid");
        }
        try {
            commitEdit();
        } catch (ParseException e) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Field is not valid - " + e.getMessage(), e);

        }
        Object obj = getValue();
        if (obj == null) {
            retval = null;
        } else if (obj instanceof Short) {
            Short s = (Short) obj;
            short v = s.shortValue();
            if ((v < getMinValue()) || (v > getMaxValue())) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, getMinValue(), getMaxValue());
            }
            retval = (Short) s;
        } else if (obj instanceof Integer) {
            Integer i = (Integer) obj;
            int v = i.intValue();
            if ((v < getMinValue()) || (v > getMaxValue())) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, getMinValue(), getMaxValue());
            }
            retval = new Short(i.shortValue());
        } else if (obj instanceof Long) {
            Long l = (Long) obj;
            long v = l.longValue();
            if ((v < getMinValue()) || (v > getMaxValue())) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, getMinValue(), getMaxValue());
            }
            retval = new Short(l.shortValue());
        } else if (obj instanceof Number) {
            Number n = (Number) obj;
            long v = n.longValue();
            if ((v < getMinValue()) || (v > getMaxValue())) {
                throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                        "EditedValue", v, getMinValue(), getMaxValue());
            }
            retval = new Short(n.shortValue());
        } else {
            throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                    "EditedValue", obj, "Short, Integer, Long, or Number");
        }
    }
    return (retval);
}

From source file:org.apache.sqoop.mapreduce.hcat.SqoopHCatImportHelper.java

private Object convertNumberTypes(Object val, HCatFieldSchema hfs) {
    HCatFieldSchema.Type hfsType = hfs.getType();

    if (!(val instanceof Number)) {
        return null;
    }// w  w  w.  j a v  a  2 s  . c  o  m
    if (val instanceof BigDecimal && hfsType == HCatFieldSchema.Type.STRING
            || hfsType == HCatFieldSchema.Type.VARCHAR || hfsType == HCatFieldSchema.Type.CHAR) {
        BigDecimal bd = (BigDecimal) val;
        String bdStr = null;
        if (bigDecimalFormatString) {
            bdStr = bd.toPlainString();
        } else {
            bdStr = bd.toString();
        }
        if (hfsType == HCatFieldSchema.Type.VARCHAR) {
            VarcharTypeInfo vti = (VarcharTypeInfo) hfs.getTypeInfo();
            HiveVarchar hvc = new HiveVarchar(bdStr, vti.getLength());
            return hvc;
        } else if (hfsType == HCatFieldSchema.Type.VARCHAR) {
            CharTypeInfo cti = (CharTypeInfo) hfs.getTypeInfo();
            HiveChar hChar = new HiveChar(bdStr, cti.getLength());
            return hChar;
        } else {
            return bdStr;
        }
    }
    Number n = (Number) val;
    if (hfsType == HCatFieldSchema.Type.TINYINT) {
        return n.byteValue();
    } else if (hfsType == HCatFieldSchema.Type.SMALLINT) {
        return n.shortValue();
    } else if (hfsType == HCatFieldSchema.Type.INT) {
        return n.intValue();
    } else if (hfsType == HCatFieldSchema.Type.BIGINT) {
        return n.longValue();
    } else if (hfsType == HCatFieldSchema.Type.FLOAT) {
        return n.floatValue();
    } else if (hfsType == HCatFieldSchema.Type.DOUBLE) {
        return n.doubleValue();
    } else if (hfsType == HCatFieldSchema.Type.BOOLEAN) {
        return n.byteValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
    } else if (hfsType == HCatFieldSchema.Type.STRING) {
        return n.toString();
    } else if (hfsType == HCatFieldSchema.Type.VARCHAR) {
        VarcharTypeInfo vti = (VarcharTypeInfo) hfs.getTypeInfo();
        HiveVarchar hvc = new HiveVarchar(val.toString(), vti.getLength());
        return hvc;
    } else if (hfsType == HCatFieldSchema.Type.CHAR) {
        CharTypeInfo cti = (CharTypeInfo) hfs.getTypeInfo();
        HiveChar hChar = new HiveChar(val.toString(), cti.getLength());
        return hChar;
    } else if (hfsType == HCatFieldSchema.Type.DECIMAL) {
        BigDecimal bd = new BigDecimal(n.doubleValue(), MathContext.DECIMAL128);
        return HiveDecimal.create(bd);
    }
    return null;
}

From source file:cc.aileron.accessor.TypeConvertorImpl.java

/**
 * default constractor/*from  w ww.j  av  a 2 s. com*/
 */
@Inject
public TypeConvertorImpl() {
    map.put(Boolean.TYPE, new C() {
        @Override
        public Boolean convert(final java.lang.Number number) {
            return number.intValue() != 0;
        }
    });
    map.put(Byte.TYPE, new C() {

        @Override
        public Byte convert(final java.lang.Number number) {
            return number.byteValue();
        }
    });
    map.put(Short.TYPE, new C() {

        @Override
        public Short convert(final java.lang.Number number) {
            return number.shortValue();
        }
    });
    map.put(Integer.TYPE, new C() {
        @Override
        public Integer convert(final java.lang.Number number) {
            return number.intValue();
        }

    });
    map.put(Long.TYPE, new C() {

        @Override
        public Long convert(final java.lang.Number number) {
            return number.longValue();
        }
    });
    map.put(Float.TYPE, new C() {

        @Override
        public Float convert(final java.lang.Number number) {
            return number.floatValue();
        }
    });
    map.put(Double.TYPE, new C() {

        @Override
        public Double convert(final java.lang.Number number) {
            return number.doubleValue();
        }
    });
}

From source file:de.instantouch.model.io.SnakeJSONReader.java

public void read(SnakeType type, Object value) throws SnakeModelException, IOException, InstantiationException,
        IllegalAccessException, ClassNotFoundException {

    if (type.hasReference()) {
        SnakeReference ref = type.getReference();
        if (ref == null) {
            throw new SnakeNotInitializedException("reference is not initialized");
        }//from w  w  w.  j a v  a  2 s .c om

        SnakeType key = ref.getKey();
        if (key == null) {
            throw new SnakeNotInitializedException("no reference key container os given");
        }

        read(key, value);
    } else {
        if (type instanceof SnakeEntity) {
            readEntity((SnakeEntity) type, value);
        } else if (type instanceof SnakeList) {
            readList((SnakeList) type, value);
        } else if (type instanceof SnakeMap) {
            readMap((SnakeMap) type, value);
        } else if (value == null) {
            type.setNull();
        } else if (value instanceof String) {
            type.set(value.toString());
        } else if (value instanceof Boolean) {
            type.set(value.toString());
        } else if (value instanceof Number) {
            Number number = (Number) value;

            if (value instanceof Double) {
                type.set(number.doubleValue());
            } else if (value instanceof Float) {
                type.set(number.floatValue());
            } else if (value instanceof Byte) {
                type.set(number.byteValue());
            } else if (value instanceof Short) {
                type.set(number.shortValue());
            } else if (value instanceof Integer) {
                type.set(number.intValue());
            } else if (value instanceof Long) {
                type.set(number.longValue());
            }
        }
    }

}

From source file:org.mrgeo.mapalgebra.parser.jexl.JexlParserAdapter.java

@Override
public Object evaluate(ParserNode node) throws ParserException {
    JexlNode nativeNode = (JexlNode) node.getNativeNode();
    if (nativeNode instanceof ASTAssignment) {
        JexlNode valueNode = nativeNode.jjtGetChild(1);
        return valueNode.jjtGetValue();
    } else if (nativeNode instanceof ASTNumberLiteral) {
        return ((ASTNumberLiteral) nativeNode).getLiteral();
    } else if (nativeNode instanceof ASTStringLiteral) {
        return ((ASTStringLiteral) nativeNode).getLiteral();
    } else if ((nativeNode instanceof ASTUnaryMinusNode)
            && (nativeNode.jjtGetChild(0) instanceof ASTNumberLiteral)) {
        Number oldNum = ((ASTNumberLiteral) nativeNode.jjtGetChild(0)).getLiteral();
        if (oldNum instanceof Integer) {
            return -oldNum.intValue();
        } else if (oldNum instanceof Double) {
            return -oldNum.doubleValue();
        } else if (oldNum instanceof Float) {
            return (float) (0.0 - oldNum.floatValue());
        } else if (oldNum instanceof Short) {
            return -oldNum.shortValue();
        } else if (oldNum instanceof Long) {
            return -oldNum.longValue();
        } else if (oldNum instanceof Byte) {
            return -oldNum.byteValue();
        }/*from w  ww. j a v a  2s  . c o m*/
    }
    return ((JexlNode) node.getNativeNode()).jjtGetValue();
}

From source file:org.jasper.collectionspace.smk.datasource.JsonCSDataSource.java

protected Object convertNumber(Number number, Class<?> valueClass) throws JRException {
    Number value = null;//from  w w  w.j  ava  2s. co m
    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 = Integer.valueOf(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;
}