List of usage examples for java.lang Number intValue
public abstract int intValue();
From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java
@SuppressWarnings("squid:S3776") private static Object convertPrimitiveValue(String value, Class<?> type) throws ParseException { if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) { return value.toLowerCase().trim().equals("true"); } else {/*from ww w . j ava 2 s . c o m*/ NumberFormat numberFormat = NumberFormat.getNumberInstance(); Number num = numberFormat.parse(value); if (type.equals(Byte.class) || type.equals(Byte.TYPE)) { return num.byteValue(); } else if (type.equals(Double.class) || type.equals(Double.TYPE)) { return num.doubleValue(); } else if (type.equals(Float.class) || type.equals(Float.TYPE)) { return num.floatValue(); } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) { return num.intValue(); } else if (type.equals(Long.class) || type.equals(Long.TYPE)) { return num.longValue(); } else if (type.equals(Short.class) || type.equals(Short.TYPE)) { return num.shortValue(); } else { return null; } } }
From source file:Main.java
/** * convert value to given type.// w ww. j a v a2 s . co m * null safe. * * @param value value for convert * @param type will converted type * @return value while converted */ public static Object convertCompatibleType(Object value, Class<?> type) { if (value == null || type == null || type.isAssignableFrom(value.getClass())) { return value; } if (value instanceof String) { String string = (String) value; if (char.class.equals(type) || Character.class.equals(type)) { if (string.length() != 1) { throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!" + " when convert String to char, the String MUST only 1 char.", string)); } return string.charAt(0); } else if (type.isEnum()) { return Enum.valueOf((Class<Enum>) type, string); } else if (type == BigInteger.class) { return new BigInteger(string); } else if (type == BigDecimal.class) { return new BigDecimal(string); } else if (type == Short.class || type == short.class) { return Short.valueOf(string); } else if (type == Integer.class || type == int.class) { return Integer.valueOf(string); } else if (type == Long.class || type == long.class) { return Long.valueOf(string); } else if (type == Double.class || type == double.class) { return Double.valueOf(string); } else if (type == Float.class || type == float.class) { return Float.valueOf(string); } else if (type == Byte.class || type == byte.class) { return Byte.valueOf(string); } else if (type == Boolean.class || type == boolean.class) { return Boolean.valueOf(string); } else if (type == Date.class) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) value); } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } } else if (type == Class.class) { return forName((String) value); } } else if (value instanceof Number) { Number number = (Number) value; if (type == byte.class || type == Byte.class) { return number.byteValue(); } else if (type == short.class || type == Short.class) { return number.shortValue(); } else if (type == int.class || type == Integer.class) { return number.intValue(); } else if (type == long.class || type == Long.class) { return number.longValue(); } else if (type == float.class || type == Float.class) { return number.floatValue(); } else if (type == double.class || type == Double.class) { return number.doubleValue(); } else if (type == BigInteger.class) { return BigInteger.valueOf(number.longValue()); } else if (type == BigDecimal.class) { return BigDecimal.valueOf(number.doubleValue()); } else if (type == Date.class) { return new Date(number.longValue()); } } else if (value instanceof Collection) { Collection collection = (Collection) value; if (type.isArray()) { int length = collection.size(); Object array = Array.newInstance(type.getComponentType(), length); int i = 0; for (Object item : collection) { Array.set(array, i++, item); } return array; } else if (!type.isInterface()) { try { Collection result = (Collection) type.newInstance(); result.addAll(collection); return result; } catch (Throwable e) { e.printStackTrace(); } } else if (type == List.class) { return new ArrayList<>(collection); } else if (type == Set.class) { return new HashSet<>(collection); } } else if (value.getClass().isArray() && Collection.class.isAssignableFrom(type)) { Collection collection; if (!type.isInterface()) { try { collection = (Collection) type.newInstance(); } catch (Throwable e) { collection = new ArrayList<>(); } } else if (type == Set.class) { collection = new HashSet<>(); } else { collection = new ArrayList<>(); } int length = Array.getLength(value); for (int i = 0; i < length; i++) { collection.add(Array.get(value, i)); } return collection; } return value; }
From source file:com.pureinfo.srm.reports.table.MyTabeleDataHelper.java
public static Map getRowMap(Class _contentClass, SQLCondition _condition, String _sGroupProp, String[] _caredValues) throws PureException { IContentMgr mgr = ArkContentHelper.getContentMgrOf(_contentClass); List params = new ArrayList(); IObjects datas = null;/* w ww. jav a 2 s .c o m*/ IStatement query = null; Map row = null; try { String sCondion = ""; if (_condition != null) sCondion = _condition.toSQL(params); String strSQL = "SELECT COUNT(*) AS _COUNT, {this." + _sGroupProp + "} FROM {this}" + (sCondion == null || sCondion.trim().length() < 1 ? "" : " WHERE ") + sCondion + " GROUP BY {this." + _sGroupProp + '}'; query = mgr.createQuery(strSQL, 0); if (!params.isEmpty()) { query.setParameters(0, params); } datas = query.executeQuery(); row = new HashMap(datas.getSize()); do { DolphinObject data = datas.next(); if (data == null) { break; } row.put(data.getProperty(_sGroupProp), data.getProperty("_COUNT")); } while (true); } finally { params.clear(); DolphinHelper.clear(datas, query); } if (_caredValues != null) { int caredTotal = 0; int total = 0; for (Iterator iter = row.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); String sName = (String) entry.getKey(); Number num = (Number) entry.getValue(); if (ArrayUtils.contains(_caredValues, sName)) { caredTotal += num.intValue(); } total += num.intValue(); } row.put(OTHER, new Integer(total - caredTotal)); row.put(TOTAL, new Integer(total)); } return row; }
From source file:com.jeeframework.util.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to an int primitive. * *@param value The value validation is being performed on. *@param locale The locale to use to parse the number (system default if * null)//w ww . j av a 2 s . co m *@return the converted Integer value. */ public static Integer formatInt(String value, Locale locale) { Integer result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) { result = new Integer(num.intValue()); } } } return result; }
From source file:com.swordlord.gozer.datatypeformat.DataTypeHelper.java
/** * @param dataTypeClass//from w w w .j a v a 2s.co m * @param untypedValue * @return */ public static Object toDataType(Class<?> dataTypeClass, Object untypedValue) { if ((dataTypeClass == null) || (untypedValue == null) || ClassUtils.isAssignable(untypedValue.getClass(), dataTypeClass)) { if (Date.class == dataTypeClass) { return DateUtils.truncate(untypedValue, Calendar.DATE); } return untypedValue; } Object v = null; String strUntypedValue = null; boolean isStringUntypedValue = untypedValue instanceof String; Number numUntypedValue = null; boolean isNumberUntypedValue = untypedValue instanceof Number; if (isStringUntypedValue) { strUntypedValue = (String) untypedValue; } if (isNumberUntypedValue) { numUntypedValue = (Number) untypedValue; } if (dataTypeClass == boolean.class || dataTypeClass == Boolean.class) { if (isNumberUntypedValue) { v = BooleanUtils.toBooleanObject(numUntypedValue.intValue()); } else if (isStringUntypedValue) { v = BooleanUtils.toBooleanObject(strUntypedValue); } } else if (dataTypeClass == Integer.class) { if (isNumberUntypedValue) { v = new Integer(numUntypedValue.intValue()); } else if (isStringUntypedValue) { v = NumberUtils.createInteger(strUntypedValue); } } else if (dataTypeClass == Double.class) { if (isNumberUntypedValue) { v = new Double(numUntypedValue.doubleValue()); } else if (isStringUntypedValue) { v = NumberUtils.createDouble(strUntypedValue); } } else if (dataTypeClass == Date.class) { if (isNumberUntypedValue) { v = DateUtils.truncate(new Date(numUntypedValue.longValue()), Calendar.DATE); } } else { v = ObjectUtils.toString(untypedValue); } return v; }
From source file:com.pureinfo.srm.reports.table.MyTabeleDataHelper.java
/** * @param _contentClass content classes that take parts in do statistic; has at least 1 element * @param _alias alias for content classes,the fisrt one will be set to 'this' no matter what is passed in, * must have the same size as <code>_contentClass</code> * @param _sToCalculate to calculate what? this will be set in SELECT clause,and its value will be contained * in the data map returned//w w w. ja va 2 s. co m * @param _condition conditions, that will be used in WHERE clause * @param _sGroupProp group by this * @param _caredValues what values are cared; if result is not in this array,it will be considered as "OTHER" * @return an map.contains <code>contentClass[0]._sGroupProp</code>->its result(a <code>java.lang.Number</code>) * @throws PureException if database access error or class metadata not found */ public static Map getRowMap(Class[] _contentClass, String[] _alias, String _sToCalculate, SQLCondition _condition, String _sGroupProp, String[] _caredValues) throws PureException { IContentMgr mgr = ArkContentHelper.getContentMgrOf(_contentClass[0]); List params = new ArrayList(); IObjects datas = null; IStatement query = null; Map row = null; if (_sToCalculate == null) _sToCalculate = "count( {this.*} )"; try { String sCondion = ""; if (_condition != null) sCondion = _condition.toSQL(params); String strSQL = "SELECT " + _sToCalculate + " AS _COUNT, {this." + _sGroupProp + "} FROM {this}"; for (int i = 1; i < _alias.length; i++) { strSQL += (",{" + _alias[i] + '}'); } strSQL += (sCondion == null || sCondion.trim().length() < 1 ? "" : " WHERE ") + sCondion + " GROUP BY {this." + _sGroupProp + '}'; query = mgr.createQuery(strSQL, 0); for (int i = 1; i < _contentClass.length; i++) { query.registerAlias(_alias[i], _contentClass[i]); } if (!params.isEmpty()) { query.setParameters(0, params); } datas = query.executeQuery(); row = new HashMap(datas.getSize()); do { DolphinObject data = datas.next(); if (data == null) { break; } row.put(data.getProperty(_sGroupProp), data.getProperty("_COUNT")); } while (true); } finally { params.clear(); DolphinHelper.clear(datas, query); } int caredTotal = 0; int total = 0; for (Iterator iter = row.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); String sName = (String) entry.getKey(); Number num = (Number) entry.getValue(); if (_caredValues != null) { if (ArrayUtils.contains(_caredValues, sName)) { caredTotal += num.intValue(); } } total += num.intValue(); } row.put(OTHER, new Integer(total - caredTotal)); row.put(TOTAL, new Integer(total)); return row; }
From source file:gedi.util.FileUtils.java
public static void writeNumber(BinaryWriter out, Number d) throws IOException { if (d instanceof Byte) out.putByte(d.intValue()); else if (d instanceof Short) out.putShort(d.shortValue());/*from w w w . j a va2 s . c o m*/ else if (d instanceof Integer) out.putInt(d.intValue()); else if (d instanceof Long) out.putLong(d.longValue()); else if (d instanceof Float) out.putFloat(d.floatValue()); else out.putDouble(d.doubleValue()); }
From source file:easyJ.common.validate.GenericTypeValidator.java
/** * Checks if the value can safely be converted to an int primitive. * // www .j av a2s .co m * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if * null) * @return the converted Integer value. */ public static Integer formatInt(String value, Locale locale) { Integer result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) { result = new Integer(num.intValue()); } } } return result; }
From source file:com.shenit.commons.utils.AvatarUtils.java
/** * Get Avataer url/*from w w w .j av a 2 s .co m*/ * * @param uid * @param width * @param height * @param prefix * @param type * @return */ public static String getAvatarURI(Number uid, Number width, Number height, String prefix, AvatarIdTypeEnum type) { if (ValidationUtils.all(ValidationUtils.NULL, uid, width, height, prefix)) return null; long uidl = uid.longValue(); String hash = null; String path, name; if (type == AvatarIdTypeEnum.YY) { hash = String.valueOf(hashUid(String.valueOf(uidl), false)); path = name = String.valueOf(uid); } else { String hashId = DigestUtils.md5Hex(String.valueOf(uidl)); hash = String.valueOf(hashUid(hashId, true)); path = hashId.substring(0, 2); name = hashId.substring(2); } return String.format(AVATAR_FORMAT, prefix, hash, path, name, width.intValue(), height.intValue()); }
From source file:com.sunchenbin.store.feilong.core.lang.NumberUtil.java
/** * .//from w ww.j a v a 2 s.c o m * * <pre> * {@code * Example 1: * NumberUtil.getProgress(5, 5, "##%") * return 100% * * Example 2: * NumberUtil.getProgress(2, 3, "#0.0%") * return 66.7% * } * </pre> * * @param current * ?? * @param total * ? * @param numberPattern * the number pattern {@link NumberPattern} * @return ?numberPattern 50.5%,100%..... * @see NumberPattern * @since 1.0.7 */ public static String getProgress(Number current, Number total, String numberPattern) { if (null == current) { throw new NullPointerException("current is null"); } if (null == total) { throw new NullPointerException("total is null"); } if (current.intValue() <= 0) { throw new IllegalArgumentException("current can not <=0"); } if (total.intValue() <= 0) { throw new IllegalArgumentException("total can not <=0"); } if (current.doubleValue() > total.doubleValue()) { throw new IllegalArgumentException("current can not > total"); } // XXX int scale = 8; BigDecimal bigDecimalCurrent = ConvertUtil.toBigDecimal(current); BigDecimal divideValue = getDivideValue(bigDecimalCurrent, total, scale); return toString(divideValue, numberPattern); }