/** * Obtain a {@link DataSize} representing the specified number of kilobytes. * @param kilobytes the number of kilobytes, positive or negative * @return a {@link DataSize} */ public static DataSize ofKilobytes(long kilobytes) { return new DataSize(Math.multiplyExact(kilobytes, BYTES_PER_KB)); }
/** * Obtain a {@link DataSize} representing the specified number of megabytes. * @param megabytes the number of megabytes, positive or negative * @return a {@link DataSize} */ public static DataSize ofMegabytes(long megabytes) { return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB)); }
/** * Obtain a {@link DataSize} representing the specified number of gigabytes. * @param gigabytes the number of gigabytes, positive or negative * @return a {@link DataSize} */ public static DataSize ofGigabytes(long gigabytes) { return new DataSize(Math.multiplyExact(gigabytes, BYTES_PER_GB)); }
/** * Obtain a {@link DataSize} representing the specified number of terabytes. * @param terabytes the number of terabytes, positive or negative * @return a {@link DataSize} */ public static DataSize ofTerabytes(long terabytes) { return new DataSize(Math.multiplyExact(terabytes, BYTES_PER_TB)); }
public byte[] get() { byte[] buffer; if (bufferPool.isEmpty()) { currentSize = min(multiplyExact(currentSize, 2), maxChunkSize); buffer = new byte[currentSize]; } else { buffer = bufferPool.remove(0); currentSize = buffer.length; } usedBuffers.add(buffer); return buffer; } }
public byte[] get() { byte[] buffer; if (bufferPool.isEmpty()) { currentSize = min(multiplyExact(currentSize, 2), maxChunkSize); buffer = new byte[currentSize]; } else { buffer = bufferPool.remove(0); currentSize = buffer.length; } usedBuffers.add(buffer); return buffer; } }
public static int toMonths(int year, int months) { try { return addExact(multiplyExact(year, 12), months); } catch (ArithmeticException e) { throw new IllegalArgumentException(e); } }
public static int toMonths(int year, int months) { try { return addExact(multiplyExact(year, 12), months); } catch (ArithmeticException e) { throw new IllegalArgumentException(e); } }
private long calcAverageLengthInSeconds( long months, long days, long seconds ) { long daysInSeconds = Math.multiplyExact( days, SECONDS_PER_DAY ); long monthsInSeconds = Math.multiplyExact( months, AVG_SECONDS_PER_MONTH ); return Math.addExact( seconds, Math.addExact( daysInSeconds, monthsInSeconds ) ); }
/** * Overflow safe multiplication of two longs * * @param a left-hand operand * @param b right-hand operand * @return a * b */ public static LongValue multiply( long a, long b ) { return longValue( Math.multiplyExact( a, b ) ); }
public static long toMillis(long day, long hour, long minute, long second, long millis) { try { long value = millis; value = addExact(value, multiplyExact(day, MILLIS_IN_DAY)); value = addExact(value, multiplyExact(hour, MILLIS_IN_HOUR)); value = addExact(value, multiplyExact(minute, MILLIS_IN_MINUTE)); value = addExact(value, multiplyExact(second, MILLIS_IN_SECOND)); return value; } catch (ArithmeticException e) { throw new IllegalArgumentException(e); } }
public static long toMillis(long day, long hour, long minute, long second, long millis) { try { long value = millis; value = addExact(value, multiplyExact(day, MILLIS_IN_DAY)); value = addExact(value, multiplyExact(hour, MILLIS_IN_HOUR)); value = addExact(value, multiplyExact(minute, MILLIS_IN_MINUTE)); value = addExact(value, multiplyExact(second, MILLIS_IN_SECOND)); return value; } catch (ArithmeticException e) { throw new IllegalArgumentException(e); } }
/** * Obtain a {@link DataSize} representing an amount in the specified {@link DataUnit}. * @param amount the amount of the size, measured in terms of the unit, * positive or negative * @return a corresponding {@link DataSize} */ public static DataSize of(long amount, DataUnit unit) { Assert.notNull(unit, "Unit must not be null"); return new DataSize(Math.multiplyExact(amount, unit.size().toBytes())); }
@UsedByGeneratedCode public static long tinyintToShortDecimal(long value, long precision, long scale, long tenToScale) { try { long decimal = multiplyExact(value, tenToScale); if (overflows(decimal, intScale(precision))) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast TINYINT '%s' to DECIMAL(%s, %s)", value, precision, scale)); } return decimal; } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast TINYINT '%s' to DECIMAL(%s, %s)", value, precision, scale)); } }
@UsedByGeneratedCode public static long bigintToShortDecimal(long value, long precision, long scale, long tenToScale) { try { long decimal = multiplyExact(value, tenToScale); if (overflows(decimal, intScale(precision))) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast BIGINT '%s' to DECIMAL(%s, %s)", value, precision, scale)); } return decimal; } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast BIGINT '%s' to DECIMAL(%s, %s)", value, precision, scale)); } }
@UsedByGeneratedCode public static long smallintToShortDecimal(long value, long precision, long scale, long tenToScale) { try { long decimal = multiplyExact(value, tenToScale); if (overflows(decimal, intScale(precision))) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast SMALLINT '%s' to DECIMAL(%s, %s)", value, precision, scale)); } return decimal; } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast SMALLINT '%s' to DECIMAL(%s, %s)", value, precision, scale)); } }
@UsedByGeneratedCode public static long integerToShortDecimal(long value, long precision, long scale, long tenToScale) { try { long decimal = multiplyExact(value, tenToScale); if (overflows(decimal, intScale(precision))) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast INTEGER '%s' to DECIMAL(%s, %s)", value, precision, scale)); } return decimal; } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast INTEGER '%s' to DECIMAL(%s, %s)", value, precision, scale)); } }
@ScalarOperator(MULTIPLY) @SqlType(StandardTypes.BIGINT) public static long multiply(@SqlType(StandardTypes.BIGINT) long left, @SqlType(StandardTypes.BIGINT) long right) { try { return Math.multiplyExact(left, right); } catch (ArithmeticException e) { throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("bigint multiplication overflow: %s * %s", left, right), e); } }
@ScalarOperator(MULTIPLY) @SqlType(StandardTypes.INTEGER) public static long multiply(@SqlType(StandardTypes.INTEGER) long left, @SqlType(StandardTypes.INTEGER) long right) { try { return Math.multiplyExact((int) left, (int) right); } catch (ArithmeticException e) { throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("integer multiplication overflow: %s * %s", left, right), e); } }
private List<RecordStresser> prepare( Condition condition, PagedFile pagedFile, RecordFormat format ) { int maxRecords = Math.multiplyExact( maxPages, format.getRecordsPerPage() ); TinyLockManager locks = new TinyLockManager(); List<RecordStresser> recordStressers = new LinkedList<>(); for ( int threadId = 0; threadId < numberOfThreads; threadId++ ) { recordStressers.add( new RecordStresser( pagedFile, condition, maxRecords, format, threadId, locks ) ); } return recordStressers; }