← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5922: Impl method CalendarPeriodType getPeriods( Date, Date )

 

------------------------------------------------------------
revno: 5922
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2012-02-12 18:57:41 +0100
message:
  Impl method CalendarPeriodType getPeriods( Date, Date )
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java	2012-02-12 17:57:41 +0000
@@ -121,4 +121,28 @@
         
         return periods;
     }
+    
+    /**
+     * Generates a list of all Periods between the given start and end date. The
+     * first period will span the start date. The last period will span the end
+     * date.
+     * 
+     * @param startDate the start date.
+     * @param endDate the end date.
+     * @return a list of Periods for the defined time span.
+     */
+    public List<Period> generatePeriods( Date startDate, Date endDate )
+    {
+        List<Period> periods = new ArrayList<Period>();
+        
+        Period period = createPeriod( startDate );
+        
+        while ( period.getStartDate().before( endDate ) )
+        {
+            periods.add( period );
+            period = getNextPeriod( period );
+        }
+        
+        return periods;
+    }
 }

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/MonthlyPeriodTypeTest.java	2012-02-12 17:57:41 +0000
@@ -174,4 +174,22 @@
         assertEquals( periodType.createPeriod( new Cal( 2005, 11, 1 ).time() ), periods.get( 10 ) );
         assertEquals( periodType.createPeriod( new Cal( 2005, 12, 1 ).time() ), periods.get( 11 ) );
     }
+    
+    @Test
+    public void testGeneratePeriodsBetweenDates()
+    {
+        startCal.set( 2009, 8, 15 );
+        endCal.set( 2010, 2, 20 );
+        
+        List<Period> periods = periodType.generatePeriods( startCal.time(), endCal.time() );
+
+        assertEquals( 7, periods.size() );
+        assertEquals( periodType.createPeriod( new Cal( 2009, 8, 1 ).time() ), periods.get( 0 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2009, 9, 1 ).time() ), periods.get( 1 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2009, 10, 1 ).time() ), periods.get( 2 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2009, 11, 1 ).time() ), periods.get( 3 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2009, 12, 1 ).time() ), periods.get( 4 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2010, 1, 1 ).time() ), periods.get( 5 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2010, 2, 1 ).time() ), periods.get( 6 ) );
+    }
 }

=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/period/QuarterlyPeriodTypeTest.java	2012-02-12 17:57:41 +0000
@@ -151,4 +151,18 @@
         assertEquals( periodType.createPeriod( new Cal( 2005, 7, 1 ).time() ), periods.get( 2 ) );
         assertEquals( periodType.createPeriod( new Cal( 2005, 10, 1 ).time() ), periods.get( 3 ) );
     }
+
+    @Test
+    public void testGeneratePeriodsBetweenDates()
+    {
+        startCal.set( 2009, 8, 15 );
+        endCal.set( 2010, 2, 20 );
+        
+        List<Period> periods = periodType.generatePeriods( startCal.time(), endCal.time() );
+
+        assertEquals( 3, periods.size() );
+        assertEquals( periodType.createPeriod( new Cal( 2009, 7, 1 ).time() ), periods.get( 0 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2009, 10, 1 ).time() ), periods.get( 1 ) );
+        assertEquals( periodType.createPeriod( new Cal( 2010, 1, 1 ).time() ), periods.get( 2 ) );
+    }
 }