mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-25 05:23:51 +00:00
Merge branch 'timerange' into gpsfixstore-independent-of-race
This commit is contained in:
commit
7e40a96de9
3 files changed
+154
No files matched your search
@@ -0,0 +1,37 @@
|
||||
package com.sap.sailing.domain.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* A TimeRange defined by a starting timepoint {@link #from()}, and an ending timepoint {@link #to()}, which are both inclusive.
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public interface TimeRange extends Comparable<TimeRange>, Serializable {
|
||||
TimePoint from();
|
||||
TimePoint to();
|
||||
|
||||
/**
|
||||
* Also returns true if (either or both) the from or to timepoints are equal.
|
||||
* E.g. 10-100 lies within 10-00, and 10-50 lies within 10-100.
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
boolean liesWithin(TimeRange other);
|
||||
|
||||
/**
|
||||
* Reverse operation of {@link #liesWithin(TimeRange)}.
|
||||
* x.includes(y) = y.liesWithin(x)
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
boolean includes(TimeRange other);
|
||||
|
||||
boolean includes(TimePoint timePoint);
|
||||
|
||||
boolean intersects(TimeRange other);
|
||||
|
||||
boolean startsBefore(TimeRange other);
|
||||
|
||||
boolean endsAfter(TimeRange other);
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.sap.sailing.domain.common.impl;
|
||||
|
||||
import com.sap.sailing.domain.common.TimePoint;
|
||||
import com.sap.sailing.domain.common.TimeRange;
|
||||
import com.sap.sailing.domain.common.impl.Util.Pair;
|
||||
|
||||
public class TimeRangeImpl extends Pair<TimePoint, TimePoint> implements TimeRange {
|
||||
private static final long serialVersionUID = 8710198176227507300L;
|
||||
|
||||
public TimeRangeImpl(TimePoint from, TimePoint to) {
|
||||
super(from, to);
|
||||
if (from.after(to)) throw new IllegalArgumentException(String.format("from (%s) must lie before to (%s) in a TimeRange", from, to));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(TimeRange arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimePoint from() {
|
||||
return getA();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimePoint to() {
|
||||
return getB();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean liesWithin(TimeRange other) {
|
||||
return from().compareTo(other.from()) >= 0 && to().compareTo(other.to()) <=0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean includes(TimeRange other) {
|
||||
return other.liesWithin(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean intersects(TimeRange other) {
|
||||
return includes(other.from()) || includes(other.to()) || liesWithin(other);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean includes(TimePoint timePoint) {
|
||||
return from().compareTo(timePoint) <= 0 && to().compareTo(timePoint) >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startsBefore(TimeRange other) {
|
||||
return from().before(other.from());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean endsAfter(TimeRange other) {
|
||||
return to().after(other.to());
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.sap.sailing.domain.common.test;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sap.sailing.domain.common.TimePoint;
|
||||
import com.sap.sailing.domain.common.TimeRange;
|
||||
import com.sap.sailing.domain.common.impl.MillisecondsTimePoint;
|
||||
import com.sap.sailing.domain.common.impl.TimeRangeImpl;
|
||||
|
||||
public class TimeRangeTest {
|
||||
protected TimeRange create(long from, long to) {
|
||||
return new TimeRangeImpl(new MillisecondsTimePoint(from), new MillisecondsTimePoint(to));
|
||||
}
|
||||
|
||||
protected TimePoint create(long millis) {
|
||||
return new MillisecondsTimePoint(millis);
|
||||
}
|
||||
|
||||
private void liesWithin(long fromOuter, long toOuter, long fromInner, long toInner) {
|
||||
TimeRange outer = create(fromOuter, toOuter);
|
||||
TimeRange inner = create(fromInner, toInner);
|
||||
|
||||
Assert.assertTrue(inner.liesWithin(outer));
|
||||
Assert.assertTrue(outer.includes(inner));
|
||||
Assert.assertFalse(inner.includes(outer));
|
||||
Assert.assertFalse(outer.liesWithin(inner));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void liesWithin() {
|
||||
liesWithin(0, 100, 10, 90);
|
||||
liesWithin(0, 100, 0, 90);
|
||||
liesWithin(0, 100, 10, 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includesTimePoint() {
|
||||
TimeRange range = create(0, 100);
|
||||
Assert.assertTrue(range.includes(create(0)));
|
||||
Assert.assertTrue(range.includes(create(1)));
|
||||
Assert.assertTrue(range.includes(create(100)));
|
||||
Assert.assertFalse(range.includes(create(101)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intersects() {
|
||||
TimeRange one = create(0, 50);
|
||||
TimeRange two = create(20, 70);
|
||||
|
||||
Assert.assertFalse(one.liesWithin(two));
|
||||
Assert.assertFalse(two.liesWithin(one));
|
||||
Assert.assertTrue(one.intersects(two));
|
||||
Assert.assertTrue(two.intersects(one));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user