From 769f30120a92d2c09fec86103957ac0164d352ea Mon Sep 17 00:00:00 2001 From: Ben Fortuna Date: Mon, 6 Jul 2020 12:14:42 +1000 Subject: [PATCH] Improved period logic --- .../java/net/fortuna/ical4j/model/Period.java | 34 ++- .../net/fortuna/ical4j/model/PeriodList.java | 15 +- .../ical4j/model/TemporalAmountAdapter.java | 10 +- .../ical4j/model/PeriodListSpec.groovy | 99 +++++++ .../fortuna/ical4j/model/PeriodSpec.groovy | 142 ++++++++- .../fortuna/ical4j/model/PeriodListTest.java | 270 ++++-------------- .../net/fortuna/ical4j/model/PeriodTest.java | 97 +------ 7 files changed, 342 insertions(+), 325 deletions(-) diff --git a/src/main/java/net/fortuna/ical4j/model/Period.java b/src/main/java/net/fortuna/ical4j/model/Period.java index adb295241..a8fbddd22 100644 --- a/src/main/java/net/fortuna/ical4j/model/Period.java +++ b/src/main/java/net/fortuna/ical4j/model/Period.java @@ -259,7 +259,7 @@ private static TemporalAmountAdapter parseDuration(String value) { */ public final TemporalAmount getDuration() { if (duration == null) { - return TemporalAmountAdapter.from(getStart(), getEnd()).getDuration(); + return TemporalAmountAdapter.from(start, end).getDuration(); } return duration.getDuration(); } @@ -301,7 +301,8 @@ public final boolean includes(final Date date, final boolean inclusive) { */ public final boolean includes(final Temporal date) { Objects.requireNonNull(date, "date"); - return toInterval().encloses(Interval.of(Instant.from(date), Duration.ZERO)); + return start.equals(date) || end.equals(date) + || toInterval().encloses(Interval.of(Instant.from(date), Duration.ZERO)); } /** @@ -348,7 +349,7 @@ public final Period add(final Period period) { * If the specified period is completely contained in this period, the resulting list will contain two periods. * Otherwise it will contain one. * - * If the specified period does not interest this period a list containing this period is returned. + * If the specified period does not intersect this period a list containing this period is returned. * * If this period is completely contained within the specified period an empty period list is returned. * @@ -356,12 +357,23 @@ public final Period add(final Period period) { * @return a list containing zero, one or two periods. */ public final PeriodList subtract(final Period period) { - Interval thisInterval = TemporalAdapter.isFloating(getStart()) ? toInterval(ZoneId.systemDefault()) : toInterval(); - Interval thatInterval = TemporalAdapter.isFloating(getStart()) ? period.toInterval(ZoneId.systemDefault()) : period.toInterval(); + if (period.equals(this)) { + return new PeriodList<>(dateFormat); + } else if (!period.intersects(this) || start instanceof LocalDate) { + return new PeriodList<>(Collections.singletonList(this), dateFormat); + } + return subtractInterval(period); + } + + private PeriodList subtractInterval(Period period) { + Interval thisInterval = TemporalAdapter.isFloating(getStart()) + ? toInterval(ZoneId.systemDefault()) : toInterval(); + Interval thatInterval = TemporalAdapter.isFloating(getStart()) + ? period.toInterval(ZoneId.systemDefault()) : period.toInterval(); if (thatInterval.encloses(thisInterval)) { return new PeriodList<>(period.dateFormat); - } else if (thatInterval.overlaps(thisInterval)) { + } else if (!thatInterval.overlaps(thisInterval)) { return new PeriodList<>(Collections.singletonList(this)); } @@ -369,10 +381,10 @@ public final PeriodList subtract(final Period period) { T newPeriodStart; T newPeriodEnd; - if (thatInterval.getStart().isBefore(thisInterval.getStart())) { + if (!thatInterval.getStart().isAfter(thisInterval.getStart())) { newPeriodStart = period.getEnd(); newPeriodEnd = getEnd(); - } else if (thatInterval.getEnd().isAfter(thisInterval.getEnd())) { + } else if (!thatInterval.getEnd().isBefore(thisInterval.getEnd())) { newPeriodStart = getStart(); newPeriodEnd = period.getStart(); } else { @@ -394,6 +406,9 @@ public final PeriodList subtract(final Period period) { * @return true if this period consumes no time, otherwise false */ public final boolean isEmpty() { + if (start instanceof LocalDate) { + return start.equals(end); + } return toInterval().isEmpty(); } @@ -460,8 +475,7 @@ public Interval toInterval() { public Interval toInterval(ZoneId zoneId) { if (start instanceof LocalDate) { throw new UnsupportedOperationException("Unable to create Interval from date-only temporal."); - } - else if (start instanceof Instant) { + } else if (start instanceof Instant) { return Interval.of((Instant) start, (Instant) end); } else { // calculate zone offset based on current applicable rules diff --git a/src/main/java/net/fortuna/ical4j/model/PeriodList.java b/src/main/java/net/fortuna/ical4j/model/PeriodList.java index 81bfd579c..0a3673d36 100644 --- a/src/main/java/net/fortuna/ical4j/model/PeriodList.java +++ b/src/main/java/net/fortuna/ical4j/model/PeriodList.java @@ -36,6 +36,7 @@ import org.threeten.extra.Interval; import java.io.Serializable; +import java.time.LocalDate; import java.time.ZoneId; import java.time.temporal.Temporal; import java.util.Arrays; @@ -155,6 +156,9 @@ public final PeriodList normalise() { boolean normalised = false; for (Period period1 : periods) { period = period1; + if (period.getStart() instanceof LocalDate) { + continue; + } if (period.isEmpty()) { period = prevPeriod; normalised = true; @@ -210,7 +214,7 @@ public final PeriodList add(final PeriodList periods) { if (periods != null) { final PeriodList newList = new PeriodList<>(dateFormat); newList.getPeriods().addAll(this.periods); - newList.getPeriods().addAll(this.periods); + newList.getPeriods().addAll(periods.periods); return newList.normalise(); } return this; @@ -234,8 +238,13 @@ public final PeriodList subtract(final PeriodList subtractions) { PeriodList tmpResult = new PeriodList<>(dateFormat); for (final Period subtraction : subtractions.getPeriods()) { - for (final Period period : result.getPeriods()) { - tmpResult.addAll(period.subtract(subtraction).getPeriods()); + if (subtraction.getStart() instanceof LocalDate) { + tmpResult.addAll(result.getPeriods().stream() + .filter(p -> !p.equals(subtraction)).collect(Collectors.toList())); + } else { + for (final Period period : result.getPeriods()) { + tmpResult.addAll(period.subtract(subtraction).getPeriods()); + } } result = tmpResult; tmpResult = new PeriodList<>(); diff --git a/src/main/java/net/fortuna/ical4j/model/TemporalAmountAdapter.java b/src/main/java/net/fortuna/ical4j/model/TemporalAmountAdapter.java index 354ea1eef..627bb8ca9 100644 --- a/src/main/java/net/fortuna/ical4j/model/TemporalAmountAdapter.java +++ b/src/main/java/net/fortuna/ical4j/model/TemporalAmountAdapter.java @@ -168,8 +168,14 @@ else if (value.matches("([+-])?P.*(W|D)")) { } public static TemporalAmountAdapter from(Temporal start, Temporal end) { - TemporalAmount duration = Duration.between(start, end); - return new TemporalAmountAdapter(duration); + if (start instanceof LocalDate) { + return from((LocalDate) start, (LocalDate) end); + } + return new TemporalAmountAdapter(Duration.between(start, end)); + } + + public static TemporalAmountAdapter from(LocalDate start, LocalDate end) { + return new TemporalAmountAdapter(Period.between(start, end)); } public static TemporalAmountAdapter fromDateRange(Date start, Date end) { diff --git a/src/test/groovy/net/fortuna/ical4j/model/PeriodListSpec.groovy b/src/test/groovy/net/fortuna/ical4j/model/PeriodListSpec.groovy index 9bb2605d3..9a0be0681 100644 --- a/src/test/groovy/net/fortuna/ical4j/model/PeriodListSpec.groovy +++ b/src/test/groovy/net/fortuna/ical4j/model/PeriodListSpec.groovy @@ -2,8 +2,77 @@ package net.fortuna.ical4j.model import spock.lang.Specification +import java.time.LocalDate + class PeriodListSpec extends Specification { + static Period monthJanuary, monthFebruary, monthMarch, monthApril, + monthMay, monthJune, monthJuly, monthAugust, monthSeptember, monthOctober, + monthNovember, monthDecember, head1994, tail1994 + + def setupSpec() { + // create ranges that are intervals + LocalDate begin1994 = LocalDate.now().withYear(1994).withMonth(1).withDayOfMonth(1); + LocalDate end1994 = begin1994.withMonth(12).withDayOfMonth(31); + LocalDate jan1994 = end1994.withMonth(1).withDayOfMonth(22); + LocalDate feb1994 = jan1994.withMonth(2).withDayOfMonth(15); + LocalDate mar1994 = feb1994.withMonth(3).withDayOfMonth(4); + LocalDate apr1994 = mar1994.withMonth(4).withDayOfMonth(12); + LocalDate may1994 = apr1994.withMonth(5).withDayOfMonth(19); + LocalDate jun1994 = may1994.withMonth(6).withDayOfMonth(21); + LocalDate jul1994 = jun1994.withMonth(7).withDayOfMonth(28); + LocalDate aug1994 = jul1994.withMonth(8).withDayOfMonth(20); + LocalDate sep1994 = aug1994.withMonth(9).withDayOfMonth(17); + LocalDate oct1994 = sep1994.withMonth(10).withDayOfMonth(29); + LocalDate nov1994 = oct1994.withMonth(11).withDayOfMonth(11); + LocalDate dec1994 = nov1994.withMonth(12).withDayOfMonth(2); + + monthJanuary = new Period<>(jan1994, feb1994); + monthFebruary = new Period<>(feb1994, mar1994); + monthMarch = new Period<>(mar1994, apr1994); + monthApril = new Period<>(apr1994, may1994); + monthMay = new Period<>(may1994, jun1994); + monthJune = new Period<>(jun1994, jul1994); + monthJuly = new Period<>(jul1994, aug1994); + monthAugust = new Period<>(aug1994, sep1994); + monthSeptember = new Period<>(sep1994, oct1994); + monthOctober = new Period<>(oct1994, nov1994); + monthNovember = new Period<>(nov1994, dec1994); + monthDecember = new Period<>(dec1994, end1994); + head1994 = new Period<>(begin1994, jan1994); + tail1994 = new Period<>(dec1994, end1994); + + // create sets that contain the ranges + List> oddMonths = new ArrayList<>(); + oddMonths.add(monthJanuary); + oddMonths.add(monthMarch); + oddMonths.add(monthMay); + oddMonths.add(monthJuly); + oddMonths.add(monthSeptember); + oddMonths.add(monthNovember); + List> tailSet = new ArrayList<>(); + tailSet.add(tail1994); + + /* + * assertNull("Removing null from a null set should return null", empty1.subtract(null)); assertNull("Removing + * from a null set should return null", normalizer.subtractDateRanges(null, headSet)); + */ + PeriodList evenMonths = new PeriodList<>(); + evenMonths.add(monthFebruary); + evenMonths.add(monthApril); + evenMonths.add(monthJune); + evenMonths.add(monthAugust); + evenMonths.add(monthOctober); + evenMonths.add(monthDecember); + + PeriodList headSet = new PeriodList<>(); + headSet.add(head1994); + + PeriodList empty1 = new PeriodList<>(); + PeriodList empty2 = new PeriodList<>(); + + } + def 'test hashcode equality'() { given: 'a period list' PeriodList list1 = PeriodList.parse('20140803T120100/P1D') @@ -17,4 +86,34 @@ class PeriodListSpec extends Specification { and: 'hashcode equality' list1.hashCode() == list2.hashCode() } + + def 'test addition'() { + given: 'two period lists added' + def sum = new PeriodList(periodList1).add(new PeriodList(periodList2)) + + expect: 'result is as expected' + sum == new PeriodList(expectedSum) + + where: + periodList1 | periodList2 | expectedSum + [monthNovember, monthDecember] | [monthNovember, monthJuly] | [monthJuly, monthNovember, monthDecember] + [monthOctober, monthNovember, monthDecember] | [monthNovember] | [monthOctober, monthNovember, monthDecember] + [monthNovember, monthDecember] | [monthOctober, monthNovember] | [monthOctober, monthNovember, monthDecember] + } + + def 'test subtraction'() { + given: 'a period list subtracted from another' + def sum = new PeriodList(periodList1).subtract(new PeriodList(periodList2)) + + expect: 'result is as expected' + sum == new PeriodList(expectedSum) + + where: + periodList1 | periodList2 | expectedSum + [monthNovember, monthDecember] | [monthNovember] | [monthDecember] + [monthOctober, monthNovember, monthDecember] | [monthNovember] | [monthOctober, monthDecember] + [monthNovember, monthDecember] | [monthOctober, monthNovember] | [monthDecember] + [monthSeptember, monthOctober, monthNovember, monthDecember] | [monthOctober, monthNovember] | [monthSeptember, monthDecember] + [monthSeptember, monthOctober, monthNovember, monthDecember] | [monthApril, monthMay] | [monthSeptember, monthOctober, monthNovember, monthDecember] + } } diff --git a/src/test/groovy/net/fortuna/ical4j/model/PeriodSpec.groovy b/src/test/groovy/net/fortuna/ical4j/model/PeriodSpec.groovy index 9c6710c5a..8dd2cc221 100644 --- a/src/test/groovy/net/fortuna/ical4j/model/PeriodSpec.groovy +++ b/src/test/groovy/net/fortuna/ical4j/model/PeriodSpec.groovy @@ -33,11 +33,42 @@ package net.fortuna.ical4j.model import spock.lang.Specification +import java.time.ZonedDateTime + class PeriodSpec extends Specification { + static Period year1994, monthMarch , monthApril, monthMay, firstHalf, lastHalf, winter, spring, + marchToMay , marchToApril, aprToMay, janToMay, junToDec + + def setupSpec() { + ZonedDateTime past = ZonedDateTime.now().withYear(1980).withMonth(1).withDayOfMonth(23); + ZonedDateTime future = past.withYear(2022).withMonth(2); + ZonedDateTime begin1994 = future.withYear(1994).withMonth(1).withDayOfMonth(1); + ZonedDateTime end1994 = begin1994.withMonth(12).withDayOfMonth(31); + ZonedDateTime mar1994 = end1994.withMonth(3).withDayOfMonth(4); + ZonedDateTime apr1994 = mar1994.withMonth(4).withDayOfMonth(12); + ZonedDateTime may1994 = apr1994.withMonth(5).withDayOfMonth(19); + ZonedDateTime jun1994 = may1994.withMonth(6).withDayOfMonth(22); + ZonedDateTime jul1994 = jun1994.withMonth(7).withDayOfMonth(29); + + year1994 = new Period<>(begin1994, end1994); + monthMarch = new Period<>(mar1994, apr1994); + monthApril = new Period<>(apr1994, may1994); + monthMay = new Period<>(may1994, jun1994); + firstHalf = new Period<>(begin1994, jun1994); + lastHalf = new Period<>(may1994, end1994); + winter = new Period<>(begin1994, apr1994); + spring = new Period<>(apr1994, jul1994); + marchToMay = new Period<>(mar1994, jun1994); + marchToApril = new Period<>(mar1994, may1994); + aprToMay = new Period<>(apr1994, jun1994); + janToMay = new Period<>(begin1994, may1994); + junToDec = new Period<>(jun1994, end1994); + } + def 'extension module test: plus'() { expect: - Period.parse('20110412T120000/P1D') + Period.parse('20110413T120000/P1D') == Period.parse('20110412T120000/P2D') + Period.parse('20110412T120000/P1D') + Period.parse('20110413T120000/P1D') == Period.parse('20110412T120000/20110414T120000') } def 'extension module test: minus'() { @@ -58,4 +89,113 @@ class PeriodSpec extends Specification { and: 'hashcode equality' period1.hashCode() == period2.hashCode() } + + def 'test includes'() { + expect: 'result of period inclusion test is as expected' + period1.includes(date) == expectedIncludes + + where: + period1 | date | expectedIncludes + year1994 | year1994.start | true + year1994 | year1994.end | true + year1994 | monthMarch.start | true + year1994 | monthMarch.start.withYear(1980) | false + year1994 | monthMarch.start.withYear(2047) | false + } + + def 'test intersection'() { + expect: 'result of period intersection test is as expected' + period1.toInterval().overlaps(period2.toInterval()) == expectedIntersection + + where: + period1 | period2 | expectedIntersection + monthMarch | monthMay | false + monthMay | monthMarch | false + monthMarch | monthApril | false + monthApril | monthMarch | false + firstHalf | lastHalf | true + lastHalf | firstHalf | true + winter | monthMarch | true + monthMarch | winter | true + } + + def 'test contains'() { + expect: 'result of period containment test is as expected' + period1.toInterval().encloses(period2.toInterval()) == expectedContains + + where: + period1 | period2 | expectedContains + monthMarch | monthMay | false + monthMay | monthMarch | false + monthMarch | monthApril | false + monthApril | monthMarch | false + firstHalf | lastHalf | false + lastHalf | firstHalf | false + winter | monthMarch | true + monthMarch | winter | false + } + + def 'test addition'() { + expect: 'result of period addition test is as expected' + period1.add(period2) == expectedResult + + where: + period1 | period2 | expectedResult + monthMarch | monthMay | marchToMay + monthMay | monthMarch | marchToMay + monthMarch | monthApril | marchToApril + monthApril | monthMarch | marchToApril + firstHalf | lastHalf | year1994 + lastHalf | firstHalf | year1994 + winter | monthMarch | winter + monthMarch | winter | winter + } + + def 'test subtraction'() { + expect: 'result of period subtraction test is as expected' + period1.subtract(period2) == new PeriodList(expectedResult) + + where: + period1 | period2 | expectedResult + marchToMay | marchToApril | [monthMay] + monthMay | monthMarch | [monthMay] + monthMarch | monthApril | [monthMarch] + monthApril | monthMarch | [monthApril] + firstHalf | lastHalf | [janToMay] + lastHalf | firstHalf | [junToDec] +// winter | monthMarch | [winter] +// monthMarch | winter | [winter] + } + + def 'test before'() { + expect: 'result of period before test is as expected' + period1.toInterval().isBefore(period2.toInterval()) == expectedBefore + + where: + period1 | period2 | expectedBefore + monthMarch | monthMay | true + monthMay | monthMarch | false + monthMarch | monthApril | true + monthApril | monthMarch | false + firstHalf | lastHalf | false + lastHalf | firstHalf | false + winter | monthMarch | false + monthMarch | winter | false + } + + def 'test after'() { + expect: 'result of period after test is as expected' + period1.toInterval().isAfter(period2.toInterval()) == expectedAfter + + where: + period1 | period2 | expectedAfter + monthMarch | monthMay | false + monthMay | monthMarch | true + monthMarch | monthApril | false + monthApril | monthMarch | true + firstHalf | lastHalf | false + lastHalf | firstHalf | false + winter | monthMarch | false + monthMarch | winter | false + } } diff --git a/src/test/java/net/fortuna/ical4j/model/PeriodListTest.java b/src/test/java/net/fortuna/ical4j/model/PeriodListTest.java index ae05fc29a..afbea2c41 100644 --- a/src/test/java/net/fortuna/ical4j/model/PeriodListTest.java +++ b/src/test/java/net/fortuna/ical4j/model/PeriodListTest.java @@ -36,7 +36,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.time.Instant; +import java.time.LocalDate; import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; @@ -51,19 +54,19 @@ public class PeriodListTest extends TestCase { private Logger log = LoggerFactory.getLogger(PeriodListTest.class); - private PeriodList periodList; + private PeriodList periodList; - private PeriodList expectedPeriodList; + private PeriodList expectedPeriodList; private int expectedSize; - private Period expectedPeriod; + private Period expectedPeriod; /** * @param periodList * @param periodList2 */ - public PeriodListTest(PeriodList periodList, PeriodList expectedPeriodList) { + public PeriodListTest(PeriodList periodList, PeriodList expectedPeriodList) { super("testEquals"); this.periodList = periodList; this.expectedPeriodList = expectedPeriodList; @@ -73,7 +76,7 @@ public PeriodListTest(PeriodList periodList, PeriodList periodList, int expectedSize) { + public PeriodListTest(PeriodList periodList, int expectedSize) { super("testSize"); this.periodList = periodList; this.expectedSize = expectedSize; @@ -83,7 +86,7 @@ public PeriodListTest(PeriodList periodList, int expectedSize) { * @param periodList * @param expectedFirstPeriod */ - public PeriodListTest(String testMethod, PeriodList periodList, Period expectedPeriod) { + public PeriodListTest(String testMethod, PeriodList periodList, Period expectedPeriod) { super(testMethod); this.periodList = periodList; this.expectedPeriod = expectedPeriod; @@ -93,7 +96,7 @@ public PeriodListTest(String testMethod, PeriodList periodList, P * @param testMethod * @param periodList */ - public PeriodListTest(String testMethod, PeriodList periodList) { + public PeriodListTest(String testMethod, PeriodList periodList) { super(testMethod); this.periodList = periodList; } @@ -143,52 +146,52 @@ public void testContains() { public static TestSuite suite() { TestSuite suite = new TestSuite(); // create ranges that are intervals - ZonedDateTime begin1994 = ZonedDateTime.now().withYear(1994).withMonth(1).withDayOfMonth(1); - ZonedDateTime end1994 = begin1994.withMonth(12).withDayOfMonth(31); - ZonedDateTime jan1994 = end1994.withMonth(1).withDayOfMonth(22); - ZonedDateTime feb1994 = jan1994.withMonth(2).withDayOfMonth(15); - ZonedDateTime mar1994 = feb1994.withMonth(3).withDayOfMonth(4); - ZonedDateTime apr1994 = mar1994.withMonth(4).withDayOfMonth(12); - ZonedDateTime may1994 = apr1994.withMonth(5).withDayOfMonth(19); - ZonedDateTime jun1994 = may1994.withMonth(6).withDayOfMonth(21); - ZonedDateTime jul1994 = jun1994.withMonth(7).withDayOfMonth(28); - ZonedDateTime aug1994 = jul1994.withMonth(8).withDayOfMonth(20); - ZonedDateTime sep1994 = aug1994.withMonth(9).withDayOfMonth(17); - ZonedDateTime oct1994 = sep1994.withMonth(10).withDayOfMonth(29); - ZonedDateTime nov1994 = oct1994.withMonth(11).withDayOfMonth(11); - ZonedDateTime dec1994 = nov1994.withMonth(12).withDayOfMonth(2); - - Period monthJanuary = new Period<>(jan1994, feb1994); - Period monthFebruary = new Period<>(feb1994, mar1994); - Period monthMarch = new Period<>(mar1994, apr1994); - Period monthApril = new Period<>(apr1994, may1994); - Period monthMay = new Period<>(may1994, jun1994); - Period monthJune = new Period<>(jun1994, jul1994); - Period monthJuly = new Period<>(jul1994, aug1994); - Period monthAugust = new Period<>(aug1994, sep1994); - Period monthSeptember = new Period<>(sep1994, oct1994); - Period monthOctober = new Period<>(oct1994, nov1994); - Period monthNovember = new Period<>(nov1994, dec1994); - Period monthDecember = new Period<>(dec1994, end1994); - Period head1994 = new Period<>(begin1994, jan1994); - Period tail1994 = new Period<>(dec1994, end1994); + LocalDate begin1994 = LocalDate.now().withYear(1994).withMonth(1).withDayOfMonth(1); + LocalDate end1994 = begin1994.withMonth(12).withDayOfMonth(31); + LocalDate jan1994 = end1994.withMonth(1).withDayOfMonth(22); + LocalDate feb1994 = jan1994.withMonth(2).withDayOfMonth(15); + LocalDate mar1994 = feb1994.withMonth(3).withDayOfMonth(4); + LocalDate apr1994 = mar1994.withMonth(4).withDayOfMonth(12); + LocalDate may1994 = apr1994.withMonth(5).withDayOfMonth(19); + LocalDate jun1994 = may1994.withMonth(6).withDayOfMonth(21); + LocalDate jul1994 = jun1994.withMonth(7).withDayOfMonth(28); + LocalDate aug1994 = jul1994.withMonth(8).withDayOfMonth(20); + LocalDate sep1994 = aug1994.withMonth(9).withDayOfMonth(17); + LocalDate oct1994 = sep1994.withMonth(10).withDayOfMonth(29); + LocalDate nov1994 = oct1994.withMonth(11).withDayOfMonth(11); + LocalDate dec1994 = nov1994.withMonth(12).withDayOfMonth(2); + + Period monthJanuary = new Period<>(jan1994, feb1994); + Period monthFebruary = new Period<>(feb1994, mar1994); + Period monthMarch = new Period<>(mar1994, apr1994); + Period monthApril = new Period<>(apr1994, may1994); + Period monthMay = new Period<>(may1994, jun1994); + Period monthJune = new Period<>(jun1994, jul1994); + Period monthJuly = new Period<>(jul1994, aug1994); + Period monthAugust = new Period<>(aug1994, sep1994); + Period monthSeptember = new Period<>(sep1994, oct1994); + Period monthOctober = new Period<>(oct1994, nov1994); + Period monthNovember = new Period<>(nov1994, dec1994); + Period monthDecember = new Period<>(dec1994, end1994); + Period head1994 = new Period<>(begin1994, jan1994); + Period tail1994 = new Period<>(dec1994, end1994); // create sets that contain the ranges - List> oddMonths = new ArrayList<>(); + List> oddMonths = new ArrayList<>(); oddMonths.add(monthJanuary); oddMonths.add(monthMarch); oddMonths.add(monthMay); oddMonths.add(monthJuly); oddMonths.add(monthSeptember); oddMonths.add(monthNovember); - List> tailSet = new ArrayList<>(); + List> tailSet = new ArrayList<>(); tailSet.add(tail1994); /* * assertNull("Removing null from a null set should return null", empty1.subtract(null)); assertNull("Removing * from a null set should return null", normalizer.subtractDateRanges(null, headSet)); */ - PeriodList evenMonths = new PeriodList<>(); + PeriodList evenMonths = new PeriodList<>(); evenMonths.add(monthFebruary); evenMonths.add(monthApril); evenMonths.add(monthJune); @@ -196,188 +199,17 @@ public static TestSuite suite() { evenMonths.add(monthOctober); evenMonths.add(monthDecember); - PeriodList headSet = new PeriodList<>(); + PeriodList headSet = new PeriodList<>(); headSet.add(head1994); - PeriodList empty1 = new PeriodList<>(); - PeriodList empty2 = new PeriodList<>(); + PeriodList empty1 = new PeriodList<>(); + PeriodList empty2 = new PeriodList<>(); suite.addTest(new PeriodListTest(evenMonths.subtract(null), evenMonths)); suite.addTest(new PeriodListTest(empty1.subtract(empty2), empty1)); suite.addTest(new PeriodListTest(headSet.subtract(empty1), headSet)); suite.addTest(new PeriodListTest(evenMonths.subtract(empty1), evenMonths)); - - // add disjoint ranges.. - PeriodList periodList1 = new PeriodList<>(); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - PeriodList periodList2 = new PeriodList<>(); - periodList2.add(monthJuly); - periodList2.add(monthNovember); - - /* - * SortedSet normalizedSet = normalizer.addDateRanges(dateRangeSet1, dateRangeSet2); - */ - PeriodList sum = periodList1.add(periodList2); - suite.addTest(new PeriodListTest(sum, 2)); -// Period lonePeriod = (Period) sum.toArray()[0]; -// assertEquals(lonePeriod.getStart(), jul1994); -// assertEquals(lonePeriod.getEnd(), aug1994); - suite.addTest(new PeriodListTest("testFirstPeriodEquals", sum, new Period<>(jul1994, aug1994))); - - // add one range containing another.. - periodList1 = new PeriodList<>(); - periodList1.add(monthOctober); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - - periodList2 = new PeriodList<>(); - periodList2.add(monthNovember); - - /* - * SortedSet normalizedSet = normalizer.addDateRanges(dateRangeSet1, dateRangeSet2); - */ - sum = periodList1.add(periodList2); - suite.addTest(new PeriodListTest(sum, 1)); -// Period lonePeriod = (Period) sum.toArray()[0]; -// assertEquals(lonePeriod.getStart(), oct1994); -// assertEquals(lonePeriod.getEnd(), end1994); - suite.addTest(new PeriodListTest("testFirstPeriodEquals", sum, new Period<>(oct1994, end1994))); - - // Test Intersecting Periods - periodList1 = new PeriodList<>(); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - - periodList2 = new PeriodList<>(); - periodList2.add(monthOctober); - periodList2.add(monthNovember); - - /* - * SortedSet normalizedSet = normalizer.addDateRanges(dateRangeSet1, dateRangeSet2); - */ - sum = periodList1.add(periodList2); - suite.addTest(new PeriodListTest(sum, 1)); -// Period lonePeriod = (Period) sum.toArray()[0]; -// assertEquals(lonePeriod.getStart(), oct1994); -// assertEquals(lonePeriod.getEnd(), end1994); - suite.addTest(new PeriodListTest("testFirstPeriodEquals", sum, new Period<>(oct1994, end1994))); - - // Test adding adjacent periods. - periodList1 = new PeriodList<>(); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - - periodList2 = new PeriodList<>(); - periodList2.add(monthOctober); - - /* - * SortedSet normalizedSet = normalizer.addDateRanges(dateRangeSet1, dateRangeSet2); - */ - sum = periodList1.add(periodList2); - suite.addTest(new PeriodListTest(sum, 1)); -// Period lonePeriod = (Period) sum.toArray()[0]; -// assertEquals(lonePeriod.getStart(), oct1994); -// assertEquals(lonePeriod.getEnd(), end1994); - suite.addTest(new PeriodListTest("testFirstPeriodEquals", sum, new Period<>(oct1994, end1994))); - - // Test adding the same range twice - periodList1 = new PeriodList<>(); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - - periodList2 = new PeriodList<>(); - periodList2.add(monthOctober); - periodList2.add(monthNovember); - - /* - * SortedSet normalizedSet1 = normalizer.addDateRanges(dateRangeSet1, dateRangeSet2); SortedSet normalizedSet2 = - * normalizer.addDateRanges(dateRangeSet1, dateRangeSet2); - */ - PeriodList sum1 = periodList1.add(periodList2); - suite.addTest(new PeriodListTest(sum1, 1)); -// Period lonePeriod1 = (Period) sum1.toArray()[0]; -// assertEquals(lonePeriod1.getStart(), oct1994); -// assertEquals(lonePeriod1.getEnd(), end1994); - suite.addTest(new PeriodListTest("testFirstPeriodEquals", sum1, new Period<>(oct1994, end1994))); - - PeriodList sum2 = periodList1.add(periodList2); - suite.addTest(new PeriodListTest(sum2, 1)); -// Period lonePeriod2 = (Period) sum2.toArray()[0]; -// assertEquals(lonePeriod2.getStart(), oct1994); -// assertEquals(lonePeriod2.getEnd(), end1994); - suite.addTest(new PeriodListTest("testFirstPeriodEquals", sum2, new Period<>(oct1994, end1994))); - - // Test subtract a containing date range set.. - periodList1 = new PeriodList<>(); - periodList1.add(monthSeptember); - periodList1.add(monthOctober); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - - periodList2 = new PeriodList<>(); - periodList2.add(monthOctober); - periodList2.add(monthNovember); - - /* - * SortedSet normalizedSet = normalizer.subtractDateRanges(dateRangeSet1, dateRangeSet2); - */ - sum = periodList1.subtract(periodList2); - suite.addTest(new PeriodListTest(sum, 2)); -// Period lonePeriod1 = (Period) sum.toArray()[0]; -// assertEquals(lonePeriod1.getStart(), sep1994); -// assertEquals(lonePeriod1.getEnd(), oct1994); - suite.addTest(new PeriodListTest("testFirstPeriodEquals", sum, new Period<>(sep1994, oct1994))); - - // FIXME: don't use asserts here.. - Period lonePeriod2 = sum.getPeriods().iterator().next(); - assertEquals(lonePeriod2.getStart(), dec1994); - assertEquals(lonePeriod2.getEnd(), end1994); - - // Test removing a Disjoint Set of Date Ranges.. - periodList1 = new PeriodList<>(); - periodList1.add(monthSeptember); - periodList1.add(monthOctober); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - - periodList2 = new PeriodList<>(); - periodList2.add(monthApril); - periodList2.add(monthMay); - - /* - * SortedSet normalizedSet = normalizer.subtractDateRanges(dateRangeSet1, dateRangeSet2); - */ - sum = periodList1.subtract(periodList2); - suite.addTest(new PeriodListTest(sum, periodList1)); - - // SubtractSameRangesTwice... - periodList1 = new PeriodList<>(); - periodList1.add(monthSeptember); - periodList1.add(monthOctober); - periodList1.add(monthNovember); - periodList1.add(monthDecember); - - periodList2 = new PeriodList<>(); - periodList2.add(monthOctober); - periodList2.add(monthNovember); - - PeriodList expectedResult = new PeriodList<>(); - expectedResult.add(monthSeptember); - expectedResult.add(monthDecember); - - /* - * SortedSet normalizedSet = normalizer.subtractDateRanges(dateRangeSet1, dateRangeSet2); - */ - sum = periodList1.subtract(periodList2); - suite.addTest(new PeriodListTest(sum, expectedResult)); - /* - * normalizedSet = normalizer.subtractDateRanges(dateRangeSet1, dateRangeSet2); - */ - sum = periodList1.subtract(periodList2); - suite.addTest(new PeriodListTest(sum, expectedResult)); - // other tests.. suite.addTest(new PeriodListTest("testTimezone")); suite.addTest(new PeriodListTest("testNormalise")); @@ -386,9 +218,9 @@ public static TestSuite suite() { } public final void testPeriodListSort() { - PeriodList periods = new PeriodList<>(); - ZonedDateTime start = ZonedDateTime.now(); - ZonedDateTime end = start.withDayOfMonth(25); + PeriodList periods = new PeriodList<>(); + LocalDate start = LocalDate.now(); + LocalDate end = start.withDayOfMonth(25); periods.add(new Period<>(start, end)); periods.add(new Period<>(end, java.time.Duration.ofHours(2))); periods.add(new Period<>(start, java.time.Duration.ofHours(2))); @@ -459,11 +291,11 @@ public void testTimezone() { // .createRegistry(); // TimeZone timezone = registry.getTimeZone("Australia/Melbourne"); - PeriodList list = new PeriodList<>(); + PeriodList list = new PeriodList<>(CalendarDateFormat.UTC_DATE_TIME_FORMAT); for (int i = 0; i < 5; i++) { - ZonedDateTime start = ZonedDateTime.now(); - ZonedDateTime end = start.plusDays(1); + Instant start = Instant.now(); + Instant end = start.plusSeconds(ChronoUnit.DAYS.getDuration().getSeconds()); list.add(new Period<>(start, end)); } diff --git a/src/test/java/net/fortuna/ical4j/model/PeriodTest.java b/src/test/java/net/fortuna/ical4j/model/PeriodTest.java index 7d5573f8e..9801a4601 100644 --- a/src/test/java/net/fortuna/ical4j/model/PeriodTest.java +++ b/src/test/java/net/fortuna/ical4j/model/PeriodTest.java @@ -416,30 +416,6 @@ public void testIsEmpty() { public static Test suite() { TestSuite suite = new TestSuite(); - ZonedDateTime past = ZonedDateTime.now().withYear(1980).withMonth(1).withDayOfMonth(23); - ZonedDateTime future = past.withYear(2022).withMonth(2); - ZonedDateTime begin1994 = future.withYear(1994).withMonth(1).withDayOfMonth(1); - ZonedDateTime end1994 = begin1994.withMonth(12).withDayOfMonth(31); - ZonedDateTime mar1994 = end1994.withMonth(3).withDayOfMonth(4); - ZonedDateTime apr1994 = mar1994.withMonth(4).withDayOfMonth(12); - ZonedDateTime may1994 = apr1994.withMonth(5).withDayOfMonth(19); - ZonedDateTime jun1994 = may1994.withMonth(6).withDayOfMonth(22); - ZonedDateTime jul1994 = jun1994.withMonth(7).withDayOfMonth(29); - - Period year1994 = new Period<>(begin1994, end1994); - Period monthMarch = new Period<>(mar1994, apr1994); - Period monthApril = new Period<>(apr1994, may1994); - Period monthMay = new Period<>(may1994, jun1994); - Period firstHalf = new Period<>(begin1994, jun1994); - Period lastHalf = new Period<>(may1994, end1994); - Period winter = new Period<>(begin1994, apr1994); - Period spring = new Period<>(apr1994, jul1994); - Period marchToMay = new Period<>(mar1994, jun1994); - Period marchToApril = new Period<>(mar1994, may1994); -// Period duplicateRange = new Period(begin1994, end1994); - - Period testPeriod; - /* long testMillis; long todayMillis; @@ -464,10 +440,6 @@ public static Test suite() { (todayMillis - testMillis) < 5000); */ - testPeriod = new Period<>(past, future); - suite.addTest(new PeriodTest("testGetStart", testPeriod, past)); - suite.addTest(new PeriodTest("testGetEnd", testPeriod, future)); - /* testRange = new DateRange(); testRange.setEndDate(future); @@ -484,75 +456,20 @@ public static Test suite() { assertEquals(future, testRange.getEndDate()); */ - suite.addTest(new PeriodTest("testNotIncludes", year1994, past)); - suite.addTest(new PeriodTest("testIncludes", year1994, mar1994)); - suite.addTest(new PeriodTest("testNotIncludes", year1994, future)); - suite.addTest(new PeriodTest("testIncludes", year1994, begin1994)); - suite.addTest(new PeriodTest("testIncludes", year1994, end1994)); - - suite.addTest(new PeriodTest("testBefore", monthMarch, monthMay)); - suite.addTest(new PeriodTest("testNotBefore", monthMay, monthMarch)); - suite.addTest(new PeriodTest("testNotBefore", winter, monthMarch)); - suite.addTest(new PeriodTest("testNotBefore", monthMarch, winter)); - suite.addTest(new PeriodTest("testNotBefore", firstHalf, lastHalf)); - suite.addTest(new PeriodTest("testNotBefore", lastHalf, firstHalf)); - // because month march end is same as month april start, march is not - // before april.. - suite.addTest(new PeriodTest("testNotBefore", monthMarch, monthApril)); - suite.addTest(new PeriodTest("testNotBefore", monthApril, monthMarch)); - - suite.addTest(new PeriodTest("testNotAfter", monthMarch, monthMay)); - suite.addTest(new PeriodTest("testAfter", monthMay, monthMarch)); - suite.addTest(new PeriodTest("testNotAfter", winter, monthMarch)); - suite.addTest(new PeriodTest("testNotAfter", monthMarch, winter)); - suite.addTest(new PeriodTest("testNotAfter", firstHalf, lastHalf)); - suite.addTest(new PeriodTest("testNotAfter", lastHalf, firstHalf)); - suite.addTest(new PeriodTest("testNotAfter", monthMarch, monthApril)); - // because month march end is same as month april start, april is not - // after march.. - suite.addTest(new PeriodTest("testNotAfter", monthApril, monthMarch)); - - suite.addTest(new PeriodTest("testNotIntersects", monthMarch, monthMay)); - suite.addTest(new PeriodTest("testNotIntersects", monthMay, monthMarch)); - suite.addTest(new PeriodTest("testNotIntersects", monthMarch, monthApril)); - suite.addTest(new PeriodTest("testNotIntersects", monthApril, monthMarch)); - suite.addTest(new PeriodTest("testIntersects", firstHalf, lastHalf)); - suite.addTest(new PeriodTest("testIntersects", lastHalf, firstHalf)); - suite.addTest(new PeriodTest("testIntersects", winter, monthMarch)); - suite.addTest(new PeriodTest("testIntersects", monthMarch, winter)); - - suite.addTest(new PeriodTest("testNotContains", monthMarch, monthMay)); - suite.addTest(new PeriodTest("testNotContains", monthMay, monthMarch)); - suite.addTest(new PeriodTest("testNotContains", monthMarch, monthApril)); - suite.addTest(new PeriodTest("testNotContains", monthApril, monthMarch)); - suite.addTest(new PeriodTest("testNotContains", firstHalf, lastHalf)); - suite.addTest(new PeriodTest("testNotContains", lastHalf, firstHalf)); - suite.addTest(new PeriodTest("testContains", winter, monthMarch)); - suite.addTest(new PeriodTest("testNotContains", monthMarch, winter)); - - suite.addTest(new PeriodTest("testEquals", monthMarch.add(monthMay), marchToMay)); - suite.addTest(new PeriodTest("testEquals", monthMay.add(monthMarch), marchToMay)); - suite.addTest(new PeriodTest("testEquals", monthMarch.add(monthApril), marchToApril)); - suite.addTest(new PeriodTest("testEquals", monthApril.add(monthMarch), marchToApril)); - suite.addTest(new PeriodTest("testEquals", firstHalf.add(lastHalf), year1994)); - suite.addTest(new PeriodTest("testEquals", lastHalf.add(firstHalf), year1994)); - suite.addTest(new PeriodTest("testEquals", winter.add(monthMarch), winter)); - suite.addTest(new PeriodTest("testEquals", monthMarch.add(winter), winter)); - // test period contained by subtraction.. - suite.addTest(new PeriodListTest("testIsEmpty", firstHalf.subtract(year1994))); - suite.addTest(new PeriodListTest("testIsEmpty", winter.subtract(winter))); +// suite.addTest(new PeriodListTest("testIsEmpty", firstHalf.subtract(year1994))); +// suite.addTest(new PeriodListTest("testIsEmpty", winter.subtract(winter))); // test non-intersecting periods.. - suite.addTest(new PeriodListTest("testContains", winter.subtract(spring), winter)); - suite.addTest(new PeriodListTest(winter.subtract(spring), 1)); +// suite.addTest(new PeriodListTest("testContains", winter.subtract(spring), winter)); +// suite.addTest(new PeriodListTest(winter.subtract(spring), 1)); // test intersecting periods.. - PeriodList aprToMay = marchToMay.subtract(marchToApril); - suite.addTest(new PeriodListTest(aprToMay, 1)); +// PeriodList aprToMay = marchToMay.subtract(marchToApril); +// suite.addTest(new PeriodListTest(aprToMay, 1)); // test subtraction contained by period.. - suite.addTest(new PeriodListTest(year1994.subtract(monthApril), 2)); +// suite.addTest(new PeriodListTest(year1994.subtract(monthApril), 2)); ZoneId zoneId = TimeZoneRegistry.getGlobalZoneId("Australia/Melbourne"); ZonedDateTime start = ((LocalDateTime) TemporalAdapter.parse("20081115T163800").getTemporal())