dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #32215
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16453: minor performance optimization in calendar
------------------------------------------------------------
revno: 16453
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-08-20 17:58:24 +0700
message:
minor performance optimization in calendar
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/BiMonthlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyAbstractPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.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/BiMonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/BiMonthlyPeriodType.java 2014-08-11 05:27:09 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/BiMonthlyPeriodType.java 2014-08-20 10:58:24 +0000
@@ -86,25 +86,21 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
- DateUnit dateUnit = cal.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
- dateUnit = cal.plusMonths( dateUnit, 2 );
-
- return createPeriod( cal.toIso( dateUnit ), cal );
+ DateUnit dateUnit = calendar.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
+ dateUnit = calendar.plusMonths( dateUnit, 2 );
+
+ return createPeriod( calendar.toIso( dateUnit ), calendar );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
- DateUnit dateUnit = cal.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
- dateUnit = cal.minusMonths( dateUnit, 2 );
-
- return createPeriod( cal.toIso( dateUnit ), cal );
+ DateUnit dateUnit = calendar.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
+ dateUnit = calendar.minusMonths( dateUnit, 2 );
+
+ return createPeriod( calendar.toIso( dateUnit ), calendar );
}
/**
=== 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 2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/CalendarPeriodType.java 2014-08-20 10:58:24 +0000
@@ -29,6 +29,7 @@
*/
import com.google.common.collect.Lists;
+import org.hisp.dhis.calendar.Calendar;
import org.hisp.dhis.calendar.DateUnit;
import java.util.ArrayList;
@@ -63,18 +64,46 @@
* @param period the Period to base the next Period on.
* @return a Period which is the next of the given Period.
*/
- public abstract Period getNextPeriod( Period period );
-
- /**
- * Returns a Period which is the previous of the given Period. Only valid
- * Periods are returned. If the given Period is of different PeriodType than
- * the executing PeriodType, or the given Period is invalid, the returned
- * Period might overlap the given Period.
- *
- * @param period the Period to base the previous Period on.
- * @return a Period which is the previous of the given Period.
- */
- public abstract Period getPreviousPeriod( Period period );
+ public final Period getNextPeriod( Period period )
+ {
+ return getNextPeriod( period, getCalendar() );
+ }
+
+ /**
+ * Returns a Period which is the next of the given Period. Only valid
+ * Periods are returned. If the given Period is of different PeriodType than
+ * the executing PeriodType, or the given Period is invalid, the returned
+ * Period might overlap the given Period.
+ *
+ * @param period the Period to base the next Period on.
+ * @return a Period which is the next of the given Period.
+ */
+ public abstract Period getNextPeriod( Period period, Calendar calendar );
+
+ /**
+ * Returns a Period which is the previous of the given Period. Only valid
+ * Periods are returned. If the given Period is of different PeriodType than
+ * the executing PeriodType, or the given Period is invalid, the returned
+ * Period might overlap the given Period.
+ *
+ * @param period the Period to base the previous Period on.
+ * @return a Period which is the previous of the given Period.
+ */
+ public final Period getPreviousPeriod( Period period )
+ {
+ return getPreviousPeriod( period, getCalendar() );
+ }
+
+ /**
+ * Returns a Period which is the previous of the given Period. Only valid
+ * Periods are returned. If the given Period is of different PeriodType than
+ * the executing PeriodType, or the given Period is invalid, the returned
+ * Period might overlap the given Period.
+ *
+ * @param period the Period to base the previous Period on.
+ * @return a Period which is the previous of the given Period.
+ */
+ public abstract Period getPreviousPeriod( Period period, Calendar calendar );
/**
* Generates a list of Periods for a defined time span containing the given
@@ -152,10 +181,12 @@
Period period = createPeriod( startDate );
+ Calendar calendar = getCalendar();
+
while ( period.getStartDate().before( endDate ) )
{
periods.add( period );
- period = getNextPeriod( period );
+ period = getNextPeriod( period, calendar );
}
return periods;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java 2014-06-23 14:31:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/DailyPeriodType.java 2014-08-20 10:58:24 +0000
@@ -29,7 +29,6 @@
*/
import com.google.common.collect.Lists;
-
import org.hisp.dhis.calendar.Calendar;
import org.hisp.dhis.calendar.DateUnit;
@@ -86,27 +85,23 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.plusDays( dateUnit, 1 );
+ dateUnit = calendar.plusDays( dateUnit, 1 );
- Date date = cal.toIso( dateUnit ).toJdkDate();
+ Date date = calendar.toIso( dateUnit ).toJdkDate();
return new Period( this, date, date );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.minusDays( dateUnit, 1 );
+ dateUnit = calendar.minusDays( dateUnit, 1 );
- Date date = cal.toIso( dateUnit ).toJdkDate();
+ Date date = calendar.toIso( dateUnit ).toJdkDate();
return new Period( this, date, date );
}
@@ -142,7 +137,7 @@
public List<Period> generateRollingPeriods( DateUnit dateUnit )
{
Calendar cal = getCalendar();
-
+
dateUnit = cal.minusDays( dateUnit, 364 );
List<Period> periods = Lists.newArrayList();
@@ -172,7 +167,7 @@
public Date getRewindedDate( Date date, Integer rewindedPeriods )
{
Calendar cal = getCalendar();
-
+
date = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java 2014-06-23 14:46:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/FinancialPeriodType.java 2014-08-20 10:58:24 +0000
@@ -29,7 +29,6 @@
*/
import com.google.common.collect.Lists;
-
import org.hisp.dhis.calendar.Calendar;
import org.hisp.dhis.calendar.DateUnit;
@@ -92,25 +91,21 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.plusYears( dateUnit, 1 );
+ dateUnit = calendar.plusYears( dateUnit, 1 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.minusYears( dateUnit, 1 );
+ dateUnit = calendar.minusYears( dateUnit, 1 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
/**
@@ -121,7 +116,7 @@
public List<Period> generatePeriods( DateUnit dateUnit )
{
Calendar cal = getCalendar();
-
+
boolean past = dateUnit.getMonth() >= (getBaseMonth() + 1);
List<Period> periods = Lists.newArrayList();
@@ -159,7 +154,7 @@
public List<Period> generateLast5Years( Date date )
{
Calendar cal = getCalendar();
-
+
DateUnit dateUnit = createLocalDateUnitInstance( date );
boolean past = dateUnit.getMonth() >= (getBaseMonth() + 1);
@@ -182,7 +177,7 @@
public Date getRewindedDate( Date date, Integer rewindedPeriods )
{
Calendar cal = getCalendar();
-
+
date = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java 2014-06-23 14:46:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/MonthlyPeriodType.java 2014-08-20 10:58:24 +0000
@@ -29,7 +29,6 @@
*/
import com.google.common.collect.Lists;
-
import org.hisp.dhis.calendar.Calendar;
import org.hisp.dhis.calendar.DateUnit;
@@ -94,25 +93,21 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
- DateUnit dateUnit = cal.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
- dateUnit = cal.plusMonths( dateUnit, 1 );
+ DateUnit dateUnit = calendar.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
+ dateUnit = calendar.plusMonths( dateUnit, 1 );
- return createPeriod( cal.toIso( dateUnit ), cal );
+ return createPeriod( calendar.toIso( dateUnit ), calendar );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
- DateUnit dateUnit = cal.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
- dateUnit = cal.minusMonths( dateUnit, 1 );
+ DateUnit dateUnit = calendar.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
+ dateUnit = calendar.minusMonths( dateUnit, 1 );
- return createPeriod( cal.toIso( dateUnit ), cal );
+ return createPeriod( calendar.toIso( dateUnit ), calendar );
}
/**
@@ -123,7 +118,7 @@
public List<Period> generatePeriods( DateUnit dateUnit )
{
Calendar cal = getCalendar();
-
+
dateUnit.setMonth( 1 );
dateUnit.setDay( 1 );
@@ -148,7 +143,7 @@
public List<Period> generateRollingPeriods( DateUnit dateUnit )
{
Calendar cal = getCalendar();
-
+
dateUnit.setDay( 1 );
dateUnit = cal.minusMonths( dateUnit, 11 );
@@ -179,7 +174,7 @@
public Date getRewindedDate( Date date, Integer rewindedPeriods )
{
Calendar cal = getCalendar();
-
+
date = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java 2014-06-23 14:46:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/QuarterlyPeriodType.java 2014-08-20 10:58:24 +0000
@@ -75,7 +75,7 @@
{
DateUnit start = new DateUnit( dateUnit );
- start.setMonth( ( ( dateUnit.getMonth() - 1 ) - ( ( dateUnit.getMonth() - 1 ) % 3 ) ) + 1 );
+ start.setMonth( ((dateUnit.getMonth() - 1) - ((dateUnit.getMonth() - 1) % 3)) + 1 );
start.setDay( 1 );
DateUnit end = new DateUnit( start );
@@ -96,25 +96,21 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, org.hisp.dhis.calendar.Calendar calendar )
{
- org.hisp.dhis.calendar.Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.plusMonths( dateUnit, 3 );
+ dateUnit = calendar.plusMonths( dateUnit, 3 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, org.hisp.dhis.calendar.Calendar calendar )
{
- org.hisp.dhis.calendar.Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.minusMonths( dateUnit, 3 );
+ dateUnit = calendar.minusMonths( dateUnit, 3 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
/**
@@ -125,7 +121,7 @@
public List<Period> generatePeriods( DateUnit dateUnit )
{
org.hisp.dhis.calendar.Calendar cal = getCalendar();
-
+
dateUnit.setMonth( 1 );
dateUnit.setDay( 1 );
@@ -157,7 +153,7 @@
public List<Period> generateRollingPeriods( DateUnit dateUnit )
{
org.hisp.dhis.calendar.Calendar cal = getCalendar();
-
+
dateUnit.setDay( 1 );
dateUnit = cal.minusMonths( dateUnit, 9 );
@@ -238,7 +234,7 @@
public Date getRewindedDate( Date date, Integer rewindedPeriods )
{
org.hisp.dhis.calendar.Calendar cal = getCalendar();
-
+
date = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyAbstractPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyAbstractPeriodType.java 2014-06-23 14:46:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/SixMonthlyAbstractPeriodType.java 2014-08-20 10:58:24 +0000
@@ -96,25 +96,21 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
- DateUnit dateUnit = cal.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
- dateUnit = cal.plusMonths( dateUnit, 6 );
+ DateUnit dateUnit = calendar.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
+ dateUnit = calendar.plusMonths( dateUnit, 6 );
- return createPeriod( cal.toIso( dateUnit ), cal );
+ return createPeriod( calendar.toIso( dateUnit ), calendar );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
- DateUnit dateUnit = cal.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
- dateUnit = cal.minusMonths( dateUnit, 6 );
+ DateUnit dateUnit = calendar.fromIso( DateUnit.fromJdkDate( period.getStartDate() ) );
+ dateUnit = calendar.minusMonths( dateUnit, 6 );
- return createPeriod( cal.toIso( dateUnit ), cal );
+ return createPeriod( calendar.toIso( dateUnit ), calendar );
}
/**
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java 2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/TwoYearlyPeriodType.java 2014-08-20 10:58:24 +0000
@@ -99,7 +99,7 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, org.hisp.dhis.calendar.Calendar calendar )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 + 2 );
@@ -114,7 +114,7 @@
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, org.hisp.dhis.calendar.Calendar calendar )
{
Calendar cal = createCalendarInstance( period.getStartDate() );
cal.set( Calendar.YEAR, cal.get( Calendar.YEAR ) - cal.get( Calendar.YEAR ) % 2 - 2 );
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java 2014-06-27 10:28:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.java 2014-08-20 10:58:24 +0000
@@ -96,25 +96,21 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.plusWeeks( dateUnit, 1 );
+ dateUnit = calendar.plusWeeks( dateUnit, 1 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.minusWeeks( dateUnit, 1 );
+ dateUnit = calendar.minusWeeks( dateUnit, 1 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
/**
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java 2014-06-23 14:46:45 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/YearlyPeriodType.java 2014-08-20 10:58:24 +0000
@@ -96,25 +96,21 @@
// -------------------------------------------------------------------------
@Override
- public Period getNextPeriod( Period period )
+ public Period getNextPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.plusYears( dateUnit, 1 );
+ dateUnit = calendar.plusYears( dateUnit, 1 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
@Override
- public Period getPreviousPeriod( Period period )
+ public Period getPreviousPeriod( Period period, Calendar calendar )
{
- Calendar cal = getCalendar();
-
DateUnit dateUnit = createLocalDateUnitInstance( period.getStartDate() );
- dateUnit = cal.minusYears( dateUnit, 1 );
+ dateUnit = calendar.minusYears( dateUnit, 1 );
- return createPeriod( dateUnit, cal );
+ return createPeriod( dateUnit, calendar );
}
/**