List of usage examples for java.lang Number byteValue
public byte byteValue()
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(); }/*ww w. jav a2 s. c o m*/ } return ((JexlNode) node.getNativeNode()).jjtGetValue(); }
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 {//from ww w . ja v a2 s .c o m throw new IllegalArgumentException( "Unable to write number " + number + " of type " + number.getClass()); } }
From source file:com.jkoolcloud.tnt4j.streams.utils.Utils.java
/** * Casts provided number value to desired number type. * * @param num//from ww w .j ava 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; }
From source file:weave.servlets.WeaveServlet.java
/** * Tries to convert value to the given type. * @param value The value to cast to a new type. * @param type The desired type.//from w ww .j a v a2s . c om * @return The value, which may have been cast as the new type. */ protected Object cast(Object value, Class<?> type) throws RemoteException { if (type.isInstance(value)) return value; try { if (value == null) // null -> NaN { if (type == double.class || type == Double.class) value = Double.NaN; else if (type == float.class || type == Float.class) value = Float.NaN; return value; } if (value instanceof Map) // Map -> Java Bean { Object bean = type.newInstance(); for (Field field : type.getFields()) { Object fieldValue = ((Map<?, ?>) value).get(field.getName()); fieldValue = cast(fieldValue, field.getType()); field.set(bean, fieldValue); } return bean; } if (type.isArray()) // ? -> T[] { if (value instanceof String) // String -> String[] value = CSVParser.defaultParser.parseCSVRow((String) value, true); if (value instanceof List) // List -> Object[] value = ((List<?>) value).toArray(); if (value.getClass().isArray()) // T1[] -> T2[] { int n = Array.getLength(value); Class<?> itemType = type.getComponentType(); Object output = Array.newInstance(itemType, n); while (n-- > 0) Array.set(output, n, cast(Array.get(value, n), itemType)); return output; } } if (Collection.class.isAssignableFrom(type)) // ? -> <? extends Collection> { value = cast(value, Object[].class); // ? -> Object[] if (value.getClass().isArray()) // T1[] -> Vector<T2> { int n = Array.getLength(value); List<Object> output = new Vector<Object>(n); TypeVariable<?>[] itemTypes = type.getTypeParameters(); Class<?> itemType = itemTypes.length > 0 ? itemTypes[0].getClass() : null; while (n-- > 0) { Object item = Array.get(value, n); if (itemType != null) item = cast(item, itemType); // T1 -> T2 output.set(n, item); } return output; } } if (value instanceof String) // String -> ? { String string = (String) value; // String -> primitive if (type == char.class || type == Character.class) return string.charAt(0); if (type == byte.class || type == Byte.class) return Byte.parseByte(string); if (type == long.class || type == Long.class) return Long.parseLong(string); if (type == int.class || type == Integer.class) return Integer.parseInt(string); if (type == short.class || type == Short.class) return Short.parseShort(string); if (type == float.class || type == Float.class) return Float.parseFloat(string); if (type == double.class || type == Double.class) return Double.parseDouble(string); if (type == boolean.class || type == Boolean.class) return string.equalsIgnoreCase("true"); if (type == InputStream.class) // String -> InputStream { try { return new ByteArrayInputStream(string.getBytes("UTF-8")); } catch (Exception e) { return null; } } } if (value instanceof Number) // Number -> primitive { Number number = (Number) value; if (type == byte.class || type == Byte.class) return number.byteValue(); if (type == long.class || type == Long.class) return number.longValue(); if (type == int.class || type == Integer.class) return number.intValue(); if (type == short.class || type == Short.class) return number.shortValue(); if (type == float.class || type == Float.class) return number.floatValue(); if (type == double.class || type == Double.class) return number.doubleValue(); if (type == char.class || type == Character.class) return Character.toChars(number.intValue())[0]; if (type == boolean.class || type == Boolean.class) return !Double.isNaN(number.doubleValue()) && number.intValue() != 0; } } catch (Exception e) { throw new RemoteException(String.format("Unable to cast %s to %s", value.getClass().getSimpleName(), type.getSimpleName()), e); } // Return original value if not handled above. // Primitives and their Object equivalents will cast automatically. return value; }
From source file:com.facebook.stetho.json.ObjectMapper.java
private Object getValueForField(Field field, Object value) throws JSONException { try {/*from w ww .j ava 2 s . c o m*/ 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:org.apache.calcite.runtime.SqlFunctions.java
public static byte toByte(Number number) { return number.byteValue(); }
From source file:com.examples.with.different.packagename.testcarver.NumberConverter.java
/** * Convert any Number object to the specified type for this * <i>Converter</i>.//from w w w . j a va2 s . c om * <p> * This method handles conversion to the following types: * <ul> * <li><code>java.lang.Byte</code></li> * <li><code>java.lang.Short</code></li> * <li><code>java.lang.Integer</code></li> * <li><code>java.lang.Long</code></li> * <li><code>java.lang.Float</code></li> * <li><code>java.lang.Double</code></li> * <li><code>java.math.BigDecimal</code></li> * <li><code>java.math.BigInteger</code></li> * </ul> * @param sourceType The type being converted from * @param targetType The Number type to convert to * @param value The Number to convert. * * @return The converted value. */ private Number toNumber(Class sourceType, Class targetType, Number value) { // Correct Number type already if (targetType.equals(value.getClass())) { return value; } // Byte if (targetType.equals(Byte.class)) { long longValue = value.longValue(); if (longValue > Byte.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Byte.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Byte(value.byteValue()); } // Short if (targetType.equals(Short.class)) { long longValue = value.longValue(); if (longValue > Short.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Short.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Short(value.shortValue()); } // Integer if (targetType.equals(Integer.class)) { long longValue = value.longValue(); if (longValue > Integer.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Integer.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Integer(value.intValue()); } // Long if (targetType.equals(Long.class)) { return new Long(value.longValue()); } // Float if (targetType.equals(Float.class)) { if (value.doubleValue() > Float.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } return new Float(value.floatValue()); } // Double if (targetType.equals(Double.class)) { return new Double(value.doubleValue()); } // BigDecimal if (targetType.equals(BigDecimal.class)) { if (value instanceof Float || value instanceof Double) { return new BigDecimal(value.toString()); } else if (value instanceof BigInteger) { return new BigDecimal((BigInteger) value); } else { return BigDecimal.valueOf(value.longValue()); } } // BigInteger if (targetType.equals(BigInteger.class)) { if (value instanceof BigDecimal) { return ((BigDecimal) value).toBigInteger(); } else { return BigInteger.valueOf(value.longValue()); } } String msg = toString(getClass()) + " cannot handle conversion to '" + toString(targetType) + "'"; throw new ConversionException(msg); }
From source file:javadz.beanutils.converters.NumberConverter.java
/** * Convert any Number object to the specified type for this * <i>Converter</i>.// ww w .j a va 2 s. co m * <p> * This method handles conversion to the following types: * <ul> * <li><code>java.lang.Byte</code></li> * <li><code>java.lang.Short</code></li> * <li><code>java.lang.Integer</code></li> * <li><code>java.lang.Long</code></li> * <li><code>java.lang.Float</code></li> * <li><code>java.lang.Double</code></li> * <li><code>java.math.BigDecimal</code></li> * <li><code>java.math.BigInteger</code></li> * </ul> * @param sourceType The type being converted from * @param targetType The Number type to convert to * @param value The Number to convert. * * @return The converted value. */ private Number toNumber(Class sourceType, Class targetType, Number value) { // Correct Number type already if (targetType.equals(value.getClass())) { return value; } // Byte if (targetType.equals(Byte.class)) { long longValue = value.longValue(); if (longValue > Byte.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Byte.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Byte(value.byteValue()); } // Short if (targetType.equals(Short.class)) { long longValue = value.longValue(); if (longValue > Short.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Short.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Short(value.shortValue()); } // Integer if (targetType.equals(Integer.class)) { long longValue = value.longValue(); if (longValue > Integer.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Integer.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Integer(value.intValue()); } // Long if (targetType.equals(Long.class)) { return new Long(value.longValue()); } // Float if (targetType.equals(Float.class)) { if (value.doubleValue() > Float.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } return new Float(value.floatValue()); } // Double if (targetType.equals(Double.class)) { return new Double(value.doubleValue()); } // BigDecimal if (targetType.equals(BigDecimal.class)) { if (value instanceof Float || value instanceof Double) { return new BigDecimal(value.toString()); } else if (value instanceof BigInteger) { return new BigDecimal((BigInteger) value); } else { return BigDecimal.valueOf(value.longValue()); } } // BigInteger if (targetType.equals(BigInteger.class)) { if (value instanceof BigDecimal) { return ((BigDecimal) value).toBigInteger(); } else { return BigInteger.valueOf(value.longValue()); } } String msg = toString(getClass()) + " cannot handle conversion to '" + toString(targetType) + "'"; if (log().isWarnEnabled()) { log().warn(" " + msg); } throw new ConversionException(msg); }
From source file:com.swordlord.gozer.datatypeformat.DataTypeHelper.java
/** * Return compatible class for typedValue based on untypedValueClass * /*ww w . j av a 2 s .c o m*/ * @param untypedValueClass * @param typedValue * @return */ public static Object fromDataType(Class<?> untypedValueClass, Object typedValue) { Log LOG = LogFactory.getLog(DataTypeHelper.class); if (typedValue == null) { return null; } if (untypedValueClass == null) { return typedValue; } if (ClassUtils.isAssignable(typedValue.getClass(), untypedValueClass)) { return typedValue; } String strTypedValue = null; boolean isStringTypedValue = typedValue instanceof String; Number numTypedValue = null; boolean isNumberTypedValue = typedValue instanceof Number; Boolean boolTypedValue = null; boolean isBooleanTypedValue = typedValue instanceof Boolean; Date dateTypedValue = null; boolean isDateTypedValue = typedValue instanceof Date; if (isStringTypedValue) { strTypedValue = (String) typedValue; } if (isNumberTypedValue) { numTypedValue = (Number) typedValue; } if (isBooleanTypedValue) { boolTypedValue = (Boolean) typedValue; } if (isDateTypedValue) { dateTypedValue = (Date) typedValue; } Object v = null; if (String.class.equals(untypedValueClass)) { v = ObjectUtils.toString(typedValue); } else if (BigDecimal.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = NumberUtils.createBigDecimal(strTypedValue); } else if (isNumberTypedValue) { v = new BigDecimal(numTypedValue.doubleValue()); } else if (isBooleanTypedValue) { v = new BigDecimal(BooleanUtils.toInteger(boolTypedValue.booleanValue())); } else if (isDateTypedValue) { v = new BigDecimal(dateTypedValue.getTime()); } } else if (Boolean.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = BooleanUtils.toBooleanObject(strTypedValue); } else if (isNumberTypedValue) { v = BooleanUtils.toBooleanObject(numTypedValue.intValue()); } else if (isDateTypedValue) { v = BooleanUtils.toBooleanObject((int) dateTypedValue.getTime()); } } else if (Byte.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = Byte.valueOf(strTypedValue); } else if (isNumberTypedValue) { v = new Byte(numTypedValue.byteValue()); } else if (isBooleanTypedValue) { v = new Byte((byte) BooleanUtils.toInteger(boolTypedValue.booleanValue())); } else if (isDateTypedValue) { v = new Byte((byte) dateTypedValue.getTime()); } } else if (byte[].class.equals(untypedValueClass)) { if (isStringTypedValue) { v = strTypedValue.getBytes(); } } else if (Double.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = NumberUtils.createDouble(strTypedValue); } else if (isNumberTypedValue) { v = new Double(numTypedValue.doubleValue()); } else if (isBooleanTypedValue) { v = new Double(BooleanUtils.toInteger(boolTypedValue.booleanValue())); } else if (isDateTypedValue) { v = new Double(dateTypedValue.getTime()); } } else if (Float.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = NumberUtils.createFloat(strTypedValue); } else if (isNumberTypedValue) { v = new Float(numTypedValue.floatValue()); } else if (isBooleanTypedValue) { v = new Float(BooleanUtils.toInteger(boolTypedValue.booleanValue())); } else if (isDateTypedValue) { v = new Float(dateTypedValue.getTime()); } } else if (Short.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = NumberUtils.createInteger(strTypedValue); } else if (isNumberTypedValue) { v = new Integer(numTypedValue.intValue()); } else if (isBooleanTypedValue) { v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue()); } else if (isDateTypedValue) { v = new Integer((int) dateTypedValue.getTime()); } } else if (Integer.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = NumberUtils.createInteger(strTypedValue); } else if (isNumberTypedValue) { v = new Integer(numTypedValue.intValue()); } else if (isBooleanTypedValue) { v = BooleanUtils.toIntegerObject(boolTypedValue.booleanValue()); } else if (isDateTypedValue) { v = new Integer((int) dateTypedValue.getTime()); } } else if (Long.class.equals(untypedValueClass)) { if (isStringTypedValue) { v = NumberUtils.createLong(strTypedValue); } else if (isNumberTypedValue) { v = new Long(numTypedValue.longValue()); } else if (isBooleanTypedValue) { v = new Long(BooleanUtils.toInteger(boolTypedValue.booleanValue())); } else if (isDateTypedValue) { v = new Long(dateTypedValue.getTime()); } } else if (java.sql.Date.class.equals(untypedValueClass)) { if (isNumberTypedValue) { v = new java.sql.Date(numTypedValue.longValue()); } else if (isDateTypedValue) { v = new java.sql.Date(dateTypedValue.getTime()); } } else if (java.sql.Time.class.equals(untypedValueClass)) { if (isNumberTypedValue) { v = new java.sql.Time(numTypedValue.longValue()); } else if (isDateTypedValue) { v = new java.sql.Time(dateTypedValue.getTime()); } } else if (java.sql.Timestamp.class.equals(untypedValueClass)) { if (isNumberTypedValue) { v = new java.sql.Timestamp(numTypedValue.longValue()); } else if (isDateTypedValue) { v = new java.sql.Timestamp(dateTypedValue.getTime()); } } else if (Date.class.equals(untypedValueClass)) { if (isNumberTypedValue) { v = new Date(numTypedValue.longValue()); } else if (isStringTypedValue) { try { v = DateFormat.getDateInstance().parse(strTypedValue); } catch (ParseException e) { LOG.error("Unable to parse the date : " + strTypedValue); LOG.debug(e.getMessage()); } } } return v; }
From source file:com.xtructure.xevolution.genetics.impl.AbstractPopulation.java
/** * Adds the attribute in the given {@link Genome} with the given * {@link XValId} to the corresponding value in the accumulation map. * /* www . j ava2s . c o m*/ * @param <V> * the type of the attribute * @param valueId * the id of the attribute * @param accMap * the accumulation map * @param genome * the {@link Genome} from which to get the attribute */ private <V extends Comparable<V>> void updateAccMap(XValId<V> valueId, ValueMap accMap, Genome<D> genome) { getLogger().trace("begin %s.updateAccMap(%s, %s, %s)", getClass().getSimpleName(), valueId, accMap, genome); if (Number.class.isAssignableFrom(valueId.getType())) { if (accMap.get(valueId) == null) { accMap.set(// valueId, // genome.getAttribute(valueId)); } else { Number acc = (Number) accMap.get(valueId); Number nxt = (Number) genome.getAttribute(valueId); Number sum = null; ValueType type = ValueType.getValueType(valueId.getType()); if (type != null) { switch (type) { case DOUBLE: sum = acc.doubleValue() + nxt.doubleValue(); break; case FLOAT: sum = acc.floatValue() + nxt.floatValue(); break; case LONG: sum = acc.longValue() + nxt.longValue(); break; case INTEGER: sum = acc.intValue() + nxt.intValue(); break; case SHORT: sum = acc.shortValue() + nxt.shortValue(); break; case BYTE: sum = acc.byteValue() + nxt.byteValue(); break; default: break; } @SuppressWarnings("unchecked") V v = (V) sum; accMap.set(valueId, v); } } } getLogger().trace("end %s.updateAccMap()", getClass().getSimpleName()); }