changeset 268:19b44914e3e1

Make values more easily readable in the "Memory" tab for a VM reviewed-by: omajid review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-May/001153.html This is a partial fix for PR968
author Jon VanAlten <jon.vanalten@redhat.com>
date Tue, 01 May 2012 17:16:39 -0400
parents 7d5c9fdb7305
children f6f3c1cc9332
files client/src/main/java/com/redhat/thermostat/client/ui/DisplayableValues.java client/src/main/java/com/redhat/thermostat/client/ui/VmMemoryController.java client/src/main/resources/com/redhat/thermostat/client/locale/strings.properties client/src/test/java/com/redhat/thermostat/client/ui/DisplayableValuesTest.java
diffstat 4 files changed, 146 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/src/main/java/com/redhat/thermostat/client/ui/DisplayableValues.java	Tue May 01 17:16:39 2012 -0400
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.client.ui;
+
+public class DisplayableValues {
+
+    private static final long BYTES_IN_KB = 1024;
+    private static final long BYTES_IN_MB = 1024*BYTES_IN_KB;
+    private static final long BYTES_IN_GB = 1024*BYTES_IN_MB;
+    private static final long BYTES_IN_TB = 1024*BYTES_IN_GB;
+
+    private static final String BYTES_UNIT = "B";
+    private static final String KBYTES_UNIT = "KiB";
+    private static final String MBYTES_UNIT = "MiB";
+    private static final String GBYTES_UNIT = "GiB";
+    private static final String TBYTES_UNIT = "TiB";
+
+    private static final String DOUBLE_FORMAT_STRING = "%.1f";
+
+    private DisplayableValues() {} // Not to be instantiated.
+
+    public static String[] bytes(final long bytes) {
+        if (bytes < BYTES_IN_KB) {
+            return new String[] { String.valueOf(bytes), BYTES_UNIT };
+        } else if (bytes < BYTES_IN_MB) {
+            return new String[] { String.format(DOUBLE_FORMAT_STRING, (double) bytes/BYTES_IN_KB), KBYTES_UNIT };
+        } else if (bytes < BYTES_IN_GB) {
+            return new String[] { String.format(DOUBLE_FORMAT_STRING, (double) bytes/BYTES_IN_MB), MBYTES_UNIT };
+        } else if (bytes < BYTES_IN_TB) {
+            return new String[] { String.format(DOUBLE_FORMAT_STRING, (double) bytes/BYTES_IN_GB), GBYTES_UNIT };
+        } else {
+            return new String[] { String.format(DOUBLE_FORMAT_STRING, (double) bytes/BYTES_IN_TB), TBYTES_UNIT };
+        }
+    }
+}
--- a/client/src/main/java/com/redhat/thermostat/client/ui/VmMemoryController.java	Mon Apr 30 11:44:05 2012 -0400
+++ b/client/src/main/java/com/redhat/thermostat/client/ui/VmMemoryController.java	Tue May 01 17:16:39 2012 -0400
@@ -86,9 +86,9 @@
                         }
 
                         int percentageUsed = (int) (100.0 * space.used/space.capacity);
-                        String currentlyUsed = localize(LocaleResources.VM_MEMORY_SPACE_USED, String.valueOf(space.used));
-                        String currentlyUnused = localize(LocaleResources.VM_MEMORY_SPACE_FREE, String.valueOf(space.capacity - space.used));
-                        String allocatable = localize(LocaleResources.VM_MEMORY_SPACE_ADDITIONAL, String.valueOf(space.maxCapacity-space.capacity));
+                        String currentlyUsed = localize(LocaleResources.VM_MEMORY_SPACE_USED, DisplayableValues.bytes(space.used));
+                        String currentlyUnused = localize(LocaleResources.VM_MEMORY_SPACE_FREE, DisplayableValues.bytes(space.capacity - space.used));
+                        String allocatable = localize(LocaleResources.VM_MEMORY_SPACE_ADDITIONAL, DisplayableValues.bytes(space.maxCapacity - space.capacity));
                         String name = space.name; // FIXME
                         view.updateRegionSize(name, percentageUsed, currentlyUsed, currentlyUnused, allocatable);
 
--- a/client/src/main/resources/com/redhat/thermostat/client/locale/strings.properties	Mon Apr 30 11:44:05 2012 -0400
+++ b/client/src/main/resources/com/redhat/thermostat/client/locale/strings.properties	Tue May 01 17:16:39 2012 -0400
@@ -126,9 +126,9 @@
 VM_CPU_CHART_TIME_LABEL = Time
 
 VM_MEMORY_SPACE_TITLE = Memory Region Sizes
-VM_MEMORY_SPACE_USED = {0} bytes used
-VM_MEMORY_SPACE_FREE = {0} bytes unused
-VM_MEMORY_SPACE_ADDITIONAL = An additional {0} bytes can be allocated
+VM_MEMORY_SPACE_USED = {0} {1} used
+VM_MEMORY_SPACE_FREE = {0} {1} unused
+VM_MEMORY_SPACE_ADDITIONAL = An additional {0} {1} can be allocated
 
 VM_GC_COLLECTOR_OVER_GENERATION = Collector {0} running on {1}
 VM_GC_COLLECTOR_CHART_REAL_TIME_LABEL = Time
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/src/test/java/com/redhat/thermostat/client/ui/DisplayableValuesTest.java	Tue May 01 17:16:39 2012 -0400
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.client.ui;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class DisplayableValuesTest {
+
+    @Test
+    public void testBytes() {
+        testBytesOutput("1", "B", DisplayableValues.bytes(1));
+        testBytesOutput("1023", "B", DisplayableValues.bytes(1023));
+        testBytesOutput("1.0", "KiB", DisplayableValues.bytes(1024));
+        testBytesOutput("1024.0", "KiB", DisplayableValues.bytes(1_048_575));
+        testBytesOutput("1.0", "MiB", DisplayableValues.bytes(1_048_576));
+        testBytesOutput("10.0", "MiB", DisplayableValues.bytes(10_480_000));
+        testBytesOutput("42.0", "MiB", DisplayableValues.bytes(44_040_000));
+        testBytesOutput("99.9", "MiB", DisplayableValues.bytes(104_752_742));
+        testBytesOutput("100.0", "MiB", DisplayableValues.bytes(104_857_600));
+        testBytesOutput("500.0", "MiB", DisplayableValues.bytes(524_288_000));
+        testBytesOutput("900.0", "MiB", DisplayableValues.bytes(943_718_400));
+        testBytesOutput("999.9", "MiB", DisplayableValues.bytes(1_048_471_000));
+        testBytesOutput("1.0", "GiB", DisplayableValues.bytes(1_073_741_824));
+        testBytesOutput("1.1", "GiB", DisplayableValues.bytes(1_181_116_000));
+        testBytesOutput("9.9", "GiB", DisplayableValues.bytes(10_630_044_000l));
+        testBytesOutput("99.9", "GiB", DisplayableValues.bytes(107_266_808_000l));
+        testBytesOutput("1.0", "TiB", DisplayableValues.bytes(1_099_511_627_776l));
+    }
+
+    private void testBytesOutput(String number, String units, String[] output) {
+        assertEquals(2, output.length);
+        assertEquals(number, output[0]);
+        assertEquals(units, output[1]);
+    }
+}