mirror of
https://github.com/google/nomulus.git
synced 2025-05-15 08:57:12 +02:00
Change TimeOfDay getInstancesInRange to use Range
This will make the utility a lot more versatile (allow both open and closed intervals) and hand the range validation and comparison off to the Range class. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=122867110
This commit is contained in:
parent
a2d2764115
commit
91cf36c5b9
2 changed files with 94 additions and 29 deletions
|
@ -14,12 +14,15 @@
|
||||||
|
|
||||||
package google.registry.model.common;
|
package google.registry.model.common;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static google.registry.util.DateTimeUtils.END_OF_TIME;
|
||||||
|
import static google.registry.util.DateTimeUtils.START_OF_TIME;
|
||||||
import static google.registry.util.DateTimeUtils.isAtOrAfter;
|
import static google.registry.util.DateTimeUtils.isAtOrAfter;
|
||||||
import static google.registry.util.DateTimeUtils.isBeforeOrAt;
|
import static google.registry.util.DateTimeUtils.isBeforeOrAt;
|
||||||
|
|
||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.common.collect.BoundType;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.common.collect.Range;
|
||||||
|
|
||||||
import com.googlecode.objectify.annotation.Embed;
|
import com.googlecode.objectify.annotation.Embed;
|
||||||
import com.googlecode.objectify.annotation.Index;
|
import com.googlecode.objectify.annotation.Index;
|
||||||
|
@ -70,16 +73,26 @@ public class TimeOfYear extends ImmutableObject {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an {@link ImmutableSet} of {@link DateTime}s of every recurrence of this particular
|
* Returns an {@link ImmutableSet} of {@link DateTime}s of every recurrence of this particular
|
||||||
* time of year within a given range (usually a range spanning many years).
|
* time of year within a given {@link Range} (usually one spanning many years).
|
||||||
*/
|
*/
|
||||||
public ImmutableSet<DateTime> getInstancesInRange(DateTime lower, DateTime upper) {
|
public ImmutableSet<DateTime> getInstancesInRange(Range<DateTime> range) {
|
||||||
checkArgument(isBeforeOrAt(lower, upper), "Lower bound is not before or at upper bound.");
|
// In registry world, all dates are within START_OF_TIME and END_OF_TIME, so restrict any
|
||||||
|
// ranges without bounds to our notion of zero-to-infinity.
|
||||||
|
Range<DateTime> normalizedRange = Range.range(
|
||||||
|
range.hasLowerBound() ? range.lowerEndpoint() : START_OF_TIME,
|
||||||
|
range.hasLowerBound() ? range.lowerBoundType() : BoundType.CLOSED,
|
||||||
|
range.hasUpperBound() ? range.upperEndpoint() : END_OF_TIME,
|
||||||
|
range.hasUpperBound() ? range.upperBoundType() : BoundType.CLOSED);
|
||||||
ImmutableSet.Builder<DateTime> instances = ImmutableSet.builder();
|
ImmutableSet.Builder<DateTime> instances = ImmutableSet.builder();
|
||||||
DateTime firstInstance = getNextInstanceAtOrAfter(lower);
|
// This produces a greedy year range, but the edge cases will be handled appropriately via
|
||||||
for (int year = firstInstance.getYear();
|
// Range.contains().
|
||||||
year <= getLastInstanceBeforeOrAt(upper).getYear();
|
for (int year = normalizedRange.lowerEndpoint().getYear();
|
||||||
|
year <= normalizedRange.upperEndpoint().getYear();
|
||||||
year++) {
|
year++) {
|
||||||
instances.add(firstInstance.withYear(year));
|
DateTime candidate = getDateTimeWithSameYear(normalizedRange.lowerEndpoint()).withYear(year);
|
||||||
|
if (normalizedRange.contains(candidate)) {
|
||||||
|
instances.add(candidate);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return instances.build();
|
return instances.build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,11 @@
|
||||||
package google.registry.model.common;
|
package google.registry.model.common;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static google.registry.util.DateTimeUtils.END_OF_TIME;
|
||||||
|
import static google.registry.util.DateTimeUtils.START_OF_TIME;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.common.collect.Range;
|
||||||
|
|
||||||
import google.registry.testing.ExceptionRule;
|
import google.registry.testing.ExceptionRule;
|
||||||
|
|
||||||
|
@ -63,34 +66,43 @@ public class TimeOfYearTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuccess_getInstancesOfTimeOfYearInRange() {
|
public void testSuccess_getInstancesInRange_closed() {
|
||||||
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
||||||
DateTime endDate = DateTime.parse("2016-02-01T00:00:00Z");
|
DateTime endDate = DateTime.parse("2016-05-01T00:00:00Z");
|
||||||
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-10-01T00:00:00Z"));
|
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-05-01T00:00:00Z"));
|
||||||
ImmutableSet<DateTime> actual = timeOfYear.getInstancesInRange(startDate, endDate);
|
ImmutableSet<DateTime> actual =
|
||||||
|
timeOfYear.getInstancesInRange(Range.closed(startDate, endDate));
|
||||||
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
||||||
DateTime.parse("2012-10-01T00:00:00Z"),
|
DateTime.parse("2012-05-01T00:00:00Z"),
|
||||||
DateTime.parse("2013-10-01T00:00:00Z"),
|
DateTime.parse("2013-05-01T00:00:00Z"),
|
||||||
DateTime.parse("2014-10-01T00:00:00Z"),
|
DateTime.parse("2014-05-01T00:00:00Z"),
|
||||||
DateTime.parse("2015-10-01T00:00:00Z"));
|
DateTime.parse("2015-05-01T00:00:00Z"),
|
||||||
|
DateTime.parse("2016-05-01T00:00:00Z"));
|
||||||
assertThat(actual).containsExactlyElementsIn(expected);
|
assertThat(actual).containsExactlyElementsIn(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuccess_getInstancesOfTimeOfYearInRange_empty() {
|
public void testSuccess_getInstancesInRange_openClosed() {
|
||||||
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
||||||
DateTime endDate = DateTime.parse("2013-02-01T00:00:00Z");
|
DateTime endDate = DateTime.parse("2016-05-01T00:00:00Z");
|
||||||
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-03-01T00:00:00Z"));
|
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-05-01T00:00:00Z"));
|
||||||
ImmutableSet<DateTime> actual = timeOfYear.getInstancesInRange(startDate, endDate);
|
ImmutableSet<DateTime> actual =
|
||||||
assertThat(actual).isEmpty();
|
timeOfYear.getInstancesInRange(Range.openClosed(startDate, endDate));
|
||||||
|
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
||||||
|
DateTime.parse("2013-05-01T00:00:00Z"),
|
||||||
|
DateTime.parse("2014-05-01T00:00:00Z"),
|
||||||
|
DateTime.parse("2015-05-01T00:00:00Z"),
|
||||||
|
DateTime.parse("2016-05-01T00:00:00Z"));
|
||||||
|
assertThat(actual).containsExactlyElementsIn(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSuccess_getInstancesOfTimeOfYearInRange_inclusive() {
|
public void testSuccess_getInstancesInRange_closedOpen() {
|
||||||
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
||||||
DateTime endDate = DateTime.parse("2015-05-01T00:00:00Z");
|
DateTime endDate = DateTime.parse("2016-05-01T00:00:00Z");
|
||||||
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-05-01T00:00:00Z"));
|
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-05-01T00:00:00Z"));
|
||||||
ImmutableSet<DateTime> actual = timeOfYear.getInstancesInRange(startDate, endDate);
|
ImmutableSet<DateTime> actual =
|
||||||
|
timeOfYear.getInstancesInRange(Range.closedOpen(startDate, endDate));
|
||||||
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
||||||
DateTime.parse("2012-05-01T00:00:00Z"),
|
DateTime.parse("2012-05-01T00:00:00Z"),
|
||||||
DateTime.parse("2013-05-01T00:00:00Z"),
|
DateTime.parse("2013-05-01T00:00:00Z"),
|
||||||
|
@ -100,10 +112,50 @@ public class TimeOfYearTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFailure_getInstancesOfTimeOfYearInRange_inverted() {
|
public void testSuccess_getInstancesInRange_open() {
|
||||||
thrown.expect(IllegalArgumentException.class, "Lower bound is not before or at upper bound.");
|
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
||||||
TimeOfYear.fromDateTime(DateTime.parse("2013-10-01T00:00:00Z")).getInstancesInRange(
|
DateTime endDate = DateTime.parse("2016-05-01T00:00:00Z");
|
||||||
DateTime.parse("2015-10-01T00:00:00Z"),
|
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-05-01T00:00:00Z"));
|
||||||
DateTime.parse("2012-10-01T00:00:00Z"));
|
ImmutableSet<DateTime> actual =
|
||||||
|
timeOfYear.getInstancesInRange(Range.open(startDate, endDate));
|
||||||
|
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
||||||
|
DateTime.parse("2013-05-01T00:00:00Z"),
|
||||||
|
DateTime.parse("2014-05-01T00:00:00Z"),
|
||||||
|
DateTime.parse("2015-05-01T00:00:00Z"));
|
||||||
|
assertThat(actual).containsExactlyElementsIn(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSuccess_getInstancesInRange_normalizedLowerBound() {
|
||||||
|
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(START_OF_TIME);
|
||||||
|
ImmutableSet<DateTime> actual =
|
||||||
|
timeOfYear.getInstancesInRange(Range.atMost(START_OF_TIME.plusYears(2)));
|
||||||
|
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
||||||
|
START_OF_TIME,
|
||||||
|
START_OF_TIME.plusYears(1),
|
||||||
|
START_OF_TIME.plusYears(2));
|
||||||
|
assertThat(actual).containsExactlyElementsIn(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSuccess_getInstancesInRange_normalizedUpperBound() {
|
||||||
|
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(END_OF_TIME);
|
||||||
|
ImmutableSet<DateTime> actual =
|
||||||
|
timeOfYear.getInstancesInRange(Range.atLeast(END_OF_TIME.minusYears(2)));
|
||||||
|
ImmutableSet<DateTime> expected = ImmutableSet.<DateTime>of(
|
||||||
|
END_OF_TIME.minusYears(2),
|
||||||
|
END_OF_TIME.minusYears(1),
|
||||||
|
END_OF_TIME);
|
||||||
|
assertThat(actual).containsExactlyElementsIn(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSuccess_getInstancesOfTimeOfYearInRange_empty() {
|
||||||
|
DateTime startDate = DateTime.parse("2012-05-01T00:00:00Z");
|
||||||
|
DateTime endDate = DateTime.parse("2013-02-01T00:00:00Z");
|
||||||
|
TimeOfYear timeOfYear = TimeOfYear.fromDateTime(DateTime.parse("2012-03-01T00:00:00Z"));
|
||||||
|
ImmutableSet<DateTime> actual =
|
||||||
|
timeOfYear.getInstancesInRange(Range.closed(startDate, endDate));
|
||||||
|
assertThat(actual).isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue