List of usage examples for java.lang Number longValue
public abstract long longValue();
From source file:org.red5.io.utils.ConversionUtils.java
/** * Convert number to primitive wrapper like Boolean or Float * /*from w ww .j a v a 2s. c o m*/ * @param num * Number to conver * @param wrapper * Primitive wrapper type * @return Converted object */ public static Object convertNumberToWrapper(Number num, Class<?> wrapper) { //XXX Paul: Using valueOf will reduce object creation if (wrapper.equals(String.class)) { return num.toString(); } else if (wrapper.equals(Boolean.class)) { return Boolean.valueOf(num.intValue() == 1); } else if (wrapper.equals(Double.class)) { return Double.valueOf(num.doubleValue()); } else if (wrapper.equals(Long.class)) { return Long.valueOf(num.longValue()); } else if (wrapper.equals(Float.class)) { return Float.valueOf(num.floatValue()); } else if (wrapper.equals(Integer.class)) { return Integer.valueOf(num.intValue()); } else if (wrapper.equals(Short.class)) { return Short.valueOf(num.shortValue()); } else if (wrapper.equals(Byte.class)) { return Byte.valueOf(num.byteValue()); } throw new ConversionException(String.format("Unable to convert number to: %s", wrapper)); }
From source file:org.batoo.common.reflect.ReflectHelper.java
/** * Converts the number into number Type//from w w w.jav a 2 s.c om * * @param value * the number value * @param numberType * the number type * @return the converted number value * * @since 2.0.1 */ public static Number convertNumber(Number value, Class<?> numberType) { if (value == null) { return null; } if (numberType.isAssignableFrom(value.getClass())) { return value; } if ((numberType == Integer.class) || (numberType == Integer.TYPE)) { return value.intValue(); } if ((numberType == Long.class) || (numberType == Long.TYPE)) { return value.longValue(); } if ((numberType == Short.class) || (numberType == Short.TYPE)) { return value.shortValue(); } if ((numberType == Byte.class) || (numberType == Byte.TYPE)) { return value.byteValue(); } if ((numberType == Float.class) || (numberType == Float.TYPE)) { return value.floatValue(); } if ((numberType == Double.class) || (numberType == Double.TYPE)) { return value.doubleValue(); } if (numberType == BigDecimal.class) { return BigDecimal.valueOf(value.doubleValue()); } if (numberType == BigInteger.class) { return BigInteger.valueOf(value.longValue()); } throw new IllegalArgumentException(numberType + " not supported"); }
From source file:demo.config.PropertyConverter.java
/** * Convert the specified object into a Long. * //from w w w .jav a2s . co m * @param value * the value to convert * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to a Long */ public static Long toLong(Object value) throws ConversionException { Number n = toNumber(value, Long.class); if (n instanceof Long) { return (Long) n; } else { return new Long(n.longValue()); } }
From source file:org.janusgraph.diskstorage.lucene.LuceneIndex.java
private static final Filter numericFilter(String key, Cmp relation, Number value) { switch (relation) { case EQUAL://from w w w . j av a 2 s . com return AttributeUtil.isWholeNumber(value) ? NumericRangeFilter.newLongRange(key, value.longValue(), value.longValue(), true, true) : NumericRangeFilter.newDoubleRange(key, value.doubleValue(), value.doubleValue(), true, true); case NOT_EQUAL: BooleanFilter q = new BooleanFilter(); if (AttributeUtil.isWholeNumber(value)) { q.add(NumericRangeFilter.newLongRange(key, Long.MIN_VALUE, value.longValue(), true, false), BooleanClause.Occur.SHOULD); q.add(NumericRangeFilter.newLongRange(key, value.longValue(), Long.MAX_VALUE, false, true), BooleanClause.Occur.SHOULD); } else { q.add(NumericRangeFilter.newDoubleRange(key, Double.MIN_VALUE, value.doubleValue(), true, false), BooleanClause.Occur.SHOULD); q.add(NumericRangeFilter.newDoubleRange(key, value.doubleValue(), Double.MAX_VALUE, false, true), BooleanClause.Occur.SHOULD); } return q; case LESS_THAN: return (AttributeUtil.isWholeNumber(value)) ? NumericRangeFilter.newLongRange(key, Long.MIN_VALUE, value.longValue(), true, false) : NumericRangeFilter.newDoubleRange(key, Double.MIN_VALUE, value.doubleValue(), true, false); case LESS_THAN_EQUAL: return (AttributeUtil.isWholeNumber(value)) ? NumericRangeFilter.newLongRange(key, Long.MIN_VALUE, value.longValue(), true, true) : NumericRangeFilter.newDoubleRange(key, Double.MIN_VALUE, value.doubleValue(), true, true); case GREATER_THAN: return (AttributeUtil.isWholeNumber(value)) ? NumericRangeFilter.newLongRange(key, value.longValue(), Long.MAX_VALUE, false, true) : NumericRangeFilter.newDoubleRange(key, value.doubleValue(), Double.MAX_VALUE, false, true); case GREATER_THAN_EQUAL: return (AttributeUtil.isWholeNumber(value)) ? NumericRangeFilter.newLongRange(key, value.longValue(), Long.MAX_VALUE, true, true) : NumericRangeFilter.newDoubleRange(key, value.doubleValue(), Double.MAX_VALUE, true, true); default: throw new IllegalArgumentException("Unexpected relation: " + relation); } }
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());// w w w. j ava 2 s . c o m else if (d instanceof Short) out.putShort(d.shortValue()); 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:com.alibaba.wasp.plan.parser.druid.DruidParser.java
/** * The abstract class Number is the superclass of classes BigDecimal, * BigInteger, Byte, Double, Float, Integer, Long, and Short. Subclasses of * Number must provide methods to convert the represented numeric value to * byte, double, float, int, long, and short. *//*from ww w .j a v a2 s . com*/ public static byte[] convert(DataType type, Number number) throws UnsupportedException { // BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short. if (number instanceof BigDecimal) { if (type == DataType.FLOAT) { return Bytes.toBytes(number.floatValue()); } else if (type == DataType.DOUBLE) { return Bytes.toBytes(number.doubleValue()); } else if (type == DataType.INT32) { return Bytes.toBytes(number.intValue()); } else if (type == DataType.INT64) { return Bytes.toBytes(number.longValue()); } return Bytes.toBytes((BigDecimal) number); } else if (number instanceof BigInteger) { throw new UnsupportedException(" BigInteger " + number + " Unsupported"); } else if (number instanceof Byte) { return Bytes.toBytes((Byte) number); } else if (number instanceof Double) { double value = number.doubleValue(); if (type == DataType.FLOAT) { return Bytes.toBytes((float) value); } else if (type == DataType.DOUBLE) { return Bytes.toBytes(value); } else if (type == DataType.INT32) { int iv = (int) value; return Bytes.toBytes(iv); } else if (type == DataType.INT64) { long lv = (long) value; return Bytes.toBytes(lv); } } else if (number instanceof Float) { float value = number.floatValue(); if (type == DataType.FLOAT) { return Bytes.toBytes(value); } else if (type == DataType.DOUBLE) { return Bytes.toBytes((float) value); } else if (type == DataType.INT32) { int iv = (int) value; return Bytes.toBytes(iv); } else if (type == DataType.INT64) { long lv = (long) value; return Bytes.toBytes(lv); } } else if (number instanceof Integer) { int value = number.intValue(); if (type == DataType.INT32) { return Bytes.toBytes((Integer) value); } else if (type == DataType.INT64) { return Bytes.toBytes((long) value); } else if (type == DataType.FLOAT) { float fv = (float) value; return Bytes.toBytes(fv); } else if (type == DataType.DOUBLE) { double fv = (double) value; return Bytes.toBytes(fv); } } else if (number instanceof Long) { long value = number.longValue(); if (type == DataType.INT32) { return Bytes.toBytes((int) value); } else if (type == DataType.INT64) { return Bytes.toBytes(value); } else if (type == DataType.FLOAT) { float fv = (float) value; return Bytes.toBytes(fv); } else if (type == DataType.DOUBLE) { double fv = (double) value; return Bytes.toBytes(fv); } } else if (number instanceof Short) { return Bytes.toBytes((Short) number); } throw new UnsupportedException("Unknown Number:" + number + " Type:" + type + " Unsupported "); }
From source file:demo.config.PropertyConverter.java
/** * Convert the specified object into a BigInteger. * /*from ww w.ja v a2 s.c om*/ * @param value * the value to convert * @return the converted value * @throws ConversionException * thrown if the value cannot be converted to a BigInteger */ public static BigInteger toBigInteger(Object value) throws ConversionException { Number n = toNumber(value, BigInteger.class); if (n instanceof BigInteger) { return (BigInteger) n; } else { return BigInteger.valueOf(n.longValue()); } }
From source file:com.alibaba.jstorm.ui.utils.UIUtils.java
public static List<ChartSeries> getChartSeries(List<MetricInfo> infos, int window) { Map<String, ChartSeries> chartMap = Maps.newHashMap(); for (MetricInfo info : infos) { for (Map.Entry<String, Map<Integer, MetricSnapshot>> metric : info.get_metrics().entrySet()) { String name = metric.getKey(); String[] split_name = name.split("@"); String metricName = UIMetricUtils.extractMetricName(split_name); if (!metric.getValue().containsKey(window)) { LOG.info("snapshot {} missing window:{}", metric.getKey(), window); continue; }/*from w w w . j ava2 s. co m*/ MetricSnapshot snapshot = metric.getValue().get(window); ChartSeries series; if (chartMap.containsKey(metricName)) { series = chartMap.get(metricName); } else { series = new ChartSeries(metricName); chartMap.put(metricName, series); } Number number = UIMetricUtils.getMetricNumberValue(snapshot); series.addData(number); if (metricName != null && metricName.equals(MetricDef.MEMORY_USED)) { series.addLabel(UIUtils.prettyFileSize(number.longValue())); } else { series.addLabel(UIMetricUtils.getMetricValue(snapshot)); } series.addCategory(UIUtils.parseDayTime(snapshot.get_ts())); } } return Lists.newArrayList(chartMap.values()); }
From source file:org.matsim.contrib.util.LongEnumAdder.java
@Override public void add(K e, Number value) { addLong(e, value.longValue()); }
From source file:Main.java
private JFormattedTextField init(JFormattedTextField jtf) { jtf.setValue(0);/* w ww . j a v a 2 s . c om*/ jtf.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { Number v1 = (Number) a.getValue(); Number v2 = (Number) b.getValue(); sum.setValue(v1.longValue() + v2.longValue()); } }); return jtf; }