TimestampRanges { public static PartitionedTimestamps getPartitionedTimestamps(TimestampRange range, int residue, int modulus) { checkModulusAndResidue(residue, modulus); long startTimestamp = getLowestTimestampMatchingModulus(range.getLowerBound(), residue, modulus); long endTimestamp = range.getUpperBound() % modulus == residue ? range.getUpperBound() : getLowestTimestampMatchingModulus(range.getUpperBound(), residue, modulus) - modulus; if (startTimestamp > endTimestamp) { return ImmutablePartitionedTimestamps.builder() .start(range.getLowerBound()) .interval(modulus) .count(0) .build(); } int count = (int) ((endTimestamp - startTimestamp) / modulus) + 1; return ImmutablePartitionedTimestamps.builder() .start(startTimestamp) .interval(modulus) .count(count) .build(); } private  TimestampRanges(); static PartitionedTimestamps getPartitionedTimestamps(TimestampRange range, int residue, int modulus);  }
@Test public void canGetTimestampFromRangeIfItIsTheLowerBound() { assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 3, 10)) .containsExactly(SEVENTY_THREE); }
@Test public void canGetTimestampFromRangeIfItIsTheUpperBound() { assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 2, 10)) .containsExactly(EIGHTY_TWO); }
@Test public void canGetTimestampsFromRangeInTheMiddle() { assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 7, 10)) .containsExactly(77L); assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 8, 10)) .containsExactly(78L); }
@Test public void canHandleMultipleValidMatches() { assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 1, 2)) .containsExactly(73L, 75L, 77L, 79L, 81L); assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 0, 2)) .containsExactly(74L, 76L, 78L, 80L, 82L); }
@Test public void canHandleNegativeResidues() { assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, -7, 10)) .containsExactly(SEVENTY_THREE); assertThat(getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, -5, 10)) .containsExactly(75L); }
@Test public void throwsIfModulusIsNegative() { assertThatThrownBy(() -> getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 3, -8)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Modulus should be positive, but found -8."); assertThatThrownBy(() -> getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 4, -2)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Modulus should be positive, but found -2."); }
@Test public void throwsIfModulusIsZero() { assertThatThrownBy(() -> getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 0, 0)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Modulus should be positive, but found 0."); }
@Test public void throwsIfResidueEqualsOrExceedsModulus() { assertThatThrownBy(() -> getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, 2, 2)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Absolute value of residue 2 equals or exceeds modulus 2 - no solutions"); assertThatThrownBy(() -> getPartitionedTimestamps(SEVENTY_THREE_TO_EIGHTY_TWO, -3, 2)) .isInstanceOf(IllegalArgumentException.class) .hasMessageMatching("Absolute value of residue -3 equals or exceeds modulus 2 - no solutions"); }
@Test public void returnsAbsentIfTimestampRangeDoesNotContainAnyValuesMatchingModulus() { TimestampRange oneTimestamp = TimestampRange.createInclusiveRange(77, 77); assertThat(getPartitionedTimestamps(oneTimestamp, 6, 10)).isEmpty(); }