anmolbabu has uploaded a new change for review.

Change subject: engine : Math operations on SizeConverter converted sizes
......................................................................

engine : Math operations on SizeConverter converted sizes

Facilitate Math operations on SizeConverter converted sizes

Change-Id: Ie9fd4e7b7b9411cf17d66037e6424a979c2d037f
Signed-off-by: Anmol Babu <anb...@redhat.com>
---
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/SizeConverter.java
M 
backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/utils/SizeConverterTest.java
2 files changed, 100 insertions(+), 17 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/25/40625/1

diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/SizeConverter.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/SizeConverter.java
index d081064..e11e9f8 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/SizeConverter.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/SizeConverter.java
@@ -12,6 +12,7 @@
     public static final long BYTES_IN_KB = 1024L;
     public static final long BYTES_IN_MB = 1024L * 1024L;
     public static final long BYTES_IN_GB = 1024L * 1024L * 1024L;
+    public static final long BYTES_IN_TB = BYTES_IN_GB * 1024L;
 
     public SizeConverter() {
 
@@ -21,7 +22,8 @@
         BYTES(1),
         KB(2),
         MB(3),
-        GB(4);
+        GB(4),
+        TB(5);
 
         private long unitWeight;
 
@@ -49,14 +51,22 @@
         }
 
         public SizeUnit getUnit(long weight) {
-            return (SizeUnit) weightToUnit.get((int) weight).getSecond();
+            return weightToUnit.get((int) weight).getSecond();
+        }
+
+        public static SizeUnit getMaxHandledUnit() {
+            return weightToUnit.get(0).getSecond();
+        }
+
+        public static SizeUnit getMinHandledUnit() {
+            return weightToUnit.get(weightToUnit.size() - 1).getSecond();
         }
     };
 
     public static Number convert(long size, SizeUnit fromUnit, SizeUnit 
toUnit) {
         long fromType = fromUnit.getUnitWeight();
         long toType = toUnit.getUnitWeight();
-        return (size) * ((Math.pow(CONVERT_FACTOR, fromType)) / 
(Math.pow(CONVERT_FACTOR, toType)));
+        return size * (Math.pow(CONVERT_FACTOR, fromType) / 
Math.pow(CONVERT_FACTOR, toType));
     }
 
     public static Pair<SizeUnit, Double> autoConvert(long size, SizeUnit 
inUnit) {
@@ -68,4 +78,45 @@
         }
         return new Pair<SizeConverter.SizeUnit, Double>(SizeUnit.BYTES, 
(double)size);
     }
+
+    public static SizeUnit leastUnitInList(List<Pair<SizeUnit, Double>> 
operands) {
+        SizeUnit leastUnit = SizeUnit.getMaxHandledUnit();
+        for(Pair<SizeUnit, Double> operand : operands) {
+            if(operand.getFirst().getUnitWeight() < leastUnit.getUnitWeight()) 
{
+                leastUnit = operand.getFirst();
+            }
+        }
+        return leastUnit;
+    }
+
+    public static SizeUnit maxUnitInList(List<Pair<SizeUnit, Double>> 
operands) {
+        SizeUnit maxUnit = SizeUnit.getMinHandledUnit();
+        for(Pair<SizeUnit, Double> operand : operands) {
+            if(operand.getFirst().getUnitWeight() > maxUnit.getUnitWeight()) {
+                maxUnit = operand.getFirst();
+            }
+        }
+        return maxUnit;
+    }
+
+    @SafeVarargs
+    public static List<Pair<SizeUnit, Double>> 
getMathOperationSafeOperands(Pair<SizeUnit, Double>... operands) {
+        List<Pair<SizeUnit, Double>>  operationReadyOperands = new 
ArrayList<Pair<SizeUnit, Double>>();
+        List<Pair<SizeUnit, Double>> convertedOperands = new 
ArrayList<Pair<SizeUnit, Double>>();
+        for (Pair<SizeUnit, Double> operand : operands) {
+            convertedOperands.add(autoConvert(operand.getSecond().longValue(), 
operand.getFirst()));
+        }
+
+        SizeUnit finalUnit = leastUnitInList(convertedOperands);
+
+        for (Pair<SizeUnit, Double> operand : convertedOperands) {
+            if(operand.getFirst() != finalUnit) {
+                operationReadyOperands.add(new Pair<SizeConverter.SizeUnit, 
Double>(finalUnit,
+                        convert(operand.getSecond().longValue(), 
operand.getFirst(), finalUnit).doubleValue()));
+            } else {
+                operationReadyOperands.add(operand);
+            }
+        }
+        return operationReadyOperands;
+    }
 }
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
index da19b27..6f040c8 100644
--- 
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
@@ -1,24 +1,28 @@
 /*
-* 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.
-*/
+ * 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 java.util.ArrayList;
+import java.util.List;
+
 import org.junit.Test;
+import org.ovirt.engine.core.common.utils.SizeConverter.SizeUnit;
 
 public class SizeConverterTest {
     @Test
@@ -52,4 +56,32 @@
                 SizeConverter.SizeUnit.MB).intValue();
         assertEquals(megabytes, 3);
     }
+
+    @Test
+    public void testConvertMegaBytesToTB() {
+        long mb = 5 * 1024 * 1024;
+        int tbs = SizeConverter.convert(mb, SizeUnit.MB, 
SizeUnit.TB).intValue();
+        assertEquals(tbs, 5);
+    }
+
+    @Test
+    public void testGetMathOperationSafeOperands() {
+        List<Pair<SizeUnit, Double>> expected = new ArrayList<Pair<SizeUnit, 
Double>>() {
+            private static final long serialVersionUID = 1L;
+
+            {
+                add(new Pair<SizeUnit, Double>(SizeUnit.KB, 1D));
+                add(new Pair<SizeUnit, Double>(SizeUnit.KB, 1D));
+                add(new Pair<SizeUnit, Double>(SizeUnit.KB, 1024D));
+                add(new Pair<SizeUnit, Double>(SizeUnit.KB, 1024D * 1024D));
+            }
+        };
+        List<Pair<SizeUnit, Double>> actual = 
SizeConverter.getMathOperationSafeOperands(
+                new Pair<SizeUnit, Double>(SizeUnit.BYTES, 1024D),
+                new Pair<SizeUnit, Double>(SizeUnit.KB, 1D),
+                new Pair<SizeUnit, Double>(SizeUnit.MB, 1D),
+                new Pair<SizeUnit, Double>(SizeUnit.GB, 1D)
+                );
+        assertEquals(expected, actual);
+    }
 }


-- 
To view, visit https://gerrit.ovirt.org/40625
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie9fd4e7b7b9411cf17d66037e6424a979c2d037f
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.5-gluster
Gerrit-Owner: anmolbabu <anb...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to