diff --git a/net-core/Ical.Net/Ical.Net/Components/Alarm.cs b/net-core/Ical.Net/Ical.Net/Components/Alarm.cs index 4d1872f89..2b3a4bb38 100644 --- a/net-core/Ical.Net/Ical.Net/Components/Alarm.cs +++ b/net-core/Ical.Net/Ical.Net/Components/Alarm.cs @@ -56,8 +56,8 @@ public virtual string Summary public virtual Trigger Trigger { - get => Properties.Get("TRIGGER"); - set => Properties.Set("TRIGGER", value); + get => Properties.Get(TriggerRelation.Key); + set => Properties.Set(TriggerRelation.Key, value); } protected virtual IList Occurrences { get; set; } @@ -96,7 +96,7 @@ public virtual IList GetOccurrences(IRecurringComponent rc, IDa foreach (var o in rc.GetOccurrences(fromDate, toDate)) { var dt = o.Period.StartTime; - if (Trigger.Related == TriggerRelation.End) + if (string.Equals(Trigger.Related, TriggerRelation.End, TriggerRelation.Comparison)) { if (o.Period.EndTime != null) { diff --git a/net-core/Ical.Net/Ical.Net/Constants.cs b/net-core/Ical.Net/Ical.Net/Constants.cs index 5f39b6baa..a621f9a57 100644 --- a/net-core/Ical.Net/Ical.Net/Constants.cs +++ b/net-core/Ical.Net/Ical.Net/Constants.cs @@ -16,10 +16,14 @@ public static class AlarmAction public const string Procedure = "PROCEDURE"; } - public enum TriggerRelation + public static class TriggerRelation { - Start, - End + public const string Name = "TRIGGER"; + public const string Key = "TRIGGER"; + public static readonly StringComparison Comparison = StringComparison.Ordinal; + + public const string Start = "START"; + public const string End = "END"; } public static class Components diff --git a/net-core/Ical.Net/Ical.Net/DataTypes/Trigger.cs b/net-core/Ical.Net/Ical.Net/DataTypes/Trigger.cs index 04d2c424b..86a7d7164 100644 --- a/net-core/Ical.Net/Ical.Net/DataTypes/Trigger.cs +++ b/net-core/Ical.Net/Ical.Net/DataTypes/Trigger.cs @@ -14,7 +14,7 @@ public class Trigger : EncodableDataType { private IDateTime _mDateTime; private TimeSpan? _mDuration; - private TriggerRelation _mRelated = TriggerRelation.Start; + private string _mRelated = TriggerRelation.Start; public virtual IDateTime DateTime { @@ -22,17 +22,19 @@ public virtual IDateTime DateTime set { _mDateTime = value; - if (_mDateTime != null) + if (_mDateTime == null) { - // NOTE: this, along with the "Duration" setter, fixes the bug tested in - // TODO11(), as well as this thread: https://sourceforge.net/forum/forum.php?thread_id=1926742&forum_id=656447 + return; + } - // DateTime and Duration are mutually exclusive - Duration = null; + // NOTE: this, along with the "Duration" setter, fixes the bug tested in + // TODO11(), as well as this thread: https://sourceforge.net/forum/forum.php?thread_id=1926742&forum_id=656447 - // Do not allow timeless date/time values - _mDateTime.HasTime = true; - } + // DateTime and Duration are mutually exclusive + Duration = null; + + // Do not allow timeless date/time values + _mDateTime.HasTime = true; } } @@ -52,7 +54,7 @@ public virtual TimeSpan? Duration } } - public virtual TriggerRelation Related + public virtual string Related { get => _mRelated; set => _mRelated = value; @@ -62,7 +64,10 @@ public virtual TriggerRelation Related public Trigger() {} - public Trigger(TimeSpan ts) => Duration = ts; + public Trigger(TimeSpan ts) + { + Duration = ts; + } public Trigger(string value) : this() { @@ -73,13 +78,15 @@ public Trigger(string value) : this() public override void CopyFrom(ICopyable obj) { base.CopyFrom(obj); - if (obj is Trigger) + if (!(obj is Trigger)) { - var t = (Trigger) obj; - DateTime = t.DateTime; - Duration = t.Duration; - Related = t.Related; + return; } + + var t = (Trigger) obj; + DateTime = t.DateTime; + Duration = t.Duration; + Related = t.Related; } protected bool Equals(Trigger other) => Equals(_mDateTime, other._mDateTime) && _mDuration.Equals(other._mDuration) && _mRelated == other._mRelated; @@ -107,7 +114,7 @@ public override int GetHashCode() { var hashCode = _mDateTime?.GetHashCode() ?? 0; hashCode = (hashCode * 397) ^ _mDuration.GetHashCode(); - hashCode = (hashCode * 397) ^ (int) _mRelated; + hashCode = (hashCode * 397) ^ _mRelated?.GetHashCode() ?? 0; return hashCode; } } diff --git a/net-core/Ical.Net/Ical.Net/Serialization/DataTypeMapper.cs b/net-core/Ical.Net/Ical.Net/Serialization/DataTypeMapper.cs index 60fb975c7..828b7cf8e 100644 --- a/net-core/Ical.Net/Ical.Net/Serialization/DataTypeMapper.cs +++ b/net-core/Ical.Net/Ical.Net/Serialization/DataTypeMapper.cs @@ -53,7 +53,7 @@ public DataTypeMapper() AddPropertyMapping("SEQUENCE", typeof (int), false); AddPropertyMapping("STATUS", ResolveStatusProperty, false); AddPropertyMapping("TRANSP", typeof (TransparencyType), false); - AddPropertyMapping("TRIGGER", typeof (Trigger), false); + AddPropertyMapping(TriggerRelation.Name, typeof (Trigger), false); AddPropertyMapping("TZNAME", typeof (string), false); AddPropertyMapping("TZOFFSETFROM", typeof (UtcOffset), false); AddPropertyMapping("TZOFFSETTO", typeof (UtcOffset), false); diff --git a/net-core/Ical.Net/Ical.Net/Serialization/iCalendar/Serializers/DataTypes/TriggerSerializer.cs b/net-core/Ical.Net/Ical.Net/Serialization/iCalendar/Serializers/DataTypes/TriggerSerializer.cs index 1030c7451..86e1a13c4 100644 --- a/net-core/Ical.Net/Ical.Net/Serialization/iCalendar/Serializers/DataTypes/TriggerSerializer.cs +++ b/net-core/Ical.Net/Ical.Net/Serialization/iCalendar/Serializers/DataTypes/TriggerSerializer.cs @@ -20,32 +20,37 @@ public override string SerializeToString(object obj) { try { - var t = obj as Trigger; - if (t != null) + if (!(obj is Trigger t)) { - // Push the trigger onto the serialization stack - SerializationContext.Push(t); - try + return null; + } + + // Push the trigger onto the serialization stack + SerializationContext.Push(t); + try + { + var factory = GetService(); + if (factory == null) { - var factory = GetService(); - if (factory != null) - { - var valueType = t.GetValueType() ?? typeof (TimeSpan); - var serializer = factory.Build(valueType, SerializationContext) as IStringSerializer; - if (serializer != null) - { - var value = (valueType == typeof (IDateTime)) ? t.DateTime : (object) t.Duration; - return serializer.SerializeToString(value); - } - } + return null; } - finally + + var valueType = t.GetValueType() ?? typeof(TimeSpan); + if (!(factory.Build(valueType, SerializationContext) is IStringSerializer serializer)) { - // Pop the trigger off the serialization stack - SerializationContext.Pop(); + return null; } + + var value = valueType == typeof(IDateTime) + ? t.DateTime + : (object) t.Duration; + return serializer.SerializeToString(value); + } + finally + { + // Pop the trigger off the serialization stack + SerializationContext.Pop(); } - return null; } catch { @@ -57,50 +62,52 @@ public override object Deserialize(TextReader tr) { var value = tr.ReadToEnd(); - var t = CreateAndAssociate() as Trigger; - if (t != null) + if (!(CreateAndAssociate() is Trigger t)) { - // Push the trigger onto the serialization stack - SerializationContext.Push(t); - try - { - // Decode the value as needed - value = Decode(t, value); + return null; + } - // Set the trigger relation - if (t.Parameters.ContainsKey("RELATED") && t.Parameters.Get("RELATED").Equals("END")) - { - t.Related = TriggerRelation.End; - } + // Push the trigger onto the serialization stack + SerializationContext.Push(t); + try + { + // Decode the value as needed + value = Decode(t, value); - var factory = GetService(); - if (factory != null) - { - var valueType = t.GetValueType() ?? typeof (TimeSpan); - var serializer = factory.Build(valueType, SerializationContext) as IStringSerializer; - var obj = serializer?.Deserialize(new StringReader(value)); - if (obj != null) - { - if (obj is IDateTime) - { - t.DateTime = (IDateTime) obj; - } - else - { - t.Duration = (TimeSpan) obj; - } + // Set the trigger relation + if (t.Parameters.ContainsKey("RELATED") && t.Parameters.Get("RELATED").Equals("END")) + { + t.Related = TriggerRelation.End; + } - return t; - } - } + var factory = GetService(); + if (factory == null) + { + return null; } - finally + + var valueType = t.GetValueType() ?? typeof(TimeSpan); + var serializer = factory.Build(valueType, SerializationContext) as IStringSerializer; + var obj = serializer?.Deserialize(new StringReader(value)); + switch (obj) { - // Pop the trigger off the serialization stack - SerializationContext.Pop(); + case null: + return null; + case IDateTime _: + t.DateTime = (IDateTime) obj; + break; + default: + t.Duration = (TimeSpan) obj; + break; } + + return t; + } + finally + { + // Pop the trigger off the serialization stack + SerializationContext.Pop(); } - return null; } } } \ No newline at end of file