Files
TimetableDesigner/TimetableDesigner.Core/TimetableSpan.cs

86 lines
2.1 KiB
C#
Raw Normal View History

2023-03-12 12:32:26 +01:00
using System;
2023-05-07 17:39:24 +02:00
using System.Diagnostics;
2023-03-12 12:32:26 +01:00
namespace TimetableDesigner.Core
{
[Serializable]
2023-03-12 17:52:17 +01:00
public class TimetableSpan
2023-03-12 12:32:26 +01:00
{
2023-05-07 17:39:24 +02:00
#region FIELDS
private TimeOnly _from;
private TimeOnly _to;
#endregion
2023-03-12 12:32:26 +01:00
#region PROPERTIES
2023-05-07 17:39:24 +02:00
public TimeOnly From => _from;
public TimeOnly To => _to;
2023-03-12 12:32:26 +01:00
#endregion
#region CONSTRUCTORS
2023-03-12 17:52:17 +01:00
public TimetableSpan(TimeOnly from, TimeOnly to)
2023-03-12 12:32:26 +01:00
{
if (to <= from)
{
2023-05-07 17:39:24 +02:00
throw new ArgumentException("Ending value (\"to\") of TimetableSpan have to be greater than starting value (\"from\")");
2023-03-12 12:32:26 +01:00
}
2023-05-07 17:39:24 +02:00
_from = from;
_to = to;
2023-03-12 12:32:26 +01:00
}
#endregion
#region PUBLIC METHODS
2023-05-07 17:39:24 +02:00
public override bool Equals(object? obj) => obj is TimetableSpan slot && From == slot.From && To == slot.To;
public override int GetHashCode() => HashCode.Combine(From, To);
public override string? ToString() => $"{From} - {To}";
public TimetableSpanCollision CheckCollision(TimetableSpan slot)
2023-03-12 12:32:26 +01:00
{
if (slot.To <= this.From)
{
return TimetableSpanCollision.CheckedSlotBefore;
2023-03-12 12:32:26 +01:00
}
2023-05-07 17:39:24 +02:00
else if (this.To <= slot.From)
2023-03-12 12:32:26 +01:00
{
return TimetableSpanCollision.CheckedSlotAfter;
2023-03-12 12:32:26 +01:00
}
else
{
2023-05-07 17:39:24 +02:00
if (this.From <= slot.From && slot.To <= this.To)
2023-03-12 12:32:26 +01:00
{
return TimetableSpanCollision.CheckedSlotIn;
2023-03-12 12:32:26 +01:00
}
2023-05-07 17:39:24 +02:00
else if (this.From < slot.From && slot.From < this.To && this.To <= slot.To)
2023-03-12 12:32:26 +01:00
{
return TimetableSpanCollision.CheckedSlotFromIn;
2023-03-12 12:32:26 +01:00
}
2023-05-07 17:39:24 +02:00
else if (slot.From < this.From && this.From < slot.To && slot.To <= this.To)
2023-03-12 12:32:26 +01:00
{
return TimetableSpanCollision.CheckedSlotToIn;
2023-03-12 12:32:26 +01:00
}
else
{
throw new ArgumentException("Unknown collision");
}
}
}
#endregion
}
}