Example usage for java.lang Number longValue

List of usage examples for java.lang Number longValue

Introduction

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

Prototype

public abstract long longValue();

Source Link

Document

Returns the value of the specified number as a long .

Usage

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the value of adding the two numbers
 * //ww w.  j  a va 2s.  co  m
 * @param a the first number
 * @param b the second number
 * @return the value of adding the two numbers
 */
public static Number add(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        return a.doubleValue() + b.doubleValue();
    } else {
        return a.longValue() + b.longValue();
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the value of subtracting the first from the second number.
 * /*from   w w w .ja  v a  2 s.  co m*/
 * @param a the first number
 * @param b the second number
 * @return the value of subtracting the first from the second number
 */
public static Number sub(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        return a.doubleValue() - b.doubleValue();
    } else {
        return a.longValue() - b.longValue();
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the value of multiplying the two numbers.
 * /*  ww  w. ja  va2  s.  c o  m*/
 * @param a the first number
 * @param b the second number
 * @return the value of multiplying the two numbers
 */
public static Number mul(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        return a.doubleValue() * b.doubleValue();
    } else {
        return a.longValue() * b.longValue();
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns {@code true} if the two numbers are equal; {@code false}
 * otherwise./*from ww w . j a v  a  2s  . c om*/
 * 
 * @param a the first number
 * @param b the second number
 * @return {@code true} if the two numbers are equal; {@code false}
 *         otherwise
 */
public static boolean equals(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        return a.doubleValue() == b.doubleValue();
    } else {
        return a.longValue() == b.longValue();
    }
}

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  ww  w  . j av a2  s  . c  om
            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:Main.java

/**
 * Divides num1 by num2, and return the result in the correct number class.
 * //from   w ww .  j  ava 2 s .c  o m
 * @param num1 numerator
 * @param num2 denominator
 * @return num1/num2 in the most appropriate class
 */
static Number divide(Number num1, Number num2) {
    Number[] both = new Number[2];
    both[0] = num1;
    both[1] = num2;

    Number division = (Number) getObject(both);

    if (division == null) {
        return null;
    }

    //Integer, Long, Float, Double
    if (division instanceof Integer) {
        //we've got 2 integers, but we're going to use double anyways
        return new Double(num1.doubleValue() / num2.doubleValue());
    } else if (division instanceof Long) {
        return new Long(num1.longValue() / num2.longValue());
    } else if (division instanceof Float) {
        return new Float(num1.floatValue() / num2.floatValue());
    } else if (division instanceof Double) {
        return new Double(num1.doubleValue() / num2.doubleValue());
    } else {
        return null;
    }
}

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   ww  w  . j  a  v  a  2 s . 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:com.discovery.darchrow.http.ResponseUtil.java

/**
 * Down load data./*from   w  w w.j  a v a  2  s .  c o  m*/
 *
 * @param saveFileName
 *            the save file name
 * @param inputStream
 *            the input stream
 * @param contentLength
 *            the content length
 * @param request
 *            the request
 * @param response
 *            the response
 * @throws UncheckedIOException
 *             the unchecked io exception
 */
private static void downLoadData(String saveFileName, InputStream inputStream, Number contentLength,
        HttpServletRequest request, HttpServletResponse response) throws UncheckedIOException {
    Date beginDate = new Date();

    if (log.isInfoEnabled()) {
        log.info("begin download~~,saveFileName:[{}],contentLength:[{}]", saveFileName,
                FileUtil.formatSize(contentLength.longValue()));
    }
    try {
        OutputStream outputStream = response.getOutputStream();

        //? 
        //inputStream.read(buffer);
        //outputStream = new BufferedOutputStream(response.getOutputStream());
        //outputStream.write(buffer);

        IOWriteUtil.write(inputStream, outputStream);
        if (log.isInfoEnabled()) {
            Date endDate = new Date();
            log.info("end download,saveFileName:[{}],contentLength:[{}],time use:[{}]", saveFileName,
                    FileUtil.formatSize(contentLength.longValue()),
                    DateExtensionUtil.getIntervalForView(beginDate, endDate));
        }
    } catch (IOException e) {
        /*
         * ?  ClientAbortException  ????? 
         * ?? ??
         * ???????
         * ? KILL? ?? ClientAbortException
         */
        //ClientAbortException:  java.net.SocketException: Connection reset by peer: socket write error
        final String exceptionName = e.getClass().getName();

        if (StringUtil.isContain(exceptionName, "ClientAbortException")
                || StringUtil.isContain(e.getMessage(), "ClientAbortException")) {
            log.warn(
                    "[ClientAbortException],maybe user use Thunder soft or abort client soft download,exceptionName:[{}],exception message:[{}] ,request User-Agent:[{}]",
                    exceptionName, e.getMessage(), RequestUtil.getHeaderUserAgent(request));
        } else {
            log.error("[download exception],exception name: " + exceptionName, e);
            throw new UncheckedIOException(e);
        }
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the maximum value of two numbers.
 * //from w  w w.  j  av  a 2s . c  o m
 * @param a the first number
 * @param b the second number
 * @return the maximum value of two numbers
 * @see Math#min(long, long)
 * @see Math#min(double, double)
 */
public static Number max(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        return Math.max(a.doubleValue(), b.doubleValue());
    } else {
        return Math.max(a.longValue(), b.longValue());
    }
}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.util.tree.NumberArithmetic.java

/**
 * Returns the minimum value of two numbers.
 * //from w  w  w.java  2 s.com
 * @param a the first number
 * @param b the second number
 * @return the minimum value of two numbers
 * @see Math#max(long, long)
 * @see Math#max(double, double)
 */
public static Number min(Number a, Number b) {
    if (isFloatingPoint(a) || isFloatingPoint(b)) {
        return Math.min(a.doubleValue(), b.doubleValue());
    } else {
        return Math.min(a.longValue(), b.longValue());
    }
}