aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-02-10 23:09:10 +0000
committerPJ Fanning <fanningpj@apache.org>2022-02-10 23:09:10 +0000
commit8a6966999ab18477aa7c115cc3bd82a5dc84ae61 (patch)
treec8559edd221930378b7d21b7545428464d4dacca
parent98091532bf3faf3a338c89e557db7189d956de48 (diff)
downloadpoi-8a6966999ab18477aa7c115cc3bd82a5dc84ae61.tar.gz
poi-8a6966999ab18477aa7c115cc3bd82a5dc84ae61.zip
refactor workday code
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897950 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java47
1 files changed, 42 insertions, 5 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java
index f6293f5e20..98988b5be4 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java
@@ -17,8 +17,11 @@
package org.apache.poi.ss.formula.atp;
+import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.util.LocaleUtil;
@@ -30,6 +33,35 @@ import org.apache.poi.util.Removal;
public class WorkdayCalculator {
public static final WorkdayCalculator instance = new WorkdayCalculator();
+ private static final Set<Integer> standardWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.SATURDAY, Calendar.SUNDAY}));
+ private static final Set<Integer> sunMonWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.SUNDAY, Calendar.MONDAY}));
+ private static final Set<Integer> monTuesWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.MONDAY, Calendar.TUESDAY}));
+ private static final Set<Integer> tuesWedsWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.TUESDAY, Calendar.WEDNESDAY}));
+ private static final Set<Integer> wedsThursWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.WEDNESDAY, Calendar.THURSDAY}));
+ private static final Set<Integer> thursFriWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.THURSDAY, Calendar.FRIDAY}));
+ private static final Set<Integer> friSatWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.FRIDAY, Calendar.SATURDAY}));
+ private static final Set<Integer> monWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.MONDAY}));
+ private static final Set<Integer> tuesWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.TUESDAY}));
+ private static final Set<Integer> wedsWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.WEDNESDAY}));
+ private static final Set<Integer> thursWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.THURSDAY}));
+ private static final Set<Integer> friWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.FRIDAY}));
+ private static final Set<Integer> satWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.SATURDAY}));
+ private static final Set<Integer> sunWeekend =
+ new HashSet<>(Arrays.asList(new Integer[]{Calendar.SUNDAY}));
+
/**
* Constructor.
*/
@@ -46,10 +78,12 @@ public class WorkdayCalculator {
* @return number of workdays between start and end dates, including both dates.
*/
public int calculateWorkdays(double start, double end, double[] holidays) {
- int saturdaysPast = this.pastDaysOfWeek(start, end, Calendar.SATURDAY);
- int sundaysPast = this.pastDaysOfWeek(start, end, Calendar.SUNDAY);
+ Integer[] weekendDays = new Integer[standardWeekend.size()];
+ weekendDays = standardWeekend.toArray(weekendDays);
+ int weekendDay1Past = weekendDays.length == 0 ? 0 : this.pastDaysOfWeek(start, end, weekendDays[0]);
+ int weekendDay2Past = weekendDays.length <= 1 ? 0 : this.pastDaysOfWeek(start, end, weekendDays[1]);
int nonWeekendHolidays = this.calculateNonWeekendHolidays(start, end, holidays);
- return (int) (end - start + 1) - saturdaysPast - sundaysPast - nonWeekendHolidays;
+ return (int) (end - start + 1) - weekendDay1Past - weekendDay2Past - nonWeekendHolidays;
}
/**
@@ -131,8 +165,11 @@ public class WorkdayCalculator {
}
private boolean isWeekend(Calendar date) {
- return date.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
- && date.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY;
+ return isWeekend(date, standardWeekend);
+ }
+
+ private boolean isWeekend(Calendar date, Set<Integer> weekendDays) {
+ return weekendDays.contains(date.get(Calendar.DAY_OF_WEEK));
}
/**