Last active
September 24, 2019 14:43
-
-
Save DaHoC/0315b380b1c59934fab4 to your computer and use it in GitHub Desktop.
Calculate potential overlap for 2 given timespans with begin and end
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.playground.test; | |
import java.time.Instant; | |
public class TimespanOverlap { | |
private Instant begin; | |
private Instant end; | |
public TimespanOverlap(Instant begin, Instant end) { | |
this.begin = begin; | |
this.end = end; | |
} | |
public Instant getBegin() { | |
return begin; | |
} | |
public Instant getEnd() { | |
return end; | |
} | |
@Override | |
public String toString() { | |
return "TimespanOverlap{" + | |
"begin=" + begin + | |
", end=" + end + | |
'}'; | |
} | |
/** | |
* Checks if this timespan overlaps with given timespan. | |
* @param otherTimespan another timespan to check against for overlaps | |
* @return <code>true</code> if overlap is found, <code>false</code> otherwise | |
*/ | |
public final boolean isOverlapping(final TimespanOverlap otherTimespan) { | |
assert (begin != null && end != null); | |
assert (otherTimespan.getBegin() != null && otherTimespan.getEnd() != null); | |
final TimespanOverlap overlap = getOverlapWith(otherTimespan); | |
// Only when begin of overlap candidate is before or at end of overlap it is really an overlap | |
return (overlap.getBegin().isBefore(overlap.getEnd())); | |
} | |
/** | |
* Calculate real overlap, so the latest begin time of the two timespans and the earliest end times. | |
* | |
* @param otherTimespan another timespan to check against for overlaps | |
* @return overlap | |
*/ | |
public final TimespanOverlap getOverlapWith(final TimespanOverlap otherTimespan) { | |
Instant maxBeginTime = begin.isAfter(otherTimespan.getBegin()) ? begin : otherTimespan.getBegin(); | |
Instant minEndTime = end.isBefore(otherTimespan.getEnd()) ? end : otherTimespan.getEnd(); | |
return new TimespanOverlap(maxBeginTime, minEndTime); | |
} | |
public static void main(String[] args) { | |
TimespanOverlap o1 = new TimespanOverlap(Instant.ofEpochSecond(10), Instant.ofEpochSecond(30)); | |
TimespanOverlap o2 = new TimespanOverlap(Instant.ofEpochSecond(20), Instant.ofEpochSecond(40)); | |
TimespanOverlap o1OverlapWithO2 = o1.getOverlapWith(o2); | |
TimespanOverlap o2OverlapWithO1 = o2.getOverlapWith(o1); | |
System.out.println(o1OverlapWithO2); | |
System.out.println(o2OverlapWithO1); | |
System.out.println(o1.isOverlapping(o2)); | |
System.out.println(o2.isOverlapping(o1)); | |
TimespanOverlap o3 = new TimespanOverlap(Instant.ofEpochSecond(50), Instant.ofEpochSecond(60)); | |
TimespanOverlap o4 = new TimespanOverlap(Instant.ofEpochSecond(60), Instant.ofEpochSecond(70)); | |
TimespanOverlap o3OverlapWithO4 = o3.getOverlapWith(o4); | |
TimespanOverlap o4OverlapWithO3 = o4.getOverlapWith(o3); | |
System.out.println(o3OverlapWithO4); | |
System.out.println(o4OverlapWithO3); | |
System.out.println(o3.isOverlapping(o4)); | |
System.out.println(o4.isOverlapping(o3)); | |
TimespanOverlap o5 = new TimespanOverlap(Instant.ofEpochSecond(10), Instant.ofEpochSecond(20)); | |
TimespanOverlap o6 = new TimespanOverlap(Instant.ofEpochSecond(30), Instant.ofEpochSecond(40)); | |
TimespanOverlap o5OverlapWithO6 = o5.getOverlapWith(o6); | |
TimespanOverlap o6OverlapWithO5 = o6.getOverlapWith(o5); | |
System.out.println(o5OverlapWithO6); | |
System.out.println(o6OverlapWithO5); | |
System.out.println(o5.isOverlapping(o6)); | |
System.out.println(o6.isOverlapping(o5)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment