List of usage examples for java.lang Number longValue
public abstract long longValue();
From source file:org.cloudgraph.hbase.results.ResultsAggregator.java
private void aggregate(FunctionPath funcPath, PlasmaDataGraph existing, PlasmaDataGraph graph) { DataType scalarType = funcPath.getFunc().getName().getScalarDatatype(funcPath.getDataType()); PlasmaDataObject existingEndpoint = null; PlasmaDataObject newEndpoint = null; if (funcPath.getPath().size() == 0) { existingEndpoint = (PlasmaDataObject) existing.getRootObject(); newEndpoint = (PlasmaDataObject) graph.getRootObject(); } else {//from w w w . j a v a2 s. c o m existingEndpoint = (PlasmaDataObject) existing.getRootObject() .getDataObject(funcPath.getPath().toString()); newEndpoint = (PlasmaDataObject) graph.getRootObject().getDataObject(funcPath.getPath().toString()); } Number existingFuncValue = (Number) existingEndpoint.get(funcPath.getFunc().getName(), funcPath.getProperty()); if (funcPath.getFunc().getName().ordinal() == FunctionName.COUNT.ordinal()) { Long newCount = existingFuncValue.longValue() + 1; if (newCount > 0) { existingEndpoint.set(funcPath.getFunc().getName(), funcPath.getProperty(), newCount); } else { // overflow log.warn("aggregate " + funcPath.getFunc().getName() + " overflow for target type " + scalarType + " - truncating aggregate"); } } else { if (newEndpoint.isSet(funcPath.getProperty())) { Object newValue = newEndpoint.get(funcPath.getProperty()); Number newScalarValue = (Number) DataConverter.INSTANCE.convert(scalarType, funcPath.getProperty().getType(), newValue); switch (funcPath.getFunc().getName()) { case AVG: // Note; computing a sum until all values accumulated switch (scalarType) { case Double: Double avg = existingFuncValue != null ? existingFuncValue.doubleValue() + newScalarValue.doubleValue() : newScalarValue.doubleValue(); existingEndpoint.set(funcPath.getFunc().getName(), funcPath.getProperty(), avg); break; case Float: Float floatAvg = existingFuncValue != null ? existingFuncValue.floatValue() + newScalarValue.floatValue() : newScalarValue.floatValue(); existingEndpoint.set(funcPath.getFunc().getName(), funcPath.getProperty(), floatAvg); break; default: throw new IllegalArgumentException("illsgal datatype (" + scalarType + ") conversion for function, " + funcPath.getFunc().getName()); } break; case MAX: if (existingFuncValue == null || Double.valueOf(newScalarValue.doubleValue()) .compareTo(existingFuncValue.doubleValue()) > 0) existingEndpoint.set(funcPath.getFunc().getName(), funcPath.getProperty(), newScalarValue); break; case MIN: if (existingFuncValue == null || Double.valueOf(newScalarValue.doubleValue()) .compareTo(existingFuncValue.doubleValue()) < 0) existingEndpoint.set(funcPath.getFunc().getName(), funcPath.getProperty(), newScalarValue); break; case SUM: switch (scalarType) { case Double: Double doubleSum = existingFuncValue != null ? existingFuncValue.doubleValue() + newScalarValue.doubleValue() : newScalarValue.doubleValue(); if (!doubleSum.isInfinite()) { existingEndpoint.set(funcPath.getFunc().getName(), funcPath.getProperty(), doubleSum); } else { // overflow log.warn("aggregate " + funcPath.getFunc().getName() + " overflow for target type " + scalarType + " - truncating aggregate"); } break; case Long: Long longSum = existingFuncValue != null ? existingFuncValue.longValue() + newScalarValue.longValue() : newScalarValue.longValue(); if (longSum > 0) { existingEndpoint.set(funcPath.getFunc().getName(), funcPath.getProperty(), longSum); } else { // overflow log.warn("aggregate " + funcPath.getFunc().getName() + " overflow for target type " + scalarType + " - truncating aggregate"); } break; default: throw new IllegalArgumentException("illegal datatype (" + scalarType + ") conversion for function, " + funcPath.getFunc().getName()); } break; default: throw new GraphServiceException( "unimplemented aggregate function, " + funcPath.getFunc().getName()); } } else if (!funcPath.getProperty().isNullable()) log.warn("expected value for non-nullable property, " + funcPath.getProperty()); } }
From source file:com.github.nlloyd.hornofmongo.adaptor.Timestamp.java
@JSConstructor public Timestamp(Object t, Object i) { super();//from ww w. j av a 2s .co m if ((t instanceof Undefined) && (i instanceof Undefined)) { this.t = 0l; this.i = 0l; } else if ((t instanceof Undefined) || (i instanceof Undefined)) { Context.throwAsScriptRuntimeEx(new IllegalArgumentException("Error: Timestamp needs 0 or 2 arguments")); } else { Number tNum = null; Number iNum = null; try { tNum = NumberUtils.createNumber(Context.toString(t)); } catch (NumberFormatException nfe) { Context.throwAsScriptRuntimeEx( new IllegalArgumentException("Error: Timestamp time must be a number")); } try { iNum = NumberUtils.createNumber(Context.toString(i)); } catch (NumberFormatException nfe) { Context.throwAsScriptRuntimeEx( new IllegalArgumentException("Error: Timestamp increment must be a number")); } if (tNum.longValue() > largestVal) throw new IllegalArgumentException("The first argument must be in seconds;" + t.toString() + " is too large (max " + largestVal + ")"); this.t = tNum.longValue(); this.i = iNum.longValue(); } put("t", this, this.t); put("i", this, this.i); }
From source file:com.couchbase.client.java.document.json.JsonObject.java
/** * Retrieves the value from the field name and casts it to {@link Long}. * * Note that if value was stored as another numerical type, some truncation or rounding may occur. * * @param name the name of the field./* www . j a v a 2 s . c o m*/ * @return the result or null if it does not exist. */ public Long getLong(String name) { //let it fail in the more general case where it isn't actually a number Number number = (Number) content.get(name); if (number == null) { return null; } else if (number instanceof Long) { return (Long) number; } else { return number.longValue(); //autoboxing to Long } }
From source file:org.sigmah.client.util.ClientUtils.java
/** * Returns the given {@code number} corresponding {@code Long} value.<br> * If the {@code number} is {@code null}, the method returns {@code null}. * //from w ww . jav a2 s .c om * @param number * The number, may be {@code null}. * @return The given {@code number} corresponding {@code Long} value, or {@code null}. */ public static Long getLong(final Number number) { return number != null ? number.longValue() : null; }
From source file:com.examples.with.different.packagename.testcarver.NumberConverter.java
/** * Convert any Number object to the specified type for this * <i>Converter</i>.//from w ww.j a v a2 s . c om * <p> * This method handles conversion to the following types: * <ul> * <li><code>java.lang.Byte</code></li> * <li><code>java.lang.Short</code></li> * <li><code>java.lang.Integer</code></li> * <li><code>java.lang.Long</code></li> * <li><code>java.lang.Float</code></li> * <li><code>java.lang.Double</code></li> * <li><code>java.math.BigDecimal</code></li> * <li><code>java.math.BigInteger</code></li> * </ul> * @param sourceType The type being converted from * @param targetType The Number type to convert to * @param value The Number to convert. * * @return The converted value. */ private Number toNumber(Class sourceType, Class targetType, Number value) { // Correct Number type already if (targetType.equals(value.getClass())) { return value; } // Byte if (targetType.equals(Byte.class)) { long longValue = value.longValue(); if (longValue > Byte.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Byte.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Byte(value.byteValue()); } // Short if (targetType.equals(Short.class)) { long longValue = value.longValue(); if (longValue > Short.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Short.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Short(value.shortValue()); } // Integer if (targetType.equals(Integer.class)) { long longValue = value.longValue(); if (longValue > Integer.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } if (longValue < Integer.MIN_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too small " + toString(targetType)); } return new Integer(value.intValue()); } // Long if (targetType.equals(Long.class)) { return new Long(value.longValue()); } // Float if (targetType.equals(Float.class)) { if (value.doubleValue() > Float.MAX_VALUE) { throw new ConversionException( toString(sourceType) + " value '" + value + "' is too large for " + toString(targetType)); } return new Float(value.floatValue()); } // Double if (targetType.equals(Double.class)) { return new Double(value.doubleValue()); } // BigDecimal if (targetType.equals(BigDecimal.class)) { if (value instanceof Float || value instanceof Double) { return new BigDecimal(value.toString()); } else if (value instanceof BigInteger) { return new BigDecimal((BigInteger) value); } else { return BigDecimal.valueOf(value.longValue()); } } // BigInteger if (targetType.equals(BigInteger.class)) { if (value instanceof BigDecimal) { return ((BigDecimal) value).toBigInteger(); } else { return BigInteger.valueOf(value.longValue()); } } String msg = toString(getClass()) + " cannot handle conversion to '" + toString(targetType) + "'"; throw new ConversionException(msg); }
From source file:name.wramner.jmstools.analyzer.DataProvider.java
private long findWithLongResult(String sql) { Number n = findWithCachedScalarResult(sql, Number.class); return n != null ? n.longValue() : 0L; }
From source file:org.mrgeo.mapalgebra.parser.jexl.JexlParserAdapter.java
@Override public Object evaluate(ParserNode node) throws ParserException { JexlNode nativeNode = (JexlNode) node.getNativeNode(); if (nativeNode instanceof ASTAssignment) { JexlNode valueNode = nativeNode.jjtGetChild(1); return valueNode.jjtGetValue(); } else if (nativeNode instanceof ASTNumberLiteral) { return ((ASTNumberLiteral) nativeNode).getLiteral(); } else if (nativeNode instanceof ASTStringLiteral) { return ((ASTStringLiteral) nativeNode).getLiteral(); } else if ((nativeNode instanceof ASTUnaryMinusNode) && (nativeNode.jjtGetChild(0) instanceof ASTNumberLiteral)) { Number oldNum = ((ASTNumberLiteral) nativeNode.jjtGetChild(0)).getLiteral(); if (oldNum instanceof Integer) { return -oldNum.intValue(); } else if (oldNum instanceof Double) { return -oldNum.doubleValue(); } else if (oldNum instanceof Float) { return (float) (0.0 - oldNum.floatValue()); } else if (oldNum instanceof Short) { return -oldNum.shortValue(); } else if (oldNum instanceof Long) { return -oldNum.longValue(); } else if (oldNum instanceof Byte) { return -oldNum.byteValue(); }/*ww w. ja va 2 s . c o m*/ } return ((JexlNode) node.getNativeNode()).jjtGetValue(); }
From source file:com.jkoolcloud.tnt4j.core.UsecTimestamp.java
/** * Creates UsecTimestamp based on specified microsecond timestamp. * * @param usecTime timestamp, in microsecond * @throws IllegalArgumentException if usecTime is negative *//*from w ww. java 2 s. co m*/ public UsecTimestamp(Number usecTime) { setTimeUsec(usecTime.longValue()); }
From source file:org.apache.sqoop.mapreduce.hcat.SqoopHCatImportHelper.java
private Object convertNumberTypes(Object val, HCatFieldSchema hfs) { HCatFieldSchema.Type hfsType = hfs.getType(); if (!(val instanceof Number)) { return null; }/*from ww w . j a va2 s . c o m*/ if (val instanceof BigDecimal && hfsType == HCatFieldSchema.Type.STRING || hfsType == HCatFieldSchema.Type.VARCHAR || hfsType == HCatFieldSchema.Type.CHAR) { BigDecimal bd = (BigDecimal) val; String bdStr = null; if (bigDecimalFormatString) { bdStr = bd.toPlainString(); } else { bdStr = bd.toString(); } if (hfsType == HCatFieldSchema.Type.VARCHAR) { VarcharTypeInfo vti = (VarcharTypeInfo) hfs.getTypeInfo(); HiveVarchar hvc = new HiveVarchar(bdStr, vti.getLength()); return hvc; } else if (hfsType == HCatFieldSchema.Type.VARCHAR) { CharTypeInfo cti = (CharTypeInfo) hfs.getTypeInfo(); HiveChar hChar = new HiveChar(bdStr, cti.getLength()); return hChar; } else { return bdStr; } } Number n = (Number) val; if (hfsType == HCatFieldSchema.Type.TINYINT) { return n.byteValue(); } else if (hfsType == HCatFieldSchema.Type.SMALLINT) { return n.shortValue(); } else if (hfsType == HCatFieldSchema.Type.INT) { return n.intValue(); } else if (hfsType == HCatFieldSchema.Type.BIGINT) { return n.longValue(); } else if (hfsType == HCatFieldSchema.Type.FLOAT) { return n.floatValue(); } else if (hfsType == HCatFieldSchema.Type.DOUBLE) { return n.doubleValue(); } else if (hfsType == HCatFieldSchema.Type.BOOLEAN) { return n.byteValue() == 0 ? Boolean.FALSE : Boolean.TRUE; } else if (hfsType == HCatFieldSchema.Type.STRING) { return n.toString(); } else if (hfsType == HCatFieldSchema.Type.VARCHAR) { VarcharTypeInfo vti = (VarcharTypeInfo) hfs.getTypeInfo(); HiveVarchar hvc = new HiveVarchar(val.toString(), vti.getLength()); return hvc; } else if (hfsType == HCatFieldSchema.Type.CHAR) { CharTypeInfo cti = (CharTypeInfo) hfs.getTypeInfo(); HiveChar hChar = new HiveChar(val.toString(), cti.getLength()); return hChar; } else if (hfsType == HCatFieldSchema.Type.DECIMAL) { BigDecimal bd = new BigDecimal(n.doubleValue(), MathContext.DECIMAL128); return HiveDecimal.create(bd); } return null; }
From source file:org.openconcerto.erp.core.reports.history.ui.HistoriqueClientBilanPanel.java
public synchronized void updateVFData(final List<Integer> listId, final int idClient) { ComptaPropsConfiguration.getInstanceCompta().getNonInteractiveSQLExecutor().execute(new Runnable() { @Override/* w w w . jav a 2s .c o m*/ public void run() { final SQLBase base = ((ComptaPropsConfiguration) ComptaPropsConfiguration.getInstance()) .getSQLBaseSociete(); long valueTotal = 0; if (listId != null && listId.size() > 0) { final SQLSelect select = new SQLSelect(); final SQLTable table = base.getTable("SAISIE_VENTE_FACTURE"); select.addSelect(table.getField("T_HT"), "SUM"); select.setWhere(new Where(table.getKey(), listId)); final Number n = (Number) base.getDBSystemRoot().getDataSource() .executeScalar(select.asString()); if (n != null) { valueTotal = n.longValue(); } } final Map<Object, Date> mapDateFact = new HashMap<Object, Date>(); // On recupere les dates de facturations VF final SQLSelect selDateFacture = new SQLSelect(); final SQLTable tableFacture = base.getTable("SAISIE_VENTE_FACTURE"); final SQLTable tableEncaisse = base.getTable("ENCAISSER_MONTANT"); final SQLTable tableEcheance = base.getTable("ECHEANCE_CLIENT"); final SQLTable tableMvt = base.getTable("MOUVEMENT"); selDateFacture.addSelect(tableFacture.getField("DATE")); selDateFacture.addSelect(tableMvt.getField("ID_PIECE")); Where w = new Where(tableFacture.getField("ID_MOUVEMENT"), "=", tableMvt.getKey()); if (idClient > 1) { w = w.and(new Where(tableFacture.getField("ID_CLIENT"), "=", idClient)); } selDateFacture.setWhere(w); addDatesToMap(base, selDateFacture, mapDateFact); // On recupere les dates de facturations final SQLSelect selDateFactureC = new SQLSelect(); final SQLTable tableComptoir = base.getTable("SAISIE_VENTE_COMPTOIR"); selDateFactureC.addSelect(tableComptoir.getField("DATE")); selDateFactureC.addSelect(tableMvt.getField("ID_PIECE")); Where wC = new Where(tableComptoir.getField("ID_MOUVEMENT"), "=", tableMvt.getKey()); if (idClient > 1) { wC = wC.and(new Where(tableComptoir.getField("ID_CLIENT"), "=", idClient)); } selDateFactureC.setWhere(wC); addDatesToMap(base, selDateFactureC, mapDateFact); // On recupere les dates d'encaissement final SQLSelect selDateEncaisse = new SQLSelect(); selDateEncaisse.addSelect(tableEncaisse.getField("DATE")); selDateEncaisse.addSelect(tableMvt.getField("ID_PIECE")); selDateEncaisse.addSelect(tableEcheance.getField("ID")); Where wEncaisse = new Where(tableEcheance.getField("ID"), "=", tableEncaisse.getField("ID_ECHEANCE_CLIENT")); wEncaisse = wEncaisse .and(new Where(tableEcheance.getField("ID_MOUVEMENT"), "=", tableMvt.getField("ID"))); wEncaisse = wEncaisse.and(new Where(tableEcheance.getArchiveField(), "=", 1)); if (idClient > 1) { wEncaisse = wEncaisse.and(new Where(tableEcheance.getField("ID_CLIENT"), "=", idClient)); } selDateEncaisse.setWhere(wEncaisse); selDateEncaisse.setArchivedPolicy(SQLSelect.BOTH); final List<Object[]> lDateEncaisse = (List<Object[]>) base.getDataSource() .execute(selDateEncaisse.asString(), new ArrayListHandler()); final Map<Object, Date> mapDateEncaisse = new HashMap<Object, Date>(); for (int i = 0; i < lDateEncaisse.size(); i++) { final Object[] tmp = lDateEncaisse.get(i); final Date d2 = (Date) tmp[0]; final Object d = mapDateEncaisse.get(tmp[1]); if (d != null) { final Date d1 = (Date) d; if (d1.before(d2)) { mapDateEncaisse.put(tmp[1], d2); } } else { mapDateEncaisse.put(tmp[1], d2); } } // Calcul moyenne int cpt = 0; int day = 0; final Calendar cal1 = Calendar.getInstance(); final Calendar cal2 = Calendar.getInstance(); for (final Iterator i = mapDateFact.keySet().iterator(); i.hasNext();) { final Object key = i.next(); final Date dFact = mapDateFact.get(key); final Date dEncaisse = mapDateEncaisse.get(key); if (dFact != null && dEncaisse != null) { cpt++; cal1.setTime(dFact); cal2.setTime(dEncaisse); cal1.set(Calendar.HOUR, 0); cal1.set(Calendar.MINUTE, 0); cal1.set(Calendar.SECOND, 0); cal1.set(Calendar.MILLISECOND, 0); cal2.set(Calendar.HOUR, 0); cal2.set(Calendar.MINUTE, 0); cal2.set(Calendar.SECOND, 0); cal2.set(Calendar.MILLISECOND, 0); day += (cal2.getTime().getTime() - cal1.getTime().getTime()) / 86400000; } } setPoucentageVentes(cpt == 0 ? 0 : day / cpt); setTotalVentesFacture(valueTotal); setNbVentesFacture(listId == null ? 0 : listId.size()); updateLabels(); } }); }