Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make RucrrencePattern.Count nullable to avoid using magic number `i…
…nt.MinValue`.
  • Loading branch information
minichma committed Dec 9, 2024
commit 86ecb64d27d09c67480f3684368bfbc63e854d3e
4 changes: 2 additions & 2 deletions Ical.Net/DataTypes/RecurrencePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class RecurrencePattern : EncodableDataType

public DateTime? Until { get; set; }

public int Count { get; set; } = int.MinValue;
public int? Count { get; set; }

/// <summary>
/// Specifies how often the recurrence should repeat.
Expand Down Expand Up @@ -181,7 +181,7 @@ public override int GetHashCode()
#pragma warning restore 0618
hashCode = (hashCode * 397) ^ (int) Frequency;
hashCode = (hashCode * 397) ^ Until.GetHashCode();
hashCode = (hashCode * 397) ^ Count;
hashCode = (hashCode * 397) ^ (Count ?? 0);
hashCode = (hashCode * 397) ^ (int) FirstDayOfWeek;
hashCode = (hashCode * 397) ^ CollectionHelpers.GetHashCode(BySecond);
hashCode = (hashCode * 397) ^ CollectionHelpers.GetHashCode(ByMinute);
Expand Down
10 changes: 7 additions & 3 deletions Ical.Net/Evaluation/RecurrencePatternEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,18 @@ private IEnumerable<DateTime> GetDates(IDateTime seed, DateTime? periodStart, Da

// optimize the start time for selecting candidates
// (only applicable where a COUNT is not specified)
if (pattern.Count == int.MinValue)
if (pattern.Count is null)
{
var incremented = seedCopy;
while (incremented < periodStart)
{
seedCopy = incremented;
IncrementDate(ref incremented, pattern, pattern.Interval);
}
} else
{
if (pattern.Count < 1)
throw new Exception("Count must be greater than 0");
}

// Do the enumeration in a separate method, as it is a generator method that is
Expand Down Expand Up @@ -256,7 +260,7 @@ private IEnumerable<DateTime> EnumerateDates(DateTime originalDate, DateTime see
break;
}

if (pattern.Count >= 1 && dateCount >= pattern.Count)
if (dateCount >= pattern.Count)
{
break;
}
Expand All @@ -281,7 +285,7 @@ private IEnumerable<DateTime> EnumerateDates(DateTime originalDate, DateTime see
// from the previous year.
//
// exclude candidates that start at the same moment as periodEnd if the period is a range but keep them if targeting a specific moment
if (pattern.Count >= 1 && dateCount >= pattern.Count)
if (dateCount >= pattern.Count)
{
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Evaluation/TodoEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void DetermineStartingRecurrence(PeriodList rdate, ref IDateTime referen

private void DetermineStartingRecurrence(RecurrencePattern recur, ref IDateTime referenceDateTime)
{
if (recur.Count != int.MinValue)
if (recur.Count.HasValue)
{
referenceDateTime = Todo.Start.Copy<IDateTime>();
}
Expand Down
26 changes: 25 additions & 1 deletion Ical.Net/Serialization/DataTypes/RecurrencePatternSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,27 @@ public virtual void CheckRange(string name, IList<int> values, int min, int max)
}
}

public virtual void CheckRange(string name, IList<int?> values, int min, int max)
{
var allowZero = (min == 0 || max == 0);
foreach (var value in values)
{
CheckRange(name, value, min, max, allowZero);
}
}

public virtual void CheckRange(string name, int value, int min, int max)
{
var allowZero = min == 0 || max == 0;
CheckRange(name, value, min, max, allowZero);
}

public virtual void CheckRange(string name, int? value, int min, int max)
{
var allowZero = min == 0 || max == 0;
CheckRange(name, value, min, max, allowZero);
}

public virtual void CheckRange(string name, int value, int min, int max, bool allowZero)
{
if (value != int.MinValue && (value < min || value > max || (!allowZero && value == 0)))
Expand All @@ -73,6 +88,15 @@ public virtual void CheckRange(string name, int value, int min, int max, bool al
}
}

public virtual void CheckRange(string name, int? value, int min, int max, bool allowZero)
{
if (value != null && (value < min || value > max || (!allowZero && value == 0)))
{
throw new ArgumentException(name + " value " + value + " is out of range. Valid values are between " + min + " and " + max +
(allowZero ? "" : ", excluding zero (0)") + ".");
}
}

public virtual void CheckMutuallyExclusive<T, TU>(string name1, string name2, T obj1, TU obj2)
{
if (Equals(obj1, default(T)) || Equals(obj2, default(TU)))
Expand Down Expand Up @@ -160,7 +184,7 @@ public override string SerializeToString(object obj)
values.Add("WKST=" + Enum.GetName(typeof(DayOfWeek), recur.FirstDayOfWeek).ToUpper().Substring(0, 2));
}

if (recur.Count != int.MinValue)
if (recur.Count.HasValue)
{
values.Add("COUNT=" + recur.Count);
}
Expand Down