| | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.Stack; |
| | | |
| | | @Data |
| | | @AllArgsConstructor |
| | |
| | | |
| | | public Duration getDuration() { |
| | | return Duration.between(start, end); |
| | | } |
| | | |
| | | public static List<Calculate> calculateRemainingCalculate(Calculate mainPeriod, Calculate... excludedPeriods) { |
| | | List<Calculate> remainPeriods = new ArrayList<>(); |
| | | remainPeriods.add(mainPeriod); |
| | | |
| | | for (Calculate excluded : excludedPeriods) { |
| | | List<Calculate> newRemaining = new ArrayList<>(); |
| | | for (Calculate remain : remainPeriods) { |
| | | newRemaining.addAll(subtractPeriod(remain, excluded)); |
| | | } |
| | | remainPeriods = newRemaining; |
| | | } |
| | | |
| | | return remainPeriods; |
| | | } |
| | | |
| | | public static Duration calculateRemainingDuration(Calculate mainPeriod, Calculate... excludedPeriods) { |
| | |
| | | |
| | | return new Calculate(overlapStart, overlapEnd); |
| | | } |
| | | |
| | | /** |
| | | * 删除重复时间,合并相连的时间段 |
| | | * @param result 结果集合 |
| | | * @param current 当前处理时间段 |
| | | * @return |
| | | */ |
| | | public static Stack<Calculate> mergeTimeRanges(Stack<Calculate> result, Calculate current) { |
| | | // 删除重复时间,合并相连的时间段 |
| | | if (result.empty()) { |
| | | result.push(current); |
| | | return result; |
| | | } |
| | | Calculate last = result.peek(); |
| | | if (last.getEnd().isBefore(current.getStart())) { |
| | | result.push(current); |
| | | } else if (last.getEnd().isBefore(current.getEnd())) { |
| | | last.setEnd(current.getEnd()); |
| | | } |
| | | return result; |
| | | } |
| | | } |