changeset 865:4ebc8c2c19fb

Reduce size of Constants class Remove some constants from the Constants class. As a (nice) side-effect, kilobytes are now 1024 everywhere. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-December/004839.html
author Omair Majid <omajid@redhat.com>
date Mon, 17 Dec 2012 14:05:26 -0500
parents 8e16f9b4ef38
children d999dac7925c cf634ed45556
files common/core/src/main/java/com/redhat/thermostat/common/Constants.java common/core/src/main/java/com/redhat/thermostat/common/utils/DisplayableValues.java common/core/src/test/java/com/redhat/thermostat/common/utils/DisplayableValuesTest.java system-backend/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java system-backend/src/main/java/com/redhat/thermostat/backend/system/MemoryStatBuilder.java system-backend/src/test/java/com/redhat/thermostat/backend/system/HostInfoBuilderTest.java system-backend/src/test/java/com/redhat/thermostat/backend/system/MemoryStatBuilderTest.java
diffstat 7 files changed, 28 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/Constants.java	Fri Dec 14 16:06:04 2012 +0100
+++ b/common/core/src/main/java/com/redhat/thermostat/common/Constants.java	Mon Dec 17 14:05:26 2012 -0500
@@ -49,12 +49,6 @@
     public static final int EXIT_BACKEND_LOAD_ERROR = 5;
     public static final int EXIT_BACKEND_START_ERROR = 6;
 
-    public static final int SAMPLING_INTERVAL_UNKNOWN = -1;
-
-    public static final String AGENT_LOCAL_HOSTNAME = "localhost";
-
-    public static final long KILOBYTES_TO_BYTES = 1000;
-
     public static final String GENERIC_SERVICE_CLASSNAME = "GenericClassName";
 
 }
--- a/common/core/src/main/java/com/redhat/thermostat/common/utils/DisplayableValues.java	Fri Dec 14 16:06:04 2012 +0100
+++ b/common/core/src/main/java/com/redhat/thermostat/common/utils/DisplayableValues.java	Mon Dec 17 14:05:26 2012 -0500
@@ -73,6 +73,10 @@
             return result;
         }
 
+        public static long convertToBytes(final long originalValue, Scale originalScale) {
+            return originalValue * originalScale.numBytes;
+        }
+
         public static Scale getScale(final long bytes) {
             if (bytes < BYTES_IN_KB) {
                 return Scale.B;
--- a/common/core/src/test/java/com/redhat/thermostat/common/utils/DisplayableValuesTest.java	Fri Dec 14 16:06:04 2012 +0100
+++ b/common/core/src/test/java/com/redhat/thermostat/common/utils/DisplayableValuesTest.java	Mon Dec 17 14:05:26 2012 -0500
@@ -102,5 +102,8 @@
         
         value = Scale.convertTo(Scale.MiB, 524_288_000);
         assertEquals(500, value, 0);
+
+        long longValue = Scale.convertToBytes(1, Scale.MiB);
+        assertEquals(1024l * 1024, longValue);
     }
 }
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Fri Dec 14 16:06:04 2012 +0100
+++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Mon Dec 17 14:05:26 2012 -0500
@@ -43,7 +43,7 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.redhat.thermostat.common.Constants;
+import com.redhat.thermostat.common.utils.DisplayableValues.Scale;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.storage.model.HostInfo;
 import com.redhat.thermostat.utils.ProcDataSource;
@@ -53,6 +53,8 @@
 
     private static final Logger logger = LoggingUtils.getLogger(HostInfoBuilder.class);
 
+    public static final String FALLBACK_LOCAL_HOSTNAME = "localhost";
+
     static class HostCpuInfo {
         public final String model;
         public final int count;
@@ -127,7 +129,7 @@
             long data = Long.valueOf(memTotalParts[1]);
             String units = memTotalParts[2];
             if (units.equals("kB")) {
-                totalMemory = data * Constants.KILOBYTES_TO_BYTES;
+                totalMemory = Scale.convertToBytes(data, Scale.KiB);
             }
         } catch (IOException ioe) {
             logger.log(Level.WARNING, "unable to read memory info");
@@ -169,7 +171,8 @@
         
         // still null, use localhost
         if (hostname == null) {
-            hostname = Constants.AGENT_LOCAL_HOSTNAME;
+            hostname = FALLBACK_LOCAL_HOSTNAME;
+
         }
         
         return hostname;
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/MemoryStatBuilder.java	Fri Dec 14 16:06:04 2012 +0100
+++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/MemoryStatBuilder.java	Mon Dec 17 14:05:26 2012 -0500
@@ -41,8 +41,9 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.common.NotImplementedException;
+import com.redhat.thermostat.common.utils.DisplayableValues;
+import com.redhat.thermostat.common.utils.DisplayableValues.Scale;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.storage.model.MemoryStat;
 import com.redhat.thermostat.utils.ProcDataSource;
@@ -126,7 +127,7 @@
             result = Long.parseLong(value);
             if (units != null) {
                 if (units.equals("kB") || units.equals("KB")) {
-                    result = result * Constants.KILOBYTES_TO_BYTES;
+                    result = Scale.convertToBytes(result, Scale.KiB);
                 } else {
                     throw new NotImplementedException("unit conversion from " + units + " not implemented");
                 }
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/HostInfoBuilderTest.java	Fri Dec 14 16:06:04 2012 +0100
+++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/HostInfoBuilderTest.java	Mon Dec 17 14:05:26 2012 -0500
@@ -51,12 +51,13 @@
 import com.redhat.thermostat.backend.system.HostInfoBuilder.HostCpuInfo;
 import com.redhat.thermostat.backend.system.HostInfoBuilder.HostMemoryInfo;
 import com.redhat.thermostat.backend.system.HostInfoBuilder.HostOsInfo;
-import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.storage.model.HostInfo;
 import com.redhat.thermostat.utils.ProcDataSource;
 
 public class HostInfoBuilderTest {
 
+    final int KILOBYTES_TO_BYTES = 1024;
+
     @Test
     public void testSimpleBuild() {
         HostInfo info = new HostInfoBuilder(new ProcDataSource()).build();
@@ -93,7 +94,7 @@
 
         HostMemoryInfo memoryInfo = new HostInfoBuilder(dataSource).getMemoryInfo();
         assertNotNull(memoryInfo);
-        assertEquals(12345 * Constants.KILOBYTES_TO_BYTES, memoryInfo.totalMemory);
+        assertEquals(12345 * KILOBYTES_TO_BYTES, memoryInfo.totalMemory);
         verify(dataSource).getMemInfoReader();
 
     }
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/MemoryStatBuilderTest.java	Fri Dec 14 16:06:04 2012 +0100
+++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/MemoryStatBuilderTest.java	Mon Dec 17 14:05:26 2012 -0500
@@ -48,12 +48,13 @@
 
 import org.junit.Test;
 
-import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.storage.model.MemoryStat;
 import com.redhat.thermostat.utils.ProcDataSource;
 
 public class MemoryStatBuilderTest {
 
+    private static final int KILOBYTES_TO_BYTES = 1024;
+
     @Test
     public void testSimpleBuild() {
         MemoryStat stat = new MemoryStatBuilder(new ProcDataSource()).build();
@@ -98,13 +99,13 @@
 
         MemoryStat stat = new MemoryStatBuilder(dataSource).build();
 
-        assertEquals(BUFFERS * Constants.KILOBYTES_TO_BYTES, stat.getBuffers());
-        assertEquals(CACHED * Constants.KILOBYTES_TO_BYTES, stat.getCached());
-        assertEquals(COMMIT_LIMIT * Constants.KILOBYTES_TO_BYTES, stat.getCommitLimit());
-        assertEquals(FREE * Constants.KILOBYTES_TO_BYTES, stat.getFree());
-        assertEquals(SWAP_FREE * Constants.KILOBYTES_TO_BYTES, stat.getSwapFree());
-        assertEquals(SWAP_TOTAL * Constants.KILOBYTES_TO_BYTES, stat.getSwapTotal());
-        assertEquals(TOTAL * Constants.KILOBYTES_TO_BYTES, stat.getTotal());
+        assertEquals(BUFFERS * KILOBYTES_TO_BYTES, stat.getBuffers());
+        assertEquals(CACHED * KILOBYTES_TO_BYTES, stat.getCached());
+        assertEquals(COMMIT_LIMIT * KILOBYTES_TO_BYTES, stat.getCommitLimit());
+        assertEquals(FREE * KILOBYTES_TO_BYTES, stat.getFree());
+        assertEquals(SWAP_FREE * KILOBYTES_TO_BYTES, stat.getSwapFree());
+        assertEquals(SWAP_TOTAL * KILOBYTES_TO_BYTES, stat.getSwapTotal());
+        assertEquals(TOTAL * KILOBYTES_TO_BYTES, stat.getTotal());
         assertTrue(stat.getTimeStamp() != 0 && stat.getTimeStamp() != Long.MIN_VALUE);
         assertTrue(stat.getTimeStamp() <= System.currentTimeMillis());
         verify(dataSource).getMemInfoReader();