DataSize implements Comparable<DataSize> { public long roundTo(Unit unit) { double rounded = Math.floor(getValue(unit) + 0.5d); Preconditions.checkArgument(rounded <= Long.MAX_VALUE, "size is too large to be represented in requested unit as a long"); return (long) rounded; } DataSize(double size, Unit unit); static DataSize succinctBytes(long bytes); static DataSize succinctDataSize(double size, Unit unit); long toBytes(); double getValue(); Unit getUnit(); double getValue(Unit unit); long roundTo(Unit unit); DataSize convertTo(Unit unit); DataSize convertToMostSuccinctDataSize(); @JsonValue @Override String toString(); @JsonCreator static DataSize valueOf(String size); @Override int compareTo(DataSize o); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testRoundTo() { assertEquals(new DataSize(0, BYTE).roundTo(BYTE), 0); assertEquals(new DataSize(0.5, BYTE).roundTo(BYTE), 1); assertEquals(new DataSize((42 * 1024) + 511, BYTE).roundTo(KILOBYTE), 42); assertEquals(new DataSize((42 * 1024) + 512, BYTE).roundTo(KILOBYTE), 43); assertEquals(new DataSize(513, TERABYTE).roundTo(PETABYTE), 1); assertEquals(new DataSize(9000L * 1024 * 1024 * 1024 * 1024, KILOBYTE).roundTo(PETABYTE), 9000); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "size is too large .*") public void testRoundToTooLarge() { new DataSize(Long.MAX_VALUE + 1024.0001, BYTE).roundTo(BYTE); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "size is too large .*") public void testRoundToTooLarge2() { new DataSize(9000, PETABYTE).roundTo(BYTE); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "size is too large .*") public void testRoundToTooLarge3() { new DataSize(9000 * 1024, PETABYTE).roundTo(KILOBYTE); }