List of usage examples for java.lang Number intValue
public abstract int intValue();
From source file:net.sf.jasperreports.functions.standard.MathFunctions.java
/** * Checks if a number is odd. If a non-integer number is specified, any digits after the decimal point are ignored. *//*w w w. ja v a2 s . c om*/ @Function("ISODD") @FunctionParameters({ @FunctionParameter("number") }) public static Boolean ISODD(Number number) { if (number == null) { logNullArgument(); return null; } else { return number.intValue() % 2 == 1; } }
From source file:nz.co.senanque.validationengine.ConvertUtils.java
public static Comparable<?> convertTo(Class<?> clazz, Object obj) { if (obj == null) { return null; }/*from w ww.j a v a2 s.c o m*/ if (clazz.isAssignableFrom(obj.getClass())) { return (Comparable<?>) obj; } if (clazz.isPrimitive()) { if (clazz.equals(Long.TYPE)) { clazz = Long.class; } else if (clazz.equals(Integer.TYPE)) { clazz = Integer.class; } else if (clazz.equals(Float.TYPE)) { clazz = Float.class; } else if (clazz.equals(Double.TYPE)) { clazz = Double.class; } else if (clazz.equals(Boolean.TYPE)) { clazz = Boolean.class; } } if (Number.class.isAssignableFrom(clazz)) { if (obj.getClass().equals(String.class)) { obj = new Double((String) obj); } if (!Number.class.isAssignableFrom(obj.getClass())) { throw new RuntimeException( "Cannot convert from " + obj.getClass().getName() + " to " + clazz.getName()); } Number number = (Number) obj; if (clazz.equals(Long.class)) { return new Long(number.longValue()); } if (clazz.equals(Integer.class)) { return new Integer(number.intValue()); } if (clazz.equals(Float.class)) { return new Float(number.floatValue()); } if (clazz.equals(Double.class)) { return new Double(number.doubleValue()); } if (clazz.equals(BigDecimal.class)) { return new BigDecimal(number.doubleValue()); } } final String oStr = String.valueOf(obj); if (clazz.equals(String.class)) { return oStr; } if (clazz.equals(java.util.Date.class)) { return java.sql.Date.valueOf(oStr); } if (clazz.equals(java.sql.Date.class)) { return java.sql.Date.valueOf(oStr); } if (clazz.equals(Boolean.class)) { return new Boolean(oStr); } throw new RuntimeException("Cannot convert from " + obj.getClass().getName() + " to " + clazz.getName()); }
From source file:com.ning.metrics.serialization.event.SmileEnvelopeEvent.java
private static void addToTree(ObjectNode root, String name, Object value) { /* could wrap everything as POJONode, but that's bit inefficient; * so let's handle some known cases. * (in reality, I doubt there could ever be non-scalars, FWIW, since * downstream systems expect simple key/value data) *///from w w w . j av a2s. co m if (value instanceof String) { root.put(name, (String) value); return; } if (value instanceof Number) { Number num = (Number) value; if (value instanceof Integer) { root.put(name, num.intValue()); } else if (value instanceof Long) { root.put(name, num.longValue()); } else if (value instanceof Double) { root.put(name, num.doubleValue()); } else { root.putPOJO(name, num); } } else if (value == Boolean.TRUE) { root.put(name, true); } else if (value == Boolean.FALSE) { root.put(name, false); } else { // most likely Date root.putPOJO(name, value); } }
From source file:io.fabric8.jolokia.support.JolokiaHelpers.java
public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException { if (clazz.isArray()) { if (value instanceof JSONArray) { JSONArray jsonArray = (JSONArray) value; Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size()); int idx = 0; for (Object element : jsonArray) { Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element)); }//from w w w. j a va 2 s .com return javaArray; } else { return null; } } else if (String.class.equals(clazz)) { return (value != null) ? value.toString() : null; } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { Number number = asNumber(value); return number != null ? number.byteValue() : null; } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { Number number = asNumber(value); return number != null ? number.shortValue() : null; } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { Number number = asNumber(value); return number != null ? number.intValue() : null; } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { Number number = asNumber(value); return number != null ? number.longValue() : null; } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { Number number = asNumber(value); return number != null ? number.floatValue() : null; } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { Number number = asNumber(value); return number != null ? number.doubleValue() : null; } else if (value instanceof JSONObject) { JSONObject jsonObject = (JSONObject) value; if (!JSONObject.class.isAssignableFrom(clazz)) { String json = jsonObject.toJSONString(); return getObjectMapper().readerFor(clazz).readValue(json); } } return value; }
From source file:com.healthmarketscience.jackcess.query.QueryTest.java
private static Row newRow(Byte attr, String expr, Number flagNum, Number extraNum, String name1, String name2) { Short flag = ((flagNum != null) ? flagNum.shortValue() : null); Integer extra = ((extraNum != null) ? extraNum.intValue() : null); return new Row(null, attr, expr, flag, extra, name1, name2, null, null); }
From source file:NumberUtils.java
/** * Converts the given number to a <code>Integer</code> (by using <code>intValue()</code>). * * @param number//from w w w .j av a 2 s. co m * @return java.lang.Integer * @throws IllegalArgumentException The given number is 'not a number' or infinite. */ public static Integer toInteger(Number number) throws IllegalArgumentException { if (number == null || number instanceof Integer) return (Integer) number; if (isNaN(number) || isInfinite(number)) throw new IllegalArgumentException("Argument must not be NaN or infinite."); return new Integer(number.intValue()); }
From source file:com.pureinfo.srm.reports.table.MyTabeleDataHelper.java
public static int getInt(Number num) { if (num != null) return num.intValue(); return 0;//from ww w . j ava 2 s.co m }
From source file:de.cebitec.readXplorer.util.GeneralUtils.java
/** * Converts a given number into a number of the given classType. If this is * not possible, it throws a ClassCastException * @param <T> one of the classes derived from Number * @param classType the type to convert the number into * @param number the number to convert/*from w ww. ja v a2 s. c o m*/ * @return The converted number */ public static <T extends Number> T convertNumber(Class<T> classType, Number number) throws ClassCastException { T convertedValue = null; if (classType.equals(Integer.class)) { convertedValue = classType.cast(number.intValue()); } else if (classType.equals(Double.class)) { convertedValue = classType.cast(number.doubleValue()); } else if (classType.equals(Long.class)) { convertedValue = classType.cast(number.longValue()); } else if (classType.equals(Float.class)) { convertedValue = classType.cast(number.floatValue()); } else if (classType.equals(Short.class)) { convertedValue = classType.cast(number.shortValue()); } else if (classType.equals(Byte.class)) { convertedValue = classType.cast(number.byteValue()); } if (convertedValue == null) { throw new ClassCastException("Cannot cast the given number into the given format."); } return convertedValue; }
From source file:com.pureinfo.srm.reports.table.MyTabeleDataHelper.java
/** * @param _sValuesCol/*from w ww . ja v a 2s. c o m*/ * @param _sRow */ public static void calculateAndSetMapRow(Map _row, String[] _sValuesCol) { if (_row == null) return; int total = 0, cared = 0; for (int i = 0; i < _sValuesCol.length; i++) { Number num = (Number) _row.get(_sValuesCol[i]); if (num != null) { cared += num.intValue(); } } for (Iterator iter = _row.values().iterator(); iter.hasNext();) { Number num = (Number) iter.next(); if (num != null) { total += num.intValue(); } } _row.put(OTHER, new Integer(total - cared)); _row.put(TOTAL, new Integer(total)); }
From source file:com.feilong.core.lang.NumberUtil.java
/** * .//from w w w. jav a 2s.c o m * * <pre class="code"> * NumberUtil.getProgress(5, 5, NumberPattern.PERCENT_WITH_NOPOINT) = 100% * NumberUtil.getProgress(2, 3, NumberPattern.PERCENT_WITH_1POINT) = 66.7% * </pre> * * @param current * ?? * @param total * ? * @param numberPattern * the number pattern {@link NumberPattern} * @return <code>current</code> null, {@link NullPointerException}<br> * <code>total</code> null, {@link NullPointerException}<br> * {@code current<=0}, {@link IllegalArgumentException}<br> * {@code total<=0}, {@link IllegalArgumentException}<br> * {@code current>total}, {@link IllegalArgumentException}<br> * @see NumberPattern * @see #getDivideValue(Number, Number, int) * @since 1.0.7 */ public static String getProgress(Number current, Number total, String numberPattern) { Validate.notNull(current, "current can't be null/empty!"); Validate.notNull(total, "total can't be null/empty!"); Validate.isTrue(current.intValue() > 0, "current can not <=0"); Validate.isTrue(total.intValue() > 0, "total can not <=0"); Validate.isTrue(current.doubleValue() <= total.doubleValue(), "current can not > total"); // XXX scale = 8? int scale = 8; BigDecimal bigDecimalCurrent = toBigDecimal(current); BigDecimal divideValue = getDivideValue(bigDecimalCurrent, total, scale); return toString(divideValue, numberPattern); }