Example usage for java.lang Number intValue

List of usage examples for java.lang Number intValue

Introduction

In this page you can find the example usage for java.lang Number intValue.

Prototype

public abstract int intValue();

Source Link

Document

Returns the value of the specified number as an int .

Usage

From source file:com.prowidesoftware.swift.model.field.Field38E.java

/**
 * Set the component1 from a Number object.
 * <br />//from   ww  w  . j  av  a  2s  .  c o  m
 * <em>If the component being set is a fixed length number, the argument will not be 
 * padded.</em> It is recommended for these cases to use the setComponent1(String) 
 * method.
 * 
 * @see #setComponent1(String)
 *
 * @param component1 the Number with the component1 content to set
 */
public Field38E setComponent1(java.lang.Number component1) {
    if (component1 != null) {
        setComponent(1, "" + component1.intValue());
    }
    return this;
}

From source file:com.pureinfo.srm.reports.table.data.ProductStatistic.java

private int[] count(Object[][] _datas, int _start, int _end) {
    int count[] = new int[_datas[0].length];
    for (int i = _start; i < _end; i++) {
        for (int j = 1; j < count.length; j++) {
            Number num = (Number) _datas[i][j];
            count[j] += num.intValue();
        }//ww  w  . j a va  2  s.  co m
    }
    return count;
}

From source file:org.crazydog.util.spring.NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 *
 * @param number      the number to convert
 * @param targetClass the target class to convert to
 * @return the converted number/*from w  w  w  .j  a  v  a  2s  . c o m*/
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see Byte
 * @see Short
 * @see Integer
 * @see Long
 * @see BigInteger
 * @see Float
 * @see Double
 * @see BigDecimal
 */
@SuppressWarnings("unchecked")
public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)
        throws IllegalArgumentException {

    org.springframework.util.Assert.notNull(number, "Number must not be null");
    org.springframework.util.Assert.notNull(targetClass, "Target class must not be null");

    if (targetClass.isInstance(number)) {
        return (T) number;
    } else if (Byte.class == targetClass) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Byte(number.byteValue());
    } else if (Short.class == targetClass) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Short(number.shortValue());
    } else if (Integer.class == targetClass) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Integer(number.intValue());
    } else if (Long.class == targetClass) {
        BigInteger bigInt = null;
        if (number instanceof BigInteger) {
            bigInt = (BigInteger) number;
        } else if (number instanceof BigDecimal) {
            bigInt = ((BigDecimal) number).toBigInteger();
        }
        // Effectively analogous to JDK 8's BigInteger.longValueExact()
        if (bigInt != null && (bigInt.compareTo(LONG_MIN) < 0 || bigInt.compareTo(LONG_MAX) > 0)) {
            raiseOverflowException(number, targetClass);
        }
        return (T) new Long(number.longValue());
    } else if (BigInteger.class == targetClass) {
        if (number instanceof BigDecimal) {
            // do not lose precision - use BigDecimal's own conversion
            return (T) ((BigDecimal) number).toBigInteger();
        } else {
            // original value is not a Big* number - use standard long conversion
            return (T) BigInteger.valueOf(number.longValue());
        }
    } else if (Float.class == targetClass) {
        return (T) new Float(number.floatValue());
    } else if (Double.class == targetClass) {
        return (T) new Double(number.doubleValue());
    } else if (BigDecimal.class == targetClass) {
        // always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return (T) new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

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  . ja  v  a 2s.  c  om
    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:com.facebook.stetho.json.ObjectMapper.java

private Object getValueForField(Field field, Object value) throws JSONException {
    try {/*from  w  w w  . j a va 2s  .  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:com.prowidesoftware.swift.model.field.Field136.java

/**
 * Set the component2 from a Number object.
 * <br />//  w w w  . ja va 2  s .  com
 * <em>If the component being set is a fixed length number, the argument will not be 
 * padded.</em> It is recommended for these cases to use the setComponent2(String) 
 * method.
 * 
 * @see #setComponent2(String)
 *
 * @param component2 the Number with the component2 content to set
 */
public Field136 setComponent2(java.lang.Number component2) {
    if (component2 != null) {
        setComponent(2, "" + component2.intValue());
    }
    return this;
}

From source file:com.github.lpezet.antiope.metrics.aws.spi.PredefinedMetricTransformer.java

/**
 * Returns a list with a single metric datum for the specified retry or
 * request count predefined metric; or an empty list if there is none.
 * //from ww w  .ja  v  a 2 s  .com
 * @param pMetricType
 *            must be either {@link Field#RequestCount} or
 *            {@link Field#RetryCount}; or else GIGO.
 */
protected List<MetricDatum> metricOfRequestOrRetryCount(APIRequestMetrics pMetricType, Request<?> pReq,
        Object pResp) {
    IMetrics m = pReq.getMetrics();
    TimingInfo ti = m.getTimingInfo();
    // Always retrieve the request count even for retry which is equivalent
    // to the number of requests minus one.
    Number oCounter = ti.getCounter(Field.RequestCount.name());
    if (oCounter == null) {
        // this is possible if one of the request handlers screwed up
        return Collections.emptyList();
    }
    int oRequestCount = oCounter.intValue();
    if (oRequestCount < 1) {
        LogFactory.getLog(getClass()).warn("request count must be at least one");
        return Collections.emptyList();
    }
    final double oCount = pMetricType == APIRequestMetrics.RequestCount ? oRequestCount : oRequestCount - 1 // retryCount = requestCount - 1
    ;
    if (oCount < 1) {
        return Collections.emptyList();
    } else {
        return Collections.singletonList(new MetricDatum().withMetricName(pReq.getServiceName())
                .withDimensions(
                        new Dimension().withName(Dimensions.MetricType.name()).withValue(pMetricType.name()))
                .withUnit(StandardUnit.Count).withValue(Double.valueOf(oCount))
                .withTimestamp(endTimestamp(ti)));
    }
}

From source file:com.prowidesoftware.swift.model.field.Field132.java

/**
 * Set the component2 from a Number object.
 * <br />/* ww  w  . j  av a  2 s.c  o  m*/
 * <em>If the component being set is a fixed length number, the argument will not be 
 * padded.</em> It is recommended for these cases to use the setComponent2(String) 
 * method.
 * 
 * @see #setComponent2(String)
 *
 * @param component2 the Number with the component2 content to set
 */
public Field132 setComponent2(java.lang.Number component2) {
    if (component2 != null) {
        setComponent(2, "" + component2.intValue());
    }
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInliner.java

private boolean evalSingleOpValue(final Number op1, final int opcode) {
    switch (opcode) {
    case Opcodes.IFEQ:
        return op1.intValue() == 0;
    case Opcodes.IFNE:
        return op1.intValue() != 0;
    case Opcodes.IFLT:
        return op1.intValue() < 0;
    case Opcodes.IFGE:
        return op1.intValue() >= 0;
    case Opcodes.IFGT:
        return op1.intValue() > 0;
    case Opcodes.IFLE:
        return op1.intValue() <= 0;
    case Opcodes.IFNULL:
        return op1 == null;
    case Opcodes.IFNONNULL:
        return op1 != null;
    default:/*from  w  w w  .  j  av  a 2 s .  c  om*/
        return false;
    }
}

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

protected Object convertNumber(Number number, Class<?> valueClass) throws JRException {
    Number value = null;/*w ww. j a v a 2s  . c  o  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;
}