feat: add Interval record to represent start and end points with overlap functionality
This commit is contained in:
18
src/main/java/de/th_luebeck/ws25/Interval.java
Normal file
18
src/main/java/de/th_luebeck/ws25/Interval.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package de.th_luebeck.ws25;
|
||||||
|
|
||||||
|
public record Interval(int start, int end) {
|
||||||
|
|
||||||
|
public int length() {
|
||||||
|
return end - start + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean overlaps(Interval other) {
|
||||||
|
return this.start <= other.end && other.start <= this.end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int overlapSize(Interval other) {
|
||||||
|
int a = Math.max(this.start, other.start);
|
||||||
|
int b = Math.min(this.end, other.end);
|
||||||
|
return Math.max(0, b - a + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user