Skip to content

Commit 793a9d4

Browse files
authored
Fix "fast forward" to first instance, fixes #85 (#86)
The fastForward method always pulled the next instance, regardless of whether the upcoming instance was already before the fast forward date or not.
1 parent 2741f55 commit 793a9d4

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

src/main/java/org/dmfs/rfc5545/recurrenceset/RecurrenceSetIterator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ public long next()
119119
*/
120120
public void fastForward(long until)
121121
{
122-
mInstances.fastForward(until);
123-
mExceptions.fastForward(until);
124-
pullNext();
122+
if (mNextInstance < until)
123+
{
124+
mInstances.fastForward(until);
125+
mExceptions.fastForward(until);
126+
pullNext();
127+
}
125128
}
126129

127130

src/test/java/org/dmfs/rfc5545/recurrenceset/RecurrenceSetIteratorTest.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Test;
2525

2626
import java.util.TimeZone;
27+
import java.util.concurrent.TimeUnit;
2728

2829
import static java.util.Arrays.asList;
2930
import static org.dmfs.jems.hamcrest.matchers.GeneratableMatcher.startsWith;
@@ -276,4 +277,82 @@ public void testMultipleRulesWithFastForward() throws InvalidRecurrenceRuleExcep
276277
new DateTime(DateTime.UTC, 2019, 1, 3, 2, 0, 0).getTimestamp()
277278
));
278279
}
280+
281+
282+
/**
283+
* See https://github.com/dmfs/lib-recur/issues/85
284+
* <p>
285+
* Fast forward to the start date (i.e. not fast forwarding at all)
286+
*/
287+
@Test
288+
public void testFastForwardToStart() throws InvalidRecurrenceRuleException
289+
{
290+
DateTime start = new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0);
291+
292+
RecurrenceSet ruleSet = new RecurrenceSet();
293+
ruleSet.addInstances(new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=DAILY;INTERVAL=1")));
294+
295+
// Create an iterator using the RecurrenceSet
296+
RecurrenceSetIterator it = ruleSet.iterator(start.getTimeZone(), start.getTimestamp());
297+
298+
// "Fast forward" to start.
299+
it.fastForward(start.getTimestamp());
300+
301+
assertThat(() -> it::next, startsWith(
302+
new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0).getTimestamp(),
303+
new DateTime(DateTime.UTC, 2019, 1, 2, 0, 0, 0).getTimestamp(),
304+
new DateTime(DateTime.UTC, 2019, 1, 3, 0, 0, 0).getTimestamp(),
305+
new DateTime(DateTime.UTC, 2019, 1, 4, 0, 0, 0).getTimestamp(),
306+
new DateTime(DateTime.UTC, 2019, 1, 5, 0, 0, 0).getTimestamp()
307+
));
308+
}
309+
310+
311+
@Test
312+
public void testFastForwardToPast() throws InvalidRecurrenceRuleException
313+
{
314+
DateTime start = new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0);
315+
316+
RecurrenceSet ruleSet = new RecurrenceSet();
317+
ruleSet.addInstances(new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=DAILY;INTERVAL=1")));
318+
319+
// Create an iterator using the RecurrenceSet
320+
RecurrenceSetIterator it = ruleSet.iterator(start.getTimeZone(), start.getTimestamp());
321+
322+
// "Fast forward" to 100 days in the past.
323+
it.fastForward(start.getTimestamp() - TimeUnit.DAYS.toMillis(100));
324+
325+
assertThat(() -> it::next, startsWith(
326+
new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0).getTimestamp(),
327+
new DateTime(DateTime.UTC, 2019, 1, 2, 0, 0, 0).getTimestamp(),
328+
new DateTime(DateTime.UTC, 2019, 1, 3, 0, 0, 0).getTimestamp(),
329+
new DateTime(DateTime.UTC, 2019, 1, 4, 0, 0, 0).getTimestamp(),
330+
new DateTime(DateTime.UTC, 2019, 1, 5, 0, 0, 0).getTimestamp()
331+
));
332+
}
333+
334+
335+
@Test
336+
public void testFastForwardToNext() throws InvalidRecurrenceRuleException
337+
{
338+
DateTime start = new DateTime(DateTime.UTC, 2019, 1, 1, 0, 0, 0);
339+
340+
RecurrenceSet ruleSet = new RecurrenceSet();
341+
ruleSet.addInstances(new RecurrenceRuleAdapter(new RecurrenceRule("FREQ=DAILY;INTERVAL=1")));
342+
343+
// Create an iterator using the RecurrenceSet
344+
RecurrenceSetIterator it = ruleSet.iterator(start.getTimeZone(), start.getTimestamp());
345+
346+
// "Fast forward" to 1 millisecond after start (skipping the first instance only)
347+
it.fastForward(start.getTimestamp() + 1);
348+
349+
assertThat(() -> it::next, startsWith(
350+
new DateTime(DateTime.UTC, 2019, 1, 2, 0, 0, 0).getTimestamp(),
351+
new DateTime(DateTime.UTC, 2019, 1, 3, 0, 0, 0).getTimestamp(),
352+
new DateTime(DateTime.UTC, 2019, 1, 4, 0, 0, 0).getTimestamp(),
353+
new DateTime(DateTime.UTC, 2019, 1, 5, 0, 0, 0).getTimestamp(),
354+
new DateTime(DateTime.UTC, 2019, 1, 6, 0, 0, 0).getTimestamp()
355+
));
356+
}
357+
279358
}

0 commit comments

Comments
 (0)