Sergey Gotliv has uploaded a new change for review.

Change subject: engine, restapi: Remove redundant SizeConverter
......................................................................

engine, restapi: Remove redundant SizeConverter

Engine has 2 implementations of SizeConverter utility which converts
disk size from one unit to another. One of those implementations
resides in the common code and its code looks elegant therefore its
chosen to stay while other has been deleted.

Change-Id: I6a82a4a2ac199eb226b8848ac579106ca0497669
Signed-off-by: Sergey Gotliv <sgot...@redhat.com>
---
A 
backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/SizeConverterTest.java
D 
backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/SizeConverter.java
D 
backend/manager/modules/restapi/interface/common/jaxrs/src/test/java/org/ovirt/engine/api/common/util/SizeConverterTest.java
M 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageDomainMapper.java
M 
backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageLogicalUnitMapper.java
5 files changed, 66 insertions(+), 121 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/13/21313/1

diff --git 
a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/SizeConverterTest.java
 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/SizeConverterTest.java
new file mode 100644
index 0000000..da19b27
--- /dev/null
+++ 
b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/SizeConverterTest.java
@@ -0,0 +1,55 @@
+/*
+* Copyright (c) 2010 Red Hat, Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*           http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package org.ovirt.engine.core.common.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class SizeConverterTest {
+    @Test
+    public void testConvertMBToBytes() {
+        long megabytes = 3L;
+        long bytes = SizeConverter.convert(megabytes, 
SizeConverter.SizeUnit.MB,
+                SizeConverter.SizeUnit.BYTES).longValue();
+        assertEquals(bytes, 3145728);
+    }
+
+    @Test
+    public void testCobvertGBToBytes() {
+        long gigabytes = 3L;
+        long bytes = SizeConverter.convert(gigabytes, 
SizeConverter.SizeUnit.GB,
+                SizeConverter.SizeUnit.BYTES).longValue();
+        assertEquals(bytes, 3221225472L);
+    }
+
+    @Test
+    public void testConvertBytestoGB() {
+        long bytes = 3221228000L;
+        int gigabytes = SizeConverter.convert(bytes, 
SizeConverter.SizeUnit.BYTES,
+                SizeConverter.SizeUnit.GB).intValue();
+        assertEquals(gigabytes, 3);
+    }
+
+    @Test
+    public void testConvertBytestoMB() {
+        long bytes = 3160000L;
+        int megabytes = SizeConverter.convert(bytes, 
SizeConverter.SizeUnit.BYTES,
+                SizeConverter.SizeUnit.MB).intValue();
+        assertEquals(megabytes, 3);
+    }
+}
diff --git 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/SizeConverter.java
 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/SizeConverter.java
deleted file mode 100644
index 4a4913c..0000000
--- 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/main/java/org/ovirt/engine/api/common/util/SizeConverter.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
-* Copyright (c) 2010 Red Hat, Inc.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*           http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
-package org.ovirt.engine.api.common.util;
-
-/**
- * A utility class for converting sizes; i.e: bytes to mega-bytes, giga-bytes 
to bytes, etc.
- *
- * @author ori
- *
- */
-public class SizeConverter {
-
-    public static Long BYTES_IN_MEGA = 1024L * 1024L;
-    public static Long MEGAS_IN_GIGA = 1024L;
-
-    public static long megasToBytes(int megabytes) {
-        return megabytes * BYTES_IN_MEGA;
-    }
-
-    public static long megasToBytes(long megabytes) {
-        return megabytes * BYTES_IN_MEGA;
-    }
-
-    public static long gigasToBytes(int gigabytes) {
-        return gigabytes * BYTES_IN_MEGA * MEGAS_IN_GIGA;
-    }
-
-    public static long gigasToBytes(long gigabytes) {
-        return gigabytes * BYTES_IN_MEGA * MEGAS_IN_GIGA;
-    }
-
-    /**
-     * Converts bytes to mega-bytes. Rounds down to the nearest mega-byte.
-     * @param bytes number of bytes
-     * @return number of megabytes.
-     */
-    public static long bytesToMegas(long bytes) {
-        return bytes/BYTES_IN_MEGA;
-    }
-
-    /**
-     * Converts bytes to giga-bytes. Rounds down to the nearest giga-byte.
-     * @param bytes number of bytes
-     * @return number of gigabytes.
-     */
-    public static long bytesToGigas(long bytes) {
-        return bytes/(BYTES_IN_MEGA * MEGAS_IN_GIGA);
-    }
-}
diff --git 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/test/java/org/ovirt/engine/api/common/util/SizeConverterTest.java
 
b/backend/manager/modules/restapi/interface/common/jaxrs/src/test/java/org/ovirt/engine/api/common/util/SizeConverterTest.java
deleted file mode 100644
index 4c7fae7..0000000
--- 
a/backend/manager/modules/restapi/interface/common/jaxrs/src/test/java/org/ovirt/engine/api/common/util/SizeConverterTest.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
-* Copyright (c) 2010 Red Hat, Inc.
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*           http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
-package org.ovirt.engine.api.common.util;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
-
-public class SizeConverterTest {
-
-    @Test
-    public void testMegasToBytes() {
-        int megabytes = 3;
-        long bytes = SizeConverter.megasToBytes(megabytes);
-        assertEquals(bytes, 3145728);
-    }
-
-    @Test
-    public void testGigasToBytes() {
-        int gigabytes = 3;
-        long bytes = SizeConverter.gigasToBytes(gigabytes);
-        assertEquals(bytes, 3221225472L);
-    }
-
-    @Test
-    public void testBytestoGigas() {
-        long bytes = 3221228000L;
-        int gigabytes = (int)SizeConverter.bytesToGigas(bytes);
-        assertEquals(gigabytes, 3);
-    }
-
-    @Test
-    public void testBytestoMegas() {
-        long bytes = 3160000;
-        int megabytes = (int)SizeConverter.bytesToMegas(bytes);
-        assertEquals(megabytes, 3);
-    }
-}
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageDomainMapper.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageDomainMapper.java
index 7f84bd1..6fdcd10 100644
--- 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageDomainMapper.java
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageDomainMapper.java
@@ -1,7 +1,6 @@
 package org.ovirt.engine.api.restapi.types;
 
 import org.apache.commons.lang.StringUtils;
-import org.ovirt.engine.api.common.util.SizeConverter;
 import org.ovirt.engine.api.common.util.StatusUtils;
 import org.ovirt.engine.api.model.NfsVersion;
 import org.ovirt.engine.api.model.Storage;
@@ -15,6 +14,7 @@
 import org.ovirt.engine.api.restapi.utils.GuidUtils;
 import org.ovirt.engine.core.common.businessentities.StorageDomainStatic;
 import org.ovirt.engine.core.common.businessentities.StorageServerConnections;
+import org.ovirt.engine.core.common.utils.SizeConverter;
 
 public class StorageDomainMapper {
 
@@ -135,12 +135,15 @@
             model.getStorage().getVolumeGroup().setId(entity.getStorage());
         }
         if (entity.getAvailableDiskSize()!= null) {
-            
model.setAvailable(SizeConverter.gigasToBytes(entity.getAvailableDiskSize().longValue()));
+            
model.setAvailable(SizeConverter.convert(entity.getAvailableDiskSize().longValue(),
+                    SizeConverter.SizeUnit.GB, 
SizeConverter.SizeUnit.BYTES).longValue());
         }
         if (entity.getUsedDiskSize()!= null) {
-            
model.setUsed(SizeConverter.gigasToBytes(entity.getUsedDiskSize().longValue()));
+            
model.setUsed(SizeConverter.convert(entity.getUsedDiskSize().longValue(),
+                    SizeConverter.SizeUnit.GB, 
SizeConverter.SizeUnit.BYTES).longValue());
         }
-        
model.setCommitted(SizeConverter.gigasToBytes(entity.getCommittedDiskSize()));
+        model.setCommitted(SizeConverter.convert(entity.getCommittedDiskSize(),
+                SizeConverter.SizeUnit.GB, 
SizeConverter.SizeUnit.BYTES).longValue());
         if (entity.getStorageFormat()!= null) {
             String storageFormat = 
StorageFormatMapper.map(entity.getStorageFormat(), null).value();
             if (storageFormat != null) {
diff --git 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageLogicalUnitMapper.java
 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageLogicalUnitMapper.java
index 16565d3..8302637 100644
--- 
a/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageLogicalUnitMapper.java
+++ 
b/backend/manager/modules/restapi/types/src/main/java/org/ovirt/engine/api/restapi/types/StorageLogicalUnitMapper.java
@@ -2,13 +2,13 @@
 
 import java.util.ArrayList;
 
-import org.ovirt.engine.api.common.util.SizeConverter;
 import org.ovirt.engine.api.model.LogicalUnit;
 import org.ovirt.engine.api.model.LunStatus;
 import org.ovirt.engine.api.model.Storage;
 import org.ovirt.engine.api.model.StorageType;
 import org.ovirt.engine.core.common.businessentities.StorageServerConnections;
 import org.ovirt.engine.core.common.businessentities.LUNs;
+import org.ovirt.engine.core.common.utils.SizeConverter;
 
 public class StorageLogicalUnitMapper {
 
@@ -40,7 +40,9 @@
         if (entity.getStatus() != null) {
             model.setStatus(map(entity.getStatus(), null).value());
         }
-        model.setSize(SizeConverter.gigasToBytes(entity.getDeviceSize()));
+        model.setSize(SizeConverter.convert((long)entity.getDeviceSize(),
+                SizeConverter.SizeUnit.GB, 
SizeConverter.SizeUnit.BYTES).longValue());
+
         model.setPaths(entity.getPathCount());
         return model;
     }


-- 
To view, visit http://gerrit.ovirt.org/21313
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6a82a4a2ac199eb226b8848ac579106ca0497669
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Sergey Gotliv <sgot...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to