anmolbabu has uploaded a new change for review.

Change subject: webadmin : Introduce TimeUnitConverter
......................................................................

webadmin : Introduce TimeUnitConverter

This patch introduces TimeUnitConverter that converts
(approximately) the input difference in times(long)
to a maximum expressable time unit.Currently it goes
upto Years.
The time expressed by this patch is just an approximate
bcoz the code assumes for simplicity every month to be
of 30 days.
Also,this patch just returns the integral magnitude and
unit of the highest expressable unit.

Change-Id: I045aaa4f93b345e0457e1a0ce2ccf0ceeb9071ad
Signed-off-by: Anmol Babu <[email protected]>
---
A 
frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/utils/TimeUnitConverter.java
A 
frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/TimeUnitConverterTest.java
2 files changed, 188 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/69/27469/1

diff --git 
a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/utils/TimeUnitConverter.java
 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/utils/TimeUnitConverter.java
new file mode 100644
index 0000000..b400ae4
--- /dev/null
+++ 
b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/utils/TimeUnitConverter.java
@@ -0,0 +1,126 @@
+package org.ovirt.engine.ui.frontend.utils;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.ovirt.engine.core.common.utils.Pair;
+
+public class TimeUnitConverter {
+
+    public static enum TimeUnit {
+
+        /*
+         * The weights of the units here are their equivalent number of 
previous units
+         */
+
+        Seconds(1 , 1) , //Base unit assumed here for all practical purposes 
is seconds.And 1 sec = 1 sec so weight = 1
+        Minutes(2 , 60) , //1 minute = 60s
+        Hours(3 , 60) , //1 hour = 60 mins
+        Days(4 , 24) , //1 day = 24 hrs
+        Months(5 , 30) , //1 months = 30 days
+        Years(6 , 12); //1 year = 12 months.So,this assumes 12 * 30 = 360 days 
as 1 year
+
+        private static List<Pair<TimeUnitConverter.TimeUnit, Long>> timeUnits 
= new ArrayList<Pair<TimeUnitConverter.TimeUnit, Long>>();
+        /*
+         * List of pairs required because to facilitate
+         * easy mapping between the unit name and its weight.
+         * And this list is later ordered in accordance with the units order 
in the unit hierarchy.
+         */
+
+        private long value;
+        private int order;
+
+        private TimeUnit(int order , long weight) {
+            this.value = weight;
+            this.order = order;
+        }
+
+        static {
+            for(TimeUnit timeUnit : TimeUnit.values()) {
+                timeUnits.add(new Pair<TimeUnitConverter.TimeUnit, 
Long>(timeUnit, timeUnit.getWeight()));
+            }
+            /*
+             * Sort  the pair list in descending order of unit's order value.
+             */
+            Collections.sort(timeUnits, new 
Comparator<Pair<TimeUnitConverter.TimeUnit, Long>>() {
+                @Override
+                public int compare(Pair<TimeUnitConverter.TimeUnit, Long> o1, 
Pair<TimeUnitConverter.TimeUnit, Long> o2) {
+                    return o1.getFirst().getOrder() - o2.getFirst().getOrder();
+                }
+            });
+        }
+
+        public long getWeight() {
+            return value;
+        }
+
+        public void setWeight(long weight) {
+            this.value = weight;
+        }
+
+        public int getOrder() {
+            return order;
+        }
+
+        public void setOrder(int order) {
+            this.order = order;
+        }
+    }
+
+    public static Pair<Long, TimeUnitConverter.TimeUnit> convert(long inValue, 
TimeUnit inUnit, TimeUnit outUnit) {
+        /*
+         * Iterate over the list starting from the outUnit all the way upto 
inUnit and divide/multiply the inValue
+         * by the weights of the units iterated so far.
+         */
+        if(inUnit.getOrder() < outUnit.getOrder()) {
+            //The conversion is from a lower to a upper unit.
+            for(int i = outUnit.getOrder(); i > inUnit.getOrder(); i-- ) {
+                /*
+                 * Iterate over the list starting from the outUnit all the way 
upto inUnit and divide the inValue
+                 * by the weights of the units iterated so far.
+                 */
+                inValue = inValue/TimeUnit.timeUnits.get(i-1).getSecond();
+            }
+        } else {
+            //The conversion is from a bigger unit to a smaller unit.
+            for(int i = outUnit.getOrder(); i < inUnit.getOrder(); i++ ) {
+                /*
+                 * Iterate over the list starting from the outUnit all the way 
upto inUnit and multiply the inValue
+                 * by the weights of the units iterated so far.
+                 */
+                inValue = inValue * TimeUnit.timeUnits.get(i).getSecond();
+            }
+        }
+        Pair<Long , TimeUnitConverter.TimeUnit> timePair = new Pair<Long, 
TimeUnitConverter.TimeUnit>(inValue, outUnit);
+        //The result is a pair of converted value in its HIGHEST POSSIBLE unit 
and the unit.
+        return timePair;
+    }
+
+    public static Pair<Long, TimeUnitConverter.TimeUnit> autoConvert(long 
inValue, TimeUnit inUnit) {
+        int i = inUnit.getOrder();
+        Pair<Long, TimeUnitConverter.TimeUnit> result = new Pair<Long, 
TimeUnitConverter.TimeUnit>();
+        int product = 1;
+        while(product <= inValue && i <= 5) {
+            product *= TimeUnit.timeUnits.get(i).getSecond();
+            result.setSecond(TimeUnit.timeUnits.get(i).getFirst());
+            if(product > inValue) {
+                /*
+                 * If product exceeds the value, undo last 
transaction(multiplication and increment in i).
+                 */
+                product /= TimeUnit.timeUnits.get(i).getSecond();
+                i--;
+                result.setSecond(TimeUnit.timeUnits.get(i).getFirst());
+                break;
+            }
+            if(i == 5) {
+                break;
+            }
+            i++;
+        }
+        result.setFirst(inValue/product);
+      //The result is a pair of converted value in its HIGHEST POSSIBLE unit 
and the unit.
+        return result;
+    }
+}
diff --git 
a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/TimeUnitConverterTest.java
 
b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/TimeUnitConverterTest.java
new file mode 100644
index 0000000..3b87158
--- /dev/null
+++ 
b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/TimeUnitConverterTest.java
@@ -0,0 +1,62 @@
+package org.ovirt.engine.ui.frontend;
+
+import java.util.Date;
+
+import org.junit.Test;
+import org.ovirt.engine.core.common.utils.Pair;
+import org.ovirt.engine.ui.frontend.utils.TimeUnitConverter;
+import org.ovirt.engine.ui.frontend.utils.TimeUnitConverter.TimeUnit;
+
+import static org.junit.Assert.assertEquals;
+
+public class TimeUnitConverterTest {
+    @Test
+    public void testConvertSecondsToMinutes() {
+        long seconds = 1800L;
+        TimeUnit inUnit = TimeUnit.Seconds;
+        TimeUnit outUnit = TimeUnit.Minutes;
+        Pair<Long, TimeUnit> expectedResults = 
TimeUnitConverter.convert(seconds, inUnit, outUnit);
+        Pair<Long, TimeUnit> result = new Pair<Long, 
TimeUnitConverter.TimeUnit>(30L , TimeUnit.Minutes);
+        assertEquals(expectedResults.getFirst() , result.getFirst());
+    }
+
+    public void testMonthsInterval() {
+        Date oldDate = new Date(2013, 07, 02, 10, 30, 00);
+        Date newDate = new Date(2014, 02, 27, 14, 06, 00);
+        long interval = newDate.getTime() - oldDate.getTime();
+        long intervalInSeconds = interval/1000;
+        Pair<Long, TimeUnit> expectedResults = 
TimeUnitConverter.autoConvert(intervalInSeconds, TimeUnit.Seconds);
+        Pair<Long, TimeUnit> result = new Pair<Long, 
TimeUnitConverter.TimeUnit>(8L , TimeUnit.Months);
+        assertEquals(expectedResults.getFirst() , result.getFirst());
+    }
+
+    @Test
+    public void testConvertMinutesToHours() {
+        long minutes = 720L;
+        TimeUnit inUnit = TimeUnit.Minutes;
+        TimeUnit outUnit = TimeUnit.Hours;
+        Pair<Long, TimeUnit> expectedResults = 
TimeUnitConverter.convert(minutes, inUnit, outUnit);
+        Pair<Long, TimeUnit> result = new Pair<Long, 
TimeUnitConverter.TimeUnit>(12L , TimeUnit.Hours);
+        assertEquals(expectedResults.getFirst() , result.getFirst());
+    }
+
+    @Test
+    public void testConvertHoursToSeconds() {
+        long hours = 12L;
+        TimeUnit inUnit = TimeUnit.Hours;
+        TimeUnit outUnit = TimeUnit.Seconds;
+        Pair<Long, TimeUnit> expectedResults = 
TimeUnitConverter.convert(hours, inUnit, outUnit);
+        Pair<Long, TimeUnit> result = new Pair<Long, 
TimeUnitConverter.TimeUnit>(43200L , TimeUnit.Seconds);
+        assertEquals(expectedResults.getFirst() , result.getFirst());
+    }
+
+    @Test
+    public void testYearsInterval() {
+        Date oldDate = new Date(1991, 06, 28, 15, 40, 45);
+        Date newDate = new Date(2014, 06, 28, 15, 40, 45);
+        long intervalInSeconds = newDate.getTime()/1000 - 
oldDate.getTime()/1000;
+        Pair<Long, TimeUnit> expectedResults = 
TimeUnitConverter.autoConvert(intervalInSeconds, TimeUnit.Seconds);
+        Pair<Long, TimeUnit> result = new Pair<Long, 
TimeUnitConverter.TimeUnit>(23L , TimeUnit.Years);
+        assertEquals(expectedResults.getFirst() , result.getFirst());
+    }
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I045aaa4f93b345e0457e1a0ce2ccf0ceeb9071ad
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: anmolbabu <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to