bridged the gap between device mapping events where "to" is inclusive, and TimeRange, where "to" is exclusive; TimePoint.plus|minus no longer wraps around but bumps

Change-Id: I97f30deb38d83872879c33971616bfcf18a5469a
This commit is contained in:
Axel Uhl committed 2016-12-23 23:10:05 +01:00
1 parent da9e5d20b3
commit d4479a3bc1
17 files changed
+121 -46

No files matched your search

@@ -16,12 +16,32 @@ public interface TimePoint extends Comparable<TimePoint>, Serializable {
Date asDate();
/**
* @return a time point that is {@code milliseconds} later than {@code this} time point, except in case the
* resulting time point would be after the {@link #EndOfTime} in which case {@link #EndOfTime} is returned
* instead of provoking an overflow with wrap-around.
*/
TimePoint plus(long milliseconds);
/**
* @return a time point that is {@code duration} later than {@code this} time point, except in case the
* resulting time point would be after the {@link #EndOfTime} in which case {@link #EndOfTime} is returned
* instead of provoking an overflow with wrap-around.
*/
TimePoint plus(Duration duration);
/**
* @return a time point that is {@code milliseconds} earlier than {@code this} time point, except in case the
* resulting time point would be before the {@link #BeginningOfTime} in which case {@link #BeginningOfTime} is returned
* instead of provoking an underflow with wrap-around.
*/
TimePoint minus(long milliseconds);
/**
* @return a time point that is {@code duration} earlier than {@code this} time point, except in case the
* resulting time point would be before the {@link #BeginningOfTime} in which case {@link #BeginningOfTime} is returned
* instead of provoking an underflow with wrap-around.
*/
TimePoint minus(Duration duration);
/**
@@ -64,7 +64,19 @@ public abstract class AbstractTimePoint implements TimePoint {
@Override
public TimePoint plus(long milliseconds) {
return new MillisecondsTimePoint(asMillis()+milliseconds);
final TimePoint result;
if (milliseconds > 0) {
if (EndOfTime.asMillis()-milliseconds >= asMillis()) {
result = new MillisecondsTimePoint(asMillis()+milliseconds);
} else {
result = EndOfTime;
}
} else if (asMillis() < milliseconds) {
result = BeginningOfTime;
} else {
result = new MillisecondsTimePoint(asMillis()+milliseconds);
}
return result;
}
@Override
@@ -21,13 +21,13 @@ public class TimeRangeImpl extends Util.Pair<TimePoint, TimePoint> implements Ti
* if {@code null}, the time range is considered open on its "left" end, and all {@link TimePoint}s at or
* after {@link TimePoint#BeginningOfTime} and at or before {@code to} are considered {@link #includes
* included} in this time range.
* @param to
* @param toExclusive
* if {@code null}, the time range is considered open on its "right" end, and all {@link TimePoint}s at
* or before {@link TimePoint#EndOfTime} and at or after {@code from} are considered {@link #includes
* included} in this time range.
*/
public TimeRangeImpl(TimePoint from, TimePoint to) {
super(from == null ? TimePoint.BeginningOfTime : from, to == null ? TimePoint.EndOfTime : to);
public TimeRangeImpl(TimePoint from, TimePoint toExclusive) {
super(from == null ? TimePoint.BeginningOfTime : from, toExclusive == null ? TimePoint.EndOfTime : toExclusive);
if (from().after(to())) {
throw new IllegalArgumentException("from " + from() + " must lie before to " + to() + " in a TimeRange");
}