You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@heron.apache.org by GitBox <gi...@apache.org> on 2018/12/20 02:56:34 UTC

[GitHub] nwangtw closed pull request #3129: ByteAmount String Format

nwangtw closed pull request #3129: ByteAmount String Format
URL: https://github.com/apache/incubator-heron/pull/3129
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/heron/common/src/java/org/apache/heron/common/basics/ByteAmount.java b/heron/common/src/java/org/apache/heron/common/basics/ByteAmount.java
index 61d9c7133c..11c57459c5 100644
--- a/heron/common/src/java/org/apache/heron/common/basics/ByteAmount.java
+++ b/heron/common/src/java/org/apache/heron/common/basics/ByteAmount.java
@@ -23,12 +23,15 @@
  * Class that encapsulates number of bytes, with helpers to handle units properly.
  */
 public final class ByteAmount implements Comparable<ByteAmount> {
-  private static final long MB = 1024L * 1024;
+  private static final long KB = 1024L;
+  private static final long MB = KB * 1024;
   private static final long GB = MB * 1024;
   @SuppressWarnings("MathRoundIntLong")
   private static final long MAX_MB = Math.round(Long.MAX_VALUE / MB);
   @SuppressWarnings("MathRoundIntLong")
   private static final long MAX_GB = Math.round(Long.MAX_VALUE / GB);
+  @SuppressWarnings("MathRoundIntLong")
+  private static final long MAX_KB = Math.round(Long.MAX_VALUE / KB);
 
   public static final ByteAmount ZERO = ByteAmount.fromBytes(0);
   private final long bytes;
@@ -97,6 +100,18 @@ public long asMegabytes() {
     return Math.round((double) bytes / MB);
   }
 
+  /**
+   * Converts the number of bytes to kilobytes, rounding if there is a remainder. Because of loss
+   * of precision due to rounding, it's strongly advised to only use this method when it is certain
+   * that the only operations performed on the object were multiplication or addition and
+   * subtraction of other megabytes. If division or increaseBy were used this method could round up
+   * or down, potentially yielding unexpected results.
+   * @return returns the ByteValue in KBs or 0 if the value is &lt; (1024) / 2
+   */
+  public long asKilobytes() {
+    return Math.round((double) bytes / KB);
+  }
+
   /**
    * Converts the number of bytes to gigabytes, rounding if there is a remainder. Because of loss
    * of precision due to rounding, it's strongly advised to only use this method when it is certain
@@ -233,9 +248,11 @@ public int hashCode() {
   public String toString() {
     String value;
     if (asGigabytes() > 0) {
-      value = String.format("%d GB (%d bytes)", asGigabytes(), asBytes());
+      value = String.format("%.1f GB (%d bytes)", (double) asBytes() / GB, asBytes());
     } else if (asMegabytes() > 0) {
-      value = String.format("%d MB (%d bytes)", asMegabytes(), asBytes());
+      value = String.format("%.1f MB (%d bytes)", (double) asBytes() / MB, asBytes());
+    } else if (asKilobytes() > 0) {
+      value = String.format("%.1f KB (%d bytes)", (double) asBytes() / KB, asBytes());
     } else {
       value = bytes + " bytes";
     }
diff --git a/heron/common/tests/java/org/apache/heron/common/basics/ByteAmountTest.java b/heron/common/tests/java/org/apache/heron/common/basics/ByteAmountTest.java
index 7b6b5c41df..24044349b4 100644
--- a/heron/common/tests/java/org/apache/heron/common/basics/ByteAmountTest.java
+++ b/heron/common/tests/java/org/apache/heron/common/basics/ByteAmountTest.java
@@ -201,17 +201,27 @@ public void testCompareNull() {
 
   @Test
   public void testToString() {
-    assertEquals("ByteAmount{0 bytes}", ByteAmount.fromBytes(0).toString());
-    assertEquals("ByteAmount{524287 bytes}", ByteAmount.fromBytes((MB / 2) - 1).toString());
-    assertEquals("ByteAmount{1 MB (524289 bytes)}", ByteAmount.fromBytes((MB / 2) + 1).toString());
-    assertEquals("ByteAmount{1 MB (1048575 bytes)}", ByteAmount.fromBytes(MB - 1).toString());
-    assertEquals("ByteAmount{1 MB (1048576 bytes)}", ByteAmount.fromBytes(MB).toString());
-    assertEquals("ByteAmount{1 GB (1073741823 bytes)}", ByteAmount.fromBytes(GB - 1).toString());
-    assertEquals("ByteAmount{1 GB (1073741824 bytes)}", ByteAmount.fromBytes(GB).toString());
-    assertEquals("ByteAmount{2 GB (2147483648 bytes)}", ByteAmount.fromBytes(2 * GB).toString());
-    assertEquals("ByteAmount{2 GB (2147483647 bytes)}",
+    assertEquals("ByteAmount{0 bytes}",
+        ByteAmount.fromBytes(0).toString());
+    assertEquals("ByteAmount{512.0 KB (524287 bytes)}",
+        ByteAmount.fromBytes((MB / 2) - 1).toString());
+    assertEquals("ByteAmount{0.5 MB (524289 bytes)}",
+        ByteAmount.fromBytes((MB / 2) + 1).toString());
+    assertEquals("ByteAmount{1.0 MB (1048575 bytes)}",
+        ByteAmount.fromBytes(MB - 1).toString());
+    assertEquals("ByteAmount{1.0 MB (1048576 bytes)}",
+        ByteAmount.fromBytes(MB).toString());
+    assertEquals("ByteAmount{1.0 GB (1073741823 bytes)}",
+        ByteAmount.fromBytes(GB - 1).toString());
+    assertEquals("ByteAmount{1.0 GB (1073741824 bytes)}",
+        ByteAmount.fromBytes(GB).toString());
+    assertEquals("ByteAmount{1.1 GB (1181116006 bytes)}",
+        ByteAmount.fromBytes(GB).increaseBy(10).toString());
+    assertEquals("ByteAmount{2.0 GB (2147483648 bytes)}",
+        ByteAmount.fromBytes(2 * GB).toString());
+    assertEquals("ByteAmount{2.0 GB (2147483647 bytes)}",
         ByteAmount.fromBytes((2 * GB) - 1).toString());
-    assertEquals("ByteAmount{8589934592 GB (9223372036854775807 bytes)}",
+    assertEquals("ByteAmount{8589934592.0 GB (9223372036854775807 bytes)}",
         ByteAmount.fromBytes(Long.MAX_VALUE).toString());
   }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services