Skip to content

Commit

Permalink
Add RecurrenceSet::isFinite method to deprecate isInfinite, closes #134
Browse files Browse the repository at this point in the history
… (#135)
  • Loading branch information
dmfs authored Apr 6, 2024
1 parent 05920e7 commit 8ee6f28
Show file tree
Hide file tree
Showing 22 changed files with 184 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ dependencies {
api libs.jems2
testImplementation project("lib-recur-hamcrest")
testImplementation project("lib-recur-confidence")
testImplementation 'org.dmfs:jems2-testing:2.22.0'
testImplementation libs.jems2.testing
testImplementation libs.jems2.confidence
testImplementation 'org.saynotobugs:confidence-core:0.42.0'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
eclipse-jdt = "2.2.600"
hamcrest = "2.2"
jems2 = "2.22.0"
jems2 = "2.23.1"
junit = "5.8.2"
junit-testkit = "1.9.2"
srcless = "0.3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public EmptyRecurrenceSet()
{
super(new AllOf<>(
new Is<>(new EmptyIterable()),
new Is<>(new Finite()),
new Is<>(new Not<>(new Infinite()))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 Marten Gajda <[email protected]>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dmfs.rfc5545.confidence.quality;

import org.dmfs.rfc5545.RecurrenceSet;
import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.AllOf;
import org.saynotobugs.confidence.quality.composite.Not;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;

@StaticFactories(value = "Recur", packageName = "org.dmfs.rfc5545.confidence")
public final class Finite extends QualityComposition<RecurrenceSet>
{
public Finite()
{
super(new AllOf<>(
new Not<>(new Satisfies<>(RecurrenceSet::isInfinite, new Text("infinite"))),
new Satisfies<>(RecurrenceSet::isFinite, new Text("finite"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.dmfs.rfc5545.RecurrenceSet;
import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.AllOf;
import org.saynotobugs.confidence.quality.composite.Not;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;

Expand All @@ -28,6 +30,8 @@ public final class Infinite extends QualityComposition<RecurrenceSet>
{
public Infinite()
{
super(new Satisfies<>(RecurrenceSet::isInfinite, new Text("infinite")));
super(new AllOf<>(
new Not<>(new Satisfies<>(RecurrenceSet::isFinite, new Text("finite"))),
new Satisfies<>(RecurrenceSet::isInfinite, new Text("infinite"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,25 @@ void test()
allOf(
org.saynotobugs.confidence.test.quality.Test.<RecurrenceSet>passes(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(false)),
with(RecurrenceSet::isFinite, returning(true)),
with(RecurrenceSet::iterator, returning(new EmptyIterator())))),

fails(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(true)),
with(RecurrenceSet::isFinite, returning(false)),
with(RecurrenceSet::iterator, returning(new EmptyIterator())))),

fails(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(false)),
with(RecurrenceSet::isFinite, returning(true)),
with(RecurrenceSet::iterator, returning(
new FastForwardable(
DateTime.parse("20240101"),
new Seq<>(DateTime.parse("20240102"), DateTime.parse("20240103"))))))),

fails(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(true)),
with(RecurrenceSet::isFinite, returning(false)),
with(RecurrenceSet::iterator, returning(
new FastForwardable(
DateTime.parse("20240101"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2024 Marten Gajda <[email protected]>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dmfs.rfc5545.confidence.quality;

import org.dmfs.rfc5545.InstanceIterator;
import org.dmfs.rfc5545.RecurrenceSet;
import org.junit.jupiter.api.Test;

import static org.dmfs.jems2.mockito.Mock.*;
import static org.saynotobugs.confidence.Assertion.assertThat;
import static org.saynotobugs.confidence.quality.Core.allOf;
import static org.saynotobugs.confidence.test.quality.Test.fails;

class FiniteTest
{
@Test
void test()
{
assertThat(new Finite(),
allOf(
org.saynotobugs.confidence.test.quality.Test.<RecurrenceSet>passes(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(false)),
with(RecurrenceSet::isFinite, returning(true)),
with(RecurrenceSet::iterator, returning(mock(InstanceIterator.class))))),

fails(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(true)),
with(RecurrenceSet::isFinite, returning(false)),
with(RecurrenceSet::iterator, returning(mock(InstanceIterator.class)))))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ void test()
allOf(
org.saynotobugs.confidence.test.quality.Test.<RecurrenceSet>passes(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(true)),
with(RecurrenceSet::isFinite, returning(false)),
with(RecurrenceSet::iterator, returning(mock(InstanceIterator.class))))),

fails(mock(RecurrenceSet.class,
with(RecurrenceSet::isInfinite, returning(false)),
with(RecurrenceSet::isFinite, returning(true)),
with(RecurrenceSet::iterator, returning(mock(InstanceIterator.class)))))));
}

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/dmfs/rfc5545/RecurrenceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package org.dmfs.rfc5545;


import org.dmfs.jems2.Optional;

/**
* A set of instances.
*/
Expand All @@ -32,6 +30,17 @@ public interface RecurrenceSet extends Iterable<DateTime>

/**
* Returns whether this {@link RecurrenceSet} is infinite or not.
*
* @deprecated in favour of {@link #isFinite()}
*/
@Deprecated(forRemoval = true)
boolean isInfinite();

/**
* Returns whether this {@link RecurrenceSet} is finite.
*/
default boolean isFinite()
{
return !isInfinite();
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/dmfs/rfc5545/optional/LastInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.dmfs.jems2.Optional;
import org.dmfs.jems2.optional.DelegatingOptional;
import org.dmfs.jems2.optional.Restrained;
import org.dmfs.jems2.optional.If;
import org.dmfs.rfc5545.DateTime;
import org.dmfs.rfc5545.RecurrenceSet;

Expand All @@ -31,6 +31,6 @@ public final class LastInstance extends DelegatingOptional<DateTime>
{
public LastInstance(RecurrenceSet recurrenceSet)
{
super(new Restrained<>(() -> !recurrenceSet.isInfinite(), new Last<>(recurrenceSet)));
super(new If<>(recurrenceSet::isFinite, new Last<>(recurrenceSet)));
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/dmfs/rfc5545/recurrenceset/Difference.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ public boolean isInfinite()
{
return mMinuend.isInfinite();
}

@Override
public boolean isFinite()
{
return mMinuend.isFinite();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ public boolean isInfinite()
{
return mDelegate.isInfinite();
}

@Override
public boolean isFinite()
{
return mDelegate.isFinite();
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/dmfs/rfc5545/recurrenceset/Merged.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.dmfs.jems2.iterable.Mapped;
import org.dmfs.jems2.iterable.Seq;
import org.dmfs.jems2.iterable.Sieved;
import org.dmfs.jems2.predicate.Not;
import org.dmfs.rfc5545.InstanceIterator;
import org.dmfs.rfc5545.RecurrenceSet;

Expand Down Expand Up @@ -56,6 +57,11 @@ public boolean isInfinite()
return new Sieved<>(RecurrenceSet::isInfinite, delegates).iterator().hasNext();
}

@Override
public boolean isFinite()
{
return !new Sieved<>(new Not<>(RecurrenceSet::isFinite), delegates).iterator().hasNext();
}
});
}

Expand All @@ -77,4 +83,10 @@ public boolean isInfinite()
{
return mDelegate.isInfinite();
}

@Override
public boolean isFinite()
{
return mDelegate.isFinite();
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/dmfs/rfc5545/recurrenceset/OfList.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ public boolean isInfinite()
{
return false;
}

@Override
public boolean isFinite()
{
return true;
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/dmfs/rfc5545/recurrenceset/OfRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ public boolean isInfinite()
{
return mRecurrenceRule.isInfinite();
}

@Override
public boolean isFinite()
{
return !mRecurrenceRule.isInfinite();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ public boolean isInfinite()
{
return mRecurrenceRule.isInfinite();
}

@Override
public boolean isFinite()
{
return !mRecurrenceRule.isInfinite();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void testInstancesAndExceptions() throws InvalidRecurrenceRuleException
new OfRule(new RecurrenceRule("FREQ=HOURLY;INTERVAL=12;COUNT=10"), DateTime.parse("20240224T120000")),
new OfRule(new RecurrenceRule("FREQ=DAILY;COUNT=5"), DateTime.parse("20240224T000000"))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20240224T120000"),
DateTime.parse("20240225T120000"),
Expand All @@ -87,7 +87,7 @@ void test_github_issue_93() throws InvalidRecurrenceRuleException
new OfRule(new RecurrenceRule("FREQ=WEEKLY;UNTIL=20200511T000000Z;BYDAY=TU"), DateTime.parse("20200414T160000Z")),
new OfList(DateTime.parse("20200421T160000Z"), DateTime.parse("20200505T160000Z"))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20200414T160000Z"),
DateTime.parse("20200428T160000Z"))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void testFastForwardRule() throws InvalidRecurrenceRuleException
new FastForwarded(DateTime.parse("20240218"),
new OfRule(new RecurrenceRule("FREQ=DAILY;COUNT=5"), DateTime.parse("20240215"))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20240218"),
DateTime.parse("20240219"))));
Expand All @@ -69,7 +69,7 @@ void testFastForwardRuleWithUnsyncedStart() throws InvalidRecurrenceRuleExceptio
new FastForwarded(DateTime.parse("20240218"),
new OfRuleAndFirst(new RecurrenceRule("FREQ=DAILY;BYDAY=FR;COUNT=3"), DateTime.parse("20240215"))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20240223"))));
}
Expand All @@ -82,7 +82,7 @@ void testFastForwardRuleWithUnsyncedStartMultipleInstances() throws InvalidRecur
new FastForwarded(DateTime.parse("20240218"),
new OfRuleAndFirst(new RecurrenceRule("FREQ=DAILY;BYDAY=FR;COUNT=5"), DateTime.parse("20240207"))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20240223"),
DateTime.parse("20240301"))));
Expand All @@ -99,7 +99,7 @@ void testFastForwardList()
DateTime.parse("20240218"),
DateTime.parse("20240219"))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20240218"),
DateTime.parse("20240219"))));
Expand All @@ -118,7 +118,7 @@ void testFastForwardMultiple() throws InvalidRecurrenceRuleException
DateTime.parse("20240220"),
DateTime.parse("20240221"))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20240218"),
DateTime.parse("20240219"),
Expand All @@ -138,7 +138,7 @@ void testFastForwardWithExceptions() throws InvalidRecurrenceRuleException
DateTime.parse("20240217"),
DateTime.parse("20240220")))),
allOf(
is(not(infinite())),
is(finite()),
iterates(
DateTime.parse("20240218"),
DateTime.parse("20240219"),
Expand Down
Loading

0 comments on commit 8ee6f28

Please sign in to comment.