List of usage examples for java.lang Number longValue
public abstract long longValue();
From source file: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 . jav a 2 s. co m*/ * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see java.lang.Byte * @see java.lang.Short * @see java.lang.Integer * @see java.lang.Long * @see java.math.BigInteger * @see java.lang.Float * @see java.lang.Double * @see java.math.BigDecimal */ public static Number convertNumberToTargetClass(Number number, Class targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return number; } else if (targetClass.equals(Byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.byteValue(); } else if (targetClass.equals(Short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.shortValue(); } else if (targetClass.equals(Integer.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.intValue(); } else if (targetClass.equals(Long.class)) { return number.longValue(); } else if (targetClass.equals(Float.class)) { return number.floatValue(); } else if (targetClass.equals(Double.class)) { return number.doubleValue(); } else if (targetClass.equals(BigInteger.class)) { return BigInteger.valueOf(number.longValue()); } else if (targetClass.equals(BigDecimal.class)) { // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double) // (see BigDecimal javadoc for details) return 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:org.mule.util.NumberUtils.java
@SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return (T) number; } else if (targetClass.equals(Byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); }/*from www. j av a2 s .c o m*/ return (T) new Byte(number.byteValue()); } else if (targetClass.equals(Short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) new Short(number.shortValue()); } else if (targetClass.equals(Integer.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) new Integer(number.intValue()); } else if (targetClass.equals(Long.class)) { return (T) new Long(number.longValue()); } else if (targetClass.equals(BigInteger.class)) { 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 (targetClass.equals(Float.class)) { return (T) new Float(number.floatValue()); } else if (targetClass.equals(Double.class)) { return (T) new Double(number.doubleValue()); } else if (targetClass.equals(BigDecimal.class)) { // 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.bitsofproof.example.Simple.java
public static long parseBit(String s) throws ParseException { Number n = NumberFormat.getNumberInstance().parse(s); if (n instanceof Double) { return (long) (n.doubleValue() * 100.0); } else {//from ww w .j a v a 2 s .co m return n.longValue() * 100; } }
From source file:NumberUtils.java
/** * Convert the given number into an instance of the given target class. * @param number the number to convert//from ww w.j a va2 s . co m * @param targetClass the target class to convert to * @return the converted number * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see java.lang.Byte * @see java.lang.Short * @see java.lang.Integer * @see java.lang.Long * @see java.math.BigInteger * @see java.lang.Float * @see java.lang.Double * @see java.math.BigDecimal */ public static Number convertNumberToTargetClass(Number number, Class targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return number; } else if (targetClass.equals(Byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Byte(number.byteValue()); } else if (targetClass.equals(Short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Short(number.shortValue()); } else if (targetClass.equals(Integer.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Integer(number.intValue()); } else if (targetClass.equals(Long.class)) { return new Long(number.longValue()); } else if (targetClass.equals(Float.class)) { return new Float(number.floatValue()); } else if (targetClass.equals(Double.class)) { return new Double(number.doubleValue()); } else if (targetClass.equals(BigInteger.class)) { return BigInteger.valueOf(number.longValue()); } else if (targetClass.equals(BigDecimal.class)) { // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double) // (see BigDecimal javadoc for details) return 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.feilong.core.util.RandomUtil.java
/** * 0-<b>(maxValue)</b>?./*from ww w . j a v a 2s . c o m*/ * * <h3>:</h3> * <blockquote> * * <pre class="code"> * RandomUtil.createRandom(8) * ?8? * * ?? 3 * </pre> * * </blockquote> * * @param maxValue * ? * @return <code>maxValue</code> null, {@link NullPointerException} * @throws NullPointerException * <code>maxValue</code> null */ public static long createRandom(Number maxValue) { Validate.notNull(maxValue, "maxValue can't be null!"); // 0.0 ? 1.0 ? double double random = JVM_RANDOM.nextDouble(); return (long) Math.floor(random * maxValue.longValue());//double ??? }
From source file:com.feilong.core.util.RandomUtil.java
/** * ?(?)<code>minInclusiveValue</code>(??)<code>maxExclusiveValue</code>?. * //from ww w . j av a 2s .c om * <h3>:</h3> * <blockquote> * * <pre class="code"> * RandomUtil.createRandom(10, 20) * 10-20? * * ?? 12 * </pre> * * </blockquote> * * @param minInclusiveValue * ? * @param maxExclusiveValue * * @return <code>minInclusiveValue</code> null, {@link NullPointerException};<br> * <code>maxExclusiveValue</code> null, {@link NullPointerException};<br> * <code>minInclusiveValue</code>{@code <}<code>maxExclusiveValue</code>,{@link IllegalArgumentException}<br> * <code>minInclusiveValue</code>{@code =}<code>maxExclusiveValue</code>, <code>minLong</code> * * @see org.apache.commons.lang3.RandomUtils#nextInt(int, int) * @see org.apache.commons.lang3.RandomUtils#nextLong(long, long) * @see org.apache.commons.lang3.RandomUtils#nextFloat(float, float) * @see org.apache.commons.lang3.RandomUtils#nextDouble(double, double) */ public static long createRandom(Number minInclusiveValue, Number maxExclusiveValue) { Validate.notNull(minInclusiveValue, "minInclusiveValue can't be null!"); Validate.notNull(maxExclusiveValue, "maxExclusiveValue can't be null!"); long minLong = minInclusiveValue.longValue(); long maxLong = maxExclusiveValue.longValue(); Validate.isTrue(maxLong >= minLong, Slf4jUtil.format("minInclusiveValue:[{}] can not < maxExclusiveValue:[{}]", maxLong, minLong)); return RandomUtils.nextLong(minLong, maxLong); }
From source file:com.jkoolcloud.tnt4j.streams.utils.TimestampFormatter.java
/** * Converts the specified numeric timestamp from one precision to another. * * @param timestamp//from ww w . j av a 2 s.c om * numeric timestamp to convert * @param fromUnits * precision units timestamp is in * @param toUnits * precision units to convert timestamp to * @return converted numeric timestamp in precision units specified by toUnits */ public static long convert(Number timestamp, TimeUnit fromUnits, TimeUnit toUnits) { return toUnits.convert(timestamp == null ? 0 : timestamp.longValue(), fromUnits); }
From source file:org.lightadmin.core.util.NumberUtils.java
@SuppressWarnings("unchecked") public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass) throws IllegalArgumentException { Assert.notNull(number, "Number must not be null"); Assert.notNull(targetClass, "Target class must not be null"); if (targetClass.isInstance(number)) { return (T) number; } else if (targetClass.equals(byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); }/*from w w w.j a va 2 s . c o m*/ return (T) new Byte(number.byteValue()); } else if (targetClass.equals(short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) new Short(number.shortValue()); } else if (targetClass.equals(int.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return (T) new Integer(number.intValue()); } else if (targetClass.equals(long.class)) { return (T) new Long(number.longValue()); } else if (targetClass.equals(float.class)) { return (T) Float.valueOf(number.toString()); } else if (targetClass.equals(double.class)) { return (T) Double.valueOf(number.toString()); } else { return org.springframework.util.NumberUtils.convertNumberToTargetClass(number, targetClass); } }
From source file:org.briljantframework.data.vector.Convert.java
@SuppressWarnings("unchecked") private static <T> T convertNumber(Class<T> cls, Number num) { if (cls.equals(Double.class) || cls.equals(Double.TYPE)) { return (T) (Double) num.doubleValue(); } else if (cls.equals(Float.class) || cls.equals(Float.TYPE)) { return (T) (Float) num.floatValue(); } else if (cls.equals(Long.class) || cls.equals(Long.TYPE)) { return (T) (Long) num.longValue(); } else if (cls.equals(Integer.class) || cls.equals(Integer.TYPE)) { return (T) (Integer) num.intValue(); } else if (cls.equals(Short.class) || cls.equals(Short.TYPE)) { return (T) (Short) num.shortValue(); } else if (cls.equals(Byte.class) || cls.equals(Byte.TYPE)) { return (T) (Byte) num.byteValue(); } else if (Complex.class.equals(cls)) { return cls.cast(Complex.valueOf(num.doubleValue())); } else if (Logical.class.equals(cls)) { return cls.cast(num.intValue() == 1 ? Logical.TRUE : Logical.FALSE); } else if (Boolean.class.equals(cls)) { return cls.cast(num.intValue() == 1); } else {/*from www . j av a 2s. c o m*/ return Na.of(cls); } }
From source file:com.feilong.servlet.http.ResponseDownloadUtil.java
/** * Down load data.//from w w w.j av 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 */ private static void downLoadData(String saveFileName, InputStream inputStream, Number contentLength, HttpServletRequest request, HttpServletResponse response) { Date beginDate = new Date(); String length = FileUtil.formatSize(contentLength.longValue()); LOGGER.info("begin download~~,saveFileName:[{}],contentLength:[{}]", saveFileName, length); try { OutputStream outputStream = response.getOutputStream(); IOWriteUtil.write(inputStream, outputStream); if (LOGGER.isInfoEnabled()) { String pattern = "end download,saveFileName:[{}],contentLength:[{}],time use:[{}]"; LOGGER.info(pattern, saveFileName, length, formatDuration(beginDate)); } } catch (IOException e) { /* * ?, ClientAbortException , ?,????, ,. * ??, ?? * ?,???...???, * ?, KILL?, ,?? ClientAbortException. */ //ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error final String exceptionName = e.getClass().getName(); if (StringUtils.contains(exceptionName, "ClientAbortException") || StringUtils.contains(e.getMessage(), "ClientAbortException")) { String pattern = "[ClientAbortException],maybe user use Thunder soft or abort client soft download,exceptionName:[{}],exception message:[{}] ,request User-Agent:[{}]"; LOGGER.warn(pattern, exceptionName, e.getMessage(), RequestUtil.getHeaderUserAgent(request)); } else { LOGGER.error("[download exception],exception name: " + exceptionName, e); throw new UncheckedIOException(e); } } }