Skip to content

Commit

Permalink
COnvert TriggerRelation from enum to class #318
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian Stockbower committed Nov 9, 2017
1 parent e0cbaf0 commit bc91b0d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 80 deletions.
6 changes: 3 additions & 3 deletions net-core/Ical.Net/Ical.Net/Components/Alarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public virtual string Summary

public virtual Trigger Trigger
{
get => Properties.Get<Trigger>("TRIGGER");
set => Properties.Set("TRIGGER", value);
get => Properties.Get<Trigger>(TriggerRelation.Key);
set => Properties.Set(TriggerRelation.Key, value);
}

protected virtual IList<AlarmOccurrence> Occurrences { get; set; }
Expand Down Expand Up @@ -96,7 +96,7 @@ public virtual IList<AlarmOccurrence> 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)
{
Expand Down
10 changes: 7 additions & 3 deletions net-core/Ical.Net/Ical.Net/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 24 additions & 17 deletions net-core/Ical.Net/Ical.Net/DataTypes/Trigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ public class Trigger : EncodableDataType
{
private IDateTime _mDateTime;
private TimeSpan? _mDuration;
private TriggerRelation _mRelated = TriggerRelation.Start;
private string _mRelated = TriggerRelation.Start;

public virtual IDateTime DateTime
{
get => _mDateTime;
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;
}
}

Expand All @@ -52,7 +54,7 @@ public virtual TimeSpan? Duration
}
}

public virtual TriggerRelation Related
public virtual string Related
{
get => _mRelated;
set => _mRelated = value;
Expand All @@ -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()
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion net-core/Ical.Net/Ical.Net/Serialization/DataTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISerializerFactory>();
if (factory == null)
{
var factory = GetService<ISerializerFactory>();
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
{
Expand All @@ -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<ISerializerFactory>();
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<ISerializerFactory>();
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;
}
}
}

0 comments on commit bc91b0d

Please sign in to comment.