List of usage examples for java.math BigDecimal longValue
@Override public long longValue()
From source file:org.eclipse.smarthome.config.core.internal.ConfigMapper.java
private static Object objectConvert(Object value, Class<?> type) { Object result = value;// w w w .ja v a 2s. c o m // Handle the conversion case of BigDecimal to Float,Double,Long,Integer and the respective // primitive types String typeName = type.getSimpleName(); if (value instanceof BigDecimal && !type.equals(BigDecimal.class)) { BigDecimal bdValue = (BigDecimal) value; if (type.equals(Float.class) || typeName.equals("float")) { result = bdValue.floatValue(); } else if (type.equals(Double.class) || typeName.equals("double")) { result = bdValue.doubleValue(); } else if (type.equals(Long.class) || typeName.equals("long")) { result = bdValue.longValue(); } else if (type.equals(Integer.class) || typeName.equals("int")) { result = bdValue.intValue(); } } else // Handle the conversion case of String to Float,Double,Long,Integer,BigDecimal,Boolean and the respective // primitive types if (value instanceof String && !type.equals(String.class)) { String bdValue = (String) value; if (type.equals(Float.class) || typeName.equals("float")) { result = Float.valueOf(bdValue); } else if (type.equals(Double.class) || typeName.equals("double")) { result = Double.valueOf(bdValue); } else if (type.equals(Long.class) || typeName.equals("long")) { result = Long.valueOf(bdValue); } else if (type.equals(BigDecimal.class)) { result = new BigDecimal(bdValue); } else if (type.equals(Integer.class) || typeName.equals("int")) { result = Integer.valueOf(bdValue); } else if (type.equals(Boolean.class) || typeName.equals("boolean")) { result = Boolean.valueOf(bdValue); } else if (type.isEnum()) { @SuppressWarnings({ "rawtypes", "unchecked" }) final Class<? extends Enum> enumType = (Class<? extends Enum>) type; @SuppressWarnings({ "unchecked" }) final Enum<?> enumvalue = Enum.valueOf(enumType, value.toString()); result = enumvalue; } else if (Collection.class.isAssignableFrom(type)) { result = Collections.singletonList(value); } } return result; }
From source file:org.projectforge.common.NumberHelper.java
/** * Pretty output of bytes, "1023 bytes", "1.1 kb", "523 kb", "1.7 Mb", "143 Gb" etc. * @param bytes/*from w w w. j a va2s .c o m*/ * @return */ public static String formatBytes(final long bytes) { if (bytes < KILO_BYTES) { return String.valueOf(bytes) + " bytes"; } if (bytes < MEGA_BYTES) { BigDecimal no = new BigDecimal(bytes).divide(KB_BD, 1, BigDecimal.ROUND_HALF_UP); if (no.longValue() >= 100) { no = no.setScale(0, BigDecimal.ROUND_HALF_UP); } return NumberFormat.getInstance(PFUserContext.getLocale()).format(no) + " kb"; } if (bytes < GIGA_BYTES) { BigDecimal no = new BigDecimal(bytes).divide(MB_BD, 1, BigDecimal.ROUND_HALF_UP); if (no.longValue() >= 100) { no = no.setScale(0, BigDecimal.ROUND_HALF_UP); } return NumberFormat.getInstance(PFUserContext.getLocale()).format(no) + " Mb"; } BigDecimal no = new BigDecimal(bytes).divide(GB_BD, 1, BigDecimal.ROUND_HALF_UP); if (no.longValue() >= 100) { no = no.setScale(0, BigDecimal.ROUND_HALF_UP); } return NumberFormat.getInstance(PFUserContext.getLocale()).format(no) + " Gb"; }
From source file:org.projectforge.framework.utils.NumberHelper.java
/** * Pretty output of bytes, "1023 bytes", "1.1 kb", "523 kb", "1.7 Mb", "143 Gb" etc. * @param bytes//w w w . j av a 2s. c o m * @return */ public static String formatBytes(final long bytes) { if (bytes < KILO_BYTES) { return String.valueOf(bytes) + " bytes"; } if (bytes < MEGA_BYTES) { BigDecimal no = new BigDecimal(bytes).divide(KB_BD, 1, BigDecimal.ROUND_HALF_UP); if (no.longValue() >= 100) { no = no.setScale(0, BigDecimal.ROUND_HALF_UP); } return NumberFormat.getInstance(ThreadLocalUserContext.getLocale()).format(no) + " kb"; } if (bytes < GIGA_BYTES) { BigDecimal no = new BigDecimal(bytes).divide(MB_BD, 1, BigDecimal.ROUND_HALF_UP); if (no.longValue() >= 100) { no = no.setScale(0, BigDecimal.ROUND_HALF_UP); } return NumberFormat.getInstance(ThreadLocalUserContext.getLocale()).format(no) + " Mb"; } BigDecimal no = new BigDecimal(bytes).divide(GB_BD, 1, BigDecimal.ROUND_HALF_UP); if (no.longValue() >= 100) { no = no.setScale(0, BigDecimal.ROUND_HALF_UP); } return NumberFormat.getInstance(ThreadLocalUserContext.getLocale()).format(no) + " Gb"; }
From source file:com.github.jessemull.microflex.util.BigDecimalUtil.java
/** * Converts a list of BigDecimals to a list of longs. * @param List<BigDecimal> list of BigDecimals * @return list of longs *//*from w w w .j a va 2 s . c o m*/ public static List<Long> toLongList(List<BigDecimal> list) { List<Long> longList = new ArrayList<Long>(); for (BigDecimal val : list) { if (!OverFlowUtil.longOverflow(val)) { OverFlowUtil.overflowError(val); } longList.add(val.longValue()); } return longList; }
From source file:com.mobileman.projecth.services.ws.mobile.impl.HaqAnswersPostServiceImpl.java
/** * @param epochTimeInSecond/*from ww w . j av a 2 s. c o m*/ * @return Date from time epoch in second */ public static Date getDateFromTimeEpochInSecond(BigDecimal epochTimeInSecond) { if (log.isDebugEnabled()) { log.debug("getDateFromTimeEpochInSecond(BigDecimal) - start"); //$NON-NLS-1$ } Date date = epochTimeInSecond != null ? new Date(epochTimeInSecond.longValue() * 1000) : null; if (log.isDebugEnabled()) { log.debug("getDateFromTimeEpochInSecond(BigDecimal) - returns"); //$NON-NLS-1$ } return date; }
From source file:org.sonar.db.AbstractDbTester.java
private static List<Map<String, Object>> getHashMap(ResultSet resultSet) throws Exception { ResultSetMetaData metaData = resultSet.getMetaData(); int colCount = metaData.getColumnCount(); List<Map<String, Object>> rows = newArrayList(); while (resultSet.next()) { Map<String, Object> columns = newHashMap(); for (int i = 1; i <= colCount; i++) { Object value = resultSet.getObject(i); if (value instanceof Clob) { Clob clob = (Clob) value; value = IOUtils.toString((clob.getAsciiStream())); doClobFree(clob);//from w w w. j ava2 s . co m } else if (value instanceof BigDecimal) { // In Oracle, INTEGER types are mapped as BigDecimal BigDecimal bgValue = ((BigDecimal) value); if (bgValue.scale() == 0) { value = bgValue.longValue(); } else { value = bgValue.doubleValue(); } } else if (value instanceof Integer) { // To be consistent, all INTEGER types are mapped as Long value = ((Integer) value).longValue(); } else if (value instanceof Timestamp) { value = new Date(((Timestamp) value).getTime()); } columns.put(metaData.getColumnLabel(i), value); } rows.add(columns); } return rows; }
From source file:be.dataminded.nifi.plugins.util.JdbcCommon.java
public static long convertToAvroStream(final ResultSet rs, final OutputStream outStream, String recordName, ResultSetRowCallback callback, final int maxRows, boolean convertNames) throws SQLException, IOException { final Schema schema = createSchema(rs, recordName, convertNames); final GenericRecord rec = new GenericData.Record(schema); final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema); try (final DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(datumWriter)) { dataFileWriter.create(schema, outStream); final ResultSetMetaData meta = rs.getMetaData(); final int nrOfColumns = meta.getColumnCount(); long nrOfRows = 0; while (rs.next()) { if (callback != null) { callback.processRow(rs); }/* w w w . j a va2s . c o m*/ for (int i = 1; i <= nrOfColumns; i++) { final int javaSqlType = meta.getColumnType(i); // Need to handle CLOB and BLOB before getObject() is called, due to ResultSet's maximum portability statement if (javaSqlType == CLOB) { Clob clob = rs.getClob(i); if (clob != null) { long numChars = clob.length(); char[] buffer = new char[(int) numChars]; InputStream is = clob.getAsciiStream(); int index = 0; int c = is.read(); while (c > 0) { buffer[index++] = (char) c; c = is.read(); } rec.put(i - 1, new String(buffer)); clob.free(); } else { rec.put(i - 1, null); } continue; } if (javaSqlType == BLOB) { Blob blob = rs.getBlob(i); if (blob != null) { long numChars = blob.length(); byte[] buffer = new byte[(int) numChars]; InputStream is = blob.getBinaryStream(); int index = 0; int c = is.read(); while (c > 0) { buffer[index++] = (byte) c; c = is.read(); } ByteBuffer bb = ByteBuffer.wrap(buffer); rec.put(i - 1, bb); blob.free(); } else { rec.put(i - 1, null); } continue; } final Object value = rs.getObject(i); if (value == null) { rec.put(i - 1, null); } else if (javaSqlType == BINARY || javaSqlType == VARBINARY || javaSqlType == LONGVARBINARY || javaSqlType == ARRAY) { // bytes requires little bit different handling byte[] bytes = rs.getBytes(i); ByteBuffer bb = ByteBuffer.wrap(bytes); rec.put(i - 1, bb); } else if (value instanceof Byte) { // tinyint(1) type is returned by JDBC driver as java.sql.Types.TINYINT // But value is returned by JDBC as java.lang.Byte // (at least H2 JDBC works this way) // direct put to avro record results: // org.apache.avro.AvroRuntimeException: Unknown datum type java.lang.Byte rec.put(i - 1, ((Byte) value).intValue()); } else if (value instanceof Short) { //MS SQL returns TINYINT as a Java Short, which Avro doesn't understand. rec.put(i - 1, ((Short) value).intValue()); } else if (value instanceof BigDecimal) { // Avro can't handle BigDecimal as a number - it will throw an AvroRuntimeException such as: "Unknown datum type: java.math.BigDecimal: 38" try { int scale = meta.getScale(i); BigDecimal bigDecimal = ((BigDecimal) value); if (scale == 0) { if (meta.getPrecision(i) < 10) { rec.put(i - 1, bigDecimal.intValue()); } else { rec.put(i - 1, bigDecimal.longValue()); } } else { rec.put(i - 1, bigDecimal.doubleValue()); } } catch (Exception e) { rec.put(i - 1, value.toString()); } } else if (value instanceof BigInteger) { // Check the precision of the BIGINT. Some databases allow arbitrary precision (> 19), but Avro won't handle that. // It the SQL type is BIGINT and the precision is between 0 and 19 (inclusive); if so, the BigInteger is likely a // long (and the schema says it will be), so try to get its value as a long. // Otherwise, Avro can't handle BigInteger as a number - it will throw an AvroRuntimeException // such as: "Unknown datum type: java.math.BigInteger: 38". In this case the schema is expecting a string. if (javaSqlType == BIGINT) { int precision = meta.getPrecision(i); if (precision < 0 || precision > MAX_DIGITS_IN_BIGINT) { rec.put(i - 1, value.toString()); } else { try { rec.put(i - 1, ((BigInteger) value).longValueExact()); } catch (ArithmeticException ae) { // Since the value won't fit in a long, convert it to a string rec.put(i - 1, value.toString()); } } } else { rec.put(i - 1, value.toString()); } } else if (value instanceof Number || value instanceof Boolean) { if (javaSqlType == BIGINT) { int precision = meta.getPrecision(i); if (precision < 0 || precision > MAX_DIGITS_IN_BIGINT) { rec.put(i - 1, value.toString()); } else { rec.put(i - 1, value); } } else { rec.put(i - 1, value); } } else { // The different types that we support are numbers (int, long, double, float), // as well as boolean values and Strings. Since Avro doesn't provide // timestamp types, we want to convert those to Strings. So we will cast anything other // than numbers or booleans to strings by using the toString() method. rec.put(i - 1, value.toString()); } } dataFileWriter.append(rec); nrOfRows += 1; if (maxRows > 0 && nrOfRows == maxRows) break; } return nrOfRows; } }
From source file:Main.java
/** * NOTE: Arithmetic operations in primitive types may lead to arithmetic overflow. To retain * precision, BigDecimal objects are used. * * @param rgb/*from w w w. j a va 2s.c o m*/ * @return */ public static int[] convertRGB_8_8_8_To_HSB_32_32_32(int[] rgb) { int[] hsb = new int[3]; int maxChroma = Math.max(Math.max(rgb[0], rgb[1]), rgb[2]); int minChroma = Math.min(Math.min(rgb[0], rgb[1]), rgb[2]); int diff = maxChroma - minChroma; // Hue BigDecimal hue; if (diff == 0) { hue = BigDecimal.ZERO; } else if (maxChroma == rgb[0]) { float tmp = (rgb[1] - rgb[2]) / (float) diff; if (tmp < 0) { tmp += 6 * Math.ceil(-tmp / 6.0); } else { tmp -= 6 * Math.floor(tmp / 6.0); } hue = BigDecimal.valueOf(tmp); } else if (maxChroma == rgb[1]) { hue = BigDecimal.valueOf((rgb[2] - rgb[0]) / (float) diff + 2); } else { hue = BigDecimal.valueOf((rgb[0] - rgb[1]) / (float) diff + 4); } // [0, 360] -> [0, 0xffffffff] hue = hue.multiply(BigDecimal.valueOf(0xffffffffL)); hue = hue.divide(BigDecimal.valueOf(6), RoundingMode.FLOOR); hsb[0] = ByteBuffer.allocate(8).putLong(hue.longValue()).getInt(4); // Saturation if (maxChroma == 0) { hsb[1] = 0; } else { // [0, 1] -> [0, 0xffffffff] BigDecimal sat = BigDecimal.valueOf(diff); sat = sat.multiply(BigDecimal.valueOf(0xffffffffL)); sat = sat.divide(BigDecimal.valueOf(maxChroma), RoundingMode.FLOOR); hsb[1] = ByteBuffer.allocate(8).putLong(sat.longValue()).getInt(4); } // Brightness // [0, 255] -> [0, 0xffffffff] BigDecimal brightness = BigDecimal.valueOf(maxChroma); brightness = brightness.multiply(BigDecimal.valueOf(0xffffffffL)); brightness = brightness.divide(BigDecimal.valueOf(0xffL), RoundingMode.FLOOR); hsb[2] = ByteBuffer.allocate(8).putLong(brightness.longValue()).getInt(4); return hsb; }
From source file:dao.DashboardDAO.java
private static List<DashboardBarData> getDashboardBarData(String userId, String query) { Map<String, Object> result = null; if (StringUtils.isNotBlank(userId)) { List<LdapInfo> ldapInfoList = JiraDAO.getCurrentUserLdapInfo(userId); if (ldapInfoList != null && ldapInfoList.size() > 0 && ldapInfoList.get(0) != null) { String orgHierarchy = ldapInfoList.get(0).orgHierarchy; if (StringUtils.isNotBlank(orgHierarchy)) { result = getJdbcTemplate().queryForMap(query, orgHierarchy, orgHierarchy + "/%"); }/*from ww w . j av a 2 s . c om*/ } } List<DashboardBarData> barDataList = new ArrayList<>(); if (result != null && result.size() >= 6) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); DateFormat df = new SimpleDateFormat("MM/yy"); for (int i = 1; i <= 6; i++) { BigDecimal value = (BigDecimal) result.get(i + "_month_ago"); DashboardBarData dashboardBarData = new DashboardBarData(); dashboardBarData.label = df.format(cal.getTime()); if (value != null) { dashboardBarData.value = value.longValue(); } else { dashboardBarData.value = 0L; } barDataList.add(dashboardBarData); cal.add(Calendar.MONTH, -1); } } return barDataList; }
From source file:com.xeiam.xchange.bitstamp.dto.marketdata.BitstampTransaction.java
private BigDecimal roundUp(BigDecimal x) { long n = x.longValue(); return new BigDecimal(x.equals(new BigDecimal(n)) ? n : n + 1); }