DataSize implements Comparable<DataSize> { public long toBytes() { double bytes = getValue(Unit.BYTE); Preconditions.checkState(bytes <= Long.MAX_VALUE, "size is too large to be represented in bytes as a long"); return (long) bytes; } 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 testToBytes() { assertEquals(new DataSize(0, BYTE).toBytes(), 0); assertEquals(new DataSize(0, MEGABYTE).toBytes(), 0); assertEquals(new DataSize(1, BYTE).toBytes(), 1); assertEquals(new DataSize(1, KILOBYTE).toBytes(), 1024); assertEquals(new DataSize(42, MEGABYTE).toBytes(), 42L * 1024 * 1024); assertEquals(new DataSize(0.037, KILOBYTE).toBytes(), 37); assertEquals(new DataSize(1, TERABYTE).toBytes(), 1024L * 1024 * 1024 * 1024); assertEquals(new DataSize(1, PETABYTE).toBytes(), 1024L * 1024 * 1024 * 1024 * 1024); assertEquals(new DataSize(1024, PETABYTE).toBytes(), 1024L * 1024 * 1024 * 1024 * 1024 * 1024); assertEquals(new DataSize(8191, PETABYTE).toBytes(), 8191L * 1024 * 1024 * 1024 * 1024 * 1024); assertEquals(new DataSize(Long.MAX_VALUE, BYTE).toBytes(), Long.MAX_VALUE); assertEquals(new DataSize(Long.MAX_VALUE / 1024.0, KILOBYTE).toBytes(), Long.MAX_VALUE); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "size is too large .*") public void testToBytesTooLarge() { new DataSize(Long.MAX_VALUE + 1024.0001, BYTE).toBytes(); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "size is too large .*") public void testToBytesTooLarge2() { new DataSize(9000, PETABYTE).toBytes(); }