Java Code Examples for com.google.common.math.DoubleMath#roundToInt()

The following examples show how to use com.google.common.math.DoubleMath#roundToInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultGenerator.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean[][] getBooleanValueArray(String hash) {
    Preconditions.checkArgument(!StringUtils.isEmpty(hash) && hash.length() >= 16, "illegal argument hash:not null "
            + "and size >= 16");

    this.hash = hash;

    boolean[][] array = new boolean[6][5];

    //初始化字符串
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 5; j++) {
            array[i][j] = false;
        }
    }

    for (int i = 0; i < hash.length(); i += 2) {
        int s = i / 2; //只取hash字符串偶数编号(从0开始)的字符

        boolean v = DoubleMath.roundToInt(Integer.parseInt(hash.charAt(i) + "", 16) / 10.0, RoundingMode.HALF_UP) > 0 ?
                true : false;
        if (s % 3 == 0) {
            array[s / 3][0] = v;
            array[s / 3][4] = v;
        } else if (s % 3 == 1) {
            array[s / 3][1] = v;
            array[s / 3][3] = v;
        } else {
            array[s / 3][2] = v;
        }
    }

    this.booleanValueArray = array;

    return this.booleanValueArray;
}
 
Example 2
Source File: HourWatermark.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * recalculate interval(in hours) if total number of partitions greater than maximum number of allowed partitions
 *
 * @param diffInMilliSecs difference in range
 * @param hourInterval hour interval (ex: 4 hours)
 * @param maxIntervals max number of allowed partitions
 * @return calculated interval in hours
 */
private static int getInterval(long diffInMilliSecs, long hourInterval, int maxIntervals) {
  int totalHours = DoubleMath.roundToInt((double) diffInMilliSecs / (60 * 60 * 1000), RoundingMode.CEILING);
  int totalIntervals = DoubleMath.roundToInt((double) totalHours / hourInterval, RoundingMode.CEILING);
  if (totalIntervals > maxIntervals) {
    hourInterval = DoubleMath.roundToInt((double) totalHours / maxIntervals, RoundingMode.CEILING);
  }
  return Ints.checkedCast(hourInterval);
}
 
Example 3
Source File: TimestampWatermark.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * recalculate interval(in hours) if total number of partitions greater than maximum number of allowed partitions
 *
 * @param diffInMilliSecs difference in range
 * @param hourInterval hour interval (ex: 4 hours)
 * @param maxIntervals max number of allowed partitions
 * @return calculated interval in hours
 */

private static int getInterval(long diffInMilliSecs, long hourInterval, int maxIntervals) {

  long totalHours = DoubleMath.roundToInt((double) diffInMilliSecs / (60 * 60 * 1000), RoundingMode.CEILING);
  long totalIntervals = DoubleMath.roundToInt((double) totalHours / hourInterval, RoundingMode.CEILING);
  if (totalIntervals > maxIntervals) {
    hourInterval = DoubleMath.roundToInt((double) totalHours / maxIntervals, RoundingMode.CEILING);
  }
  return Ints.checkedCast(hourInterval);
}
 
Example 4
Source File: DateWatermark.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * recalculate interval(in hours) if total number of partitions greater than maximum number of allowed partitions
 *
 * @param diffInMilliSecs difference in range
 * @param hourInterval hour interval (ex: 24 hours)
 * @param maxIntervals max number of allowed partitions
 * @return calculated interval in days
 */
private static int getInterval(long diffInMilliSecs, long hourInterval, int maxIntervals) {
  long dayInterval = TimeUnit.HOURS.toDays(hourInterval);
  int totalHours = DoubleMath.roundToInt((double) diffInMilliSecs / (60 * 60 * 1000), RoundingMode.CEILING);
  int totalIntervals = DoubleMath.roundToInt((double) totalHours / (dayInterval * 24), RoundingMode.CEILING);
  if (totalIntervals > maxIntervals) {
    hourInterval = DoubleMath.roundToInt((double) totalHours / maxIntervals, RoundingMode.CEILING);
    dayInterval = DoubleMath.roundToInt((double) hourInterval / 24, RoundingMode.CEILING);
  }
  return Ints.checkedCast(dayInterval);
}
 
Example 5
Source File: GuavaMathUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void should_round_to_integer_types() {
    int result3 = DoubleMath.roundToInt(2.5, RoundingMode.DOWN);
    assertThat(result3, equalTo(2));

    long result4 = DoubleMath.roundToLong(2.5, RoundingMode.HALF_UP);
    assertThat(result4, equalTo(3L));

    BigInteger result5 = DoubleMath.roundToBigInteger(2.5, RoundingMode.UP);
    assertThat(result5, equalTo(new BigInteger("3")));
}
 
Example 6
Source File: FileStreamingUtil.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private static long copyStreams(final InputStream from, final OutputStream to,
        final FileStreamingProgressListener progressListener, final long start, final long length,
        final String filename) throws IOException {

    final long startMillis = System.currentTimeMillis();
    LOG.trace("Start of copy-streams of file {} from {} to {}", filename, start, length);

    Preconditions.checkNotNull(from);
    Preconditions.checkNotNull(to);
    final byte[] buf = new byte[BUFFER_SIZE];
    long total = 0;
    int progressPercent = 1;

    ByteStreams.skipFully(from, start);

    long toRead = length;
    boolean toContinue = true;
    long shippedSinceLastEvent = 0;

    while (toContinue) {
        final int r = from.read(buf);
        if (r == -1) {
            break;
        }

        toRead -= r;
        if (toRead > 0) {
            to.write(buf, 0, r);
            total += r;
            shippedSinceLastEvent += r;
        } else {
            to.write(buf, 0, (int) toRead + r);
            total += toRead + r;
            shippedSinceLastEvent += toRead + r;
            toContinue = false;
        }

        if (progressListener != null) {
            final int newPercent = DoubleMath.roundToInt(total * 100.0 / length, RoundingMode.DOWN);

            // every 10 percent an event
            if (newPercent == 100 || newPercent > progressPercent + 10) {
                progressPercent = newPercent;
                progressListener.progress(length, shippedSinceLastEvent, total);
                shippedSinceLastEvent = 0;
            }
        }
    }

    final long totalTime = System.currentTimeMillis() - startMillis;

    if (total < length) {
        throw new FileStreamingFailedException(filename + ": " + (length - total)
                + " bytes could not be written to client, total time on write: !" + totalTime + " ms");
    }

    LOG.trace("Finished copy-stream of file {} with length {} in {} ms", filename, length, totalTime);

    return total;
}
 
Example 7
Source File: MathUtils.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("SameParameterValue")
public static int roundDoubleToInt(double value, RoundingMode roundingMode) {
    return DoubleMath.roundToInt(value, roundingMode);
}
 
Example 8
Source File: CharsetPreference.java    From ldp4j with Apache License 2.0 4 votes vote down vote up
static int round(final double weight) {
	Preconditions.checkArgument(weight>=0D,"Weight must be greater than or equal to 0 (%s)",weight);
	Preconditions.checkArgument(weight<=1D,"Weight must be lower than or equal to 1 (%s)",weight);
	return DoubleMath.roundToInt(weight*MAX_WEIGHT_DOUBLE,RoundingMode.DOWN);
}
 
Example 9
Source File: Utils.java    From CuckooFilter4J with Apache License 2.0 3 votes vote down vote up
/**
 * Calculates how many bits are needed to reach a given false positive rate.
 * 
 * @param fpProb
 *            the false positive probability.
 * @return the length of the tag needed (in bits) to reach the false
 *         positive rate.
 */
static int getBitsPerItemForFpRate(double fpProb,double loadFactor) {
	/*
	 * equation from Cuckoo Filter: Practically Better Than Bloom Bin Fan,
	 * David G. Andersen, Michael Kaminsky , Michael D. Mitzenmacher
	 */
	return DoubleMath.roundToInt(DoubleMath.log2((1 / fpProb) + 3) / loadFactor, RoundingMode.UP);
}
 
Example 10
Source File: Pixel.java    From amcgala with Educational Community License v2.0 2 votes vote down vote up
/**
 * Erzeugt einen neuen Pixel an der Stelle (x,y).
 * Die doubles werden entsprechend auf die Integerpositionen des Pixels gerundet.
 *
 * @param x die x-Koordinate des Pixels
 * @param y die y-Koordinate des Pixels
 */
public Pixel(double x, double y) {
    this.x = DoubleMath.roundToInt(x, RoundingMode.HALF_DOWN);
    this.y = DoubleMath.roundToInt(y, RoundingMode.HALF_DOWN);
}
 
Example 11
Source File: NumberUtil.java    From symja_android_library with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Converts this number to <code>int</code>; unlike {@link #intValue} this method raises {@link ArithmeticException}
 * if this integer cannot be represented by an <code>int</code> type.
 * 
 * @return the numeric value represented by this number after conversion to type <code>int</code>.
 * @throws ArithmeticException
 *             if conversion to <code>int</code> is not possible.
 */
public static int toInt(double d) throws ArithmeticException {
	// roundToInt() throws ArithmeticException if rounding not possible
	return DoubleMath.roundToInt(d, RoundingMode.UNNECESSARY);
}