| | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | @Data |
| | | @AllArgsConstructor |
| | |
| | | |
| | | 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); |
| | | } |
| | | } |