-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
585 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/dmfs/rfc5545/instanceiterator/PeekableInstanceIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.dmfs.rfc5545.instanceiterator; | ||
|
||
import org.dmfs.rfc5545.DateTime; | ||
import org.dmfs.rfc5545.InstanceIterator; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public final class PeekableInstanceIterator implements InstanceIterator | ||
{ | ||
private final InstanceIterator mDelegate; | ||
private DateTime mNext; | ||
private boolean mHasNext; | ||
|
||
public PeekableInstanceIterator(InstanceIterator delegate) | ||
{ | ||
mDelegate = delegate; | ||
pullNext(); | ||
} | ||
|
||
@Override | ||
public void fastForward(DateTime until) | ||
{ | ||
if (mHasNext && mNext.before(until)) | ||
{ | ||
mDelegate.fastForward(until); | ||
pullNext(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean hasNext() | ||
{ | ||
return mHasNext; | ||
} | ||
|
||
@Override | ||
public DateTime next() | ||
{ | ||
if (!mHasNext) | ||
{ | ||
throw new NoSuchElementException("no further elements to return"); | ||
} | ||
DateTime result = mNext; | ||
pullNext(); | ||
return result; | ||
} | ||
|
||
public DateTime peek() | ||
{ | ||
if (!mHasNext) | ||
{ | ||
throw new NoSuchElementException("no further elements to peek at"); | ||
} | ||
return mNext; | ||
} | ||
|
||
private void pullNext() | ||
{ | ||
mHasNext = mDelegate.hasNext(); | ||
if (mHasNext) | ||
{ | ||
mNext = mDelegate.next(); | ||
} | ||
|
||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/org/dmfs/rfc5545/recurrenceset/Preceding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.dmfs.rfc5545.recurrenceset; | ||
|
||
import org.dmfs.rfc5545.DateTime; | ||
import org.dmfs.rfc5545.InstanceIterator; | ||
import org.dmfs.rfc5545.RecurrenceSet; | ||
import org.dmfs.rfc5545.instanceiterator.PeekableInstanceIterator; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* A {@link RecurrenceSet} of all elements of another {@link RecurrenceSet} that precede a | ||
* given {@link DateTime}. | ||
* A {@link Preceding} {@link RecurrenceSet} is always finite. | ||
* | ||
* <h2>Example</h2> | ||
* <pre>{@code | ||
* // a RecurrenceSet that only contains past occurrences. | ||
* new Preceding(DateTime.now(), recurrenceSet()); | ||
* }</pre> | ||
*/ | ||
public final class Preceding implements RecurrenceSet | ||
{ | ||
private final DateTime mBoundary; | ||
private final RecurrenceSet mDelegate; | ||
|
||
public Preceding(DateTime boundary, RecurrenceSet delegate) | ||
{ | ||
mBoundary = boundary; | ||
mDelegate = delegate; | ||
} | ||
|
||
@Override | ||
public InstanceIterator iterator() | ||
{ | ||
PeekableInstanceIterator delegate = new PeekableInstanceIterator(mDelegate.iterator()); | ||
return new InstanceIterator() | ||
{ | ||
@Override | ||
public void fastForward(DateTime until) | ||
{ | ||
delegate.fastForward(until); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() | ||
{ | ||
return delegate.hasNext() && delegate.peek().before(mBoundary); | ||
} | ||
|
||
@Override | ||
public DateTime next() | ||
{ | ||
DateTime result = delegate.next(); | ||
if (!result.before(mBoundary)) | ||
{ | ||
throw new NoSuchElementException("No more elements"); | ||
} | ||
return result; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean isInfinite() | ||
{ | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isFinite() | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.dmfs.rfc5545.recurrenceset; | ||
|
||
import org.dmfs.rfc5545.DateTime; | ||
import org.dmfs.rfc5545.RecurrenceSet; | ||
import org.dmfs.rfc5545.RecurrenceSetComposition; | ||
|
||
/** | ||
* {@link RecurrenceSet} of the elements of another {@link RecurrenceSet} that fall | ||
* in the given right-open interval of time. | ||
* A {@link Within} {@link RecurrenceSet} is always finite. | ||
* | ||
* <h2>Example</h2> | ||
* <pre>{@code | ||
* // every occurrence in 2024 (UTC) | ||
* new Within( | ||
* DateTime.parse("20240101T000000Z"), | ||
* DateTime.parse("20250101T000000Z"), | ||
* recurrenceSet()); | ||
* }</pre> | ||
*/ | ||
public final class Within extends RecurrenceSetComposition | ||
{ | ||
public Within(DateTime fromIncluding, DateTime toExcluding, RecurrenceSet delegate) | ||
{ | ||
super(new Preceding(toExcluding, new FastForwarded(fromIncluding, delegate))); | ||
} | ||
} |
Oops, something went wrong.