| | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | import java.time.Duration; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Comparator; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.Stack; |
| | | |
| | | @Data |
| | | @AllArgsConstructor |
| | |
| | | public int compareTo(Calculate o) { |
| | | return start.compareTo(o.start); |
| | | } |
| | | |
| | | 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) { |
| | | 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; |
| | | } |
| | | |
| | | // 计算所有剩余时间段的总时长 |
| | | Duration total = Duration.ZERO; |
| | | for (Calculate period : remainPeriods) { |
| | | total = total.plus(period.getDuration()); |
| | | } |
| | | |
| | | return total; |
| | | } |
| | | |
| | | private static List<Calculate> subtractPeriod(Calculate main, Calculate toSubtract) { |
| | | List<Calculate> result = new ArrayList<>(); |
| | | |
| | | // 没有重叠的情况 |
| | | if (main.getEnd().compareTo(toSubtract.getStart()) <= 0 || |
| | | main.getStart().compareTo(toSubtract.getEnd()) >= 0) { |
| | | result.add(main); |
| | | return result; |
| | | } |
| | | |
| | | // 时间段完全包含在要减去的时间段中 |
| | | if (main.getStart().compareTo(toSubtract.getStart()) >= 0 && |
| | | main.getEnd().compareTo(toSubtract.getEnd()) <= 0) { |
| | | return result; // 返回空列表 |
| | | } |
| | | |
| | | // 时间段部分重叠 |
| | | if (main.getStart().compareTo(toSubtract.getStart()) < 0) { |
| | | // 左边有剩余 |
| | | result.add(new Calculate(main.getStart(), toSubtract.getStart())); |
| | | } |
| | | |
| | | if (main.getEnd().compareTo(toSubtract.getEnd()) > 0) { |
| | | // 右边有剩余 |
| | | result.add(new Calculate(toSubtract.getEnd(), main.getEnd())); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 判断与另一个时间区间是否有交集 |
| | | */ |
| | | public Boolean overlapsWith(Calculate otherCalculate) { |
| | | Objects.requireNonNull(otherCalculate, "另一个时间区间不能为null"); |
| | | return !this.end.isBefore(otherCalculate.start) && !otherCalculate.end.isBefore(this.start); |
| | | } |
| | | |
| | | /** |
| | | * 获取交集区间(没有返回null) |
| | | */ |
| | | public Calculate getOverlap(Calculate other) { |
| | | if (!overlapsWith(other)) { |
| | | return null; |
| | | } |
| | | |
| | | LocalDateTime overlapStart = this.start.isAfter(other.start) ? this.start : other.start; |
| | | LocalDateTime overlapEnd = this.end.isBefore(other.end) ? this.end : other.end; |
| | | |
| | | 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; |
| | | } |
| | | } |