sharee.bike-App/TINKLib/Model/Settings/PollingParameters.cs
2023-04-19 12:14:14 +02:00

101 lines
3.3 KiB
C#

using System;
namespace TINK.Settings
{
/// <summary> Holds polling parameters.</summary>
public sealed class PollingParameters : IEquatable<PollingParameters>
{
/// <summary> Holds default polling parameters. </summary>
public static PollingParameters Default { get; } = new PollingParameters(
new TimeSpan(0, 0, 0, 60 /*secs*/, 0), // Default polling interval. Was 10 secs up to 3.0.357.
true);
/// <summary> Holds polling parameters which represent polling off (empty polling object). </summary>
public static PollingParameters NoPolling { get; } = new PollingParameters(
TimeSpan.MaxValue,// Very long intervall which should never be used because polling IsActivated property is set to false.
false);
/// <summary> Constructs a polling parameter object. </summary>
/// <param name="period">Polling period.</param>
/// <param name="activated">True if polling is activated.</param>
public PollingParameters(TimeSpan period, bool activated)
{
Periode = period; // Can not be null because is a struct.
IsActivated = activated;
}
/// <summary>Holds the polling period.</summary>
public TimeSpan Periode { get; }
/// <summary> Holds value whether polling is activated or not.</summary>
public bool IsActivated { get; }
/// <summary> Checks equallity.</summary>
/// <param name="other">Object to compare with.</param>
/// <returns>True if objects are equal.</returns>
public bool Equals(PollingParameters other)
{
return this == other;
}
/// <summary> Checks equallity.</summary>
/// <param name="obj">Object to compare with.</param>
/// <returns>True if objects are equal.</returns>
public override bool Equals(object obj)
{
var l_oParameters = obj as PollingParameters;
if (l_oParameters == null)
{
return false;
}
return this == l_oParameters;
}
/// <summary> Gets the has code of object.</summary>
/// <returns></returns>
public override int GetHashCode()
{
return Periode.GetHashCode() ^ IsActivated.GetHashCode();
}
/// <summary> Gets the string representation of the object.</summary>
/// <returns></returns>
public override string ToString()
{
return $"Polling is on={IsActivated}, polling interval={Periode.TotalSeconds}[sec].";
}
/// <summary>Defines equality of thwo polling parameter objects.</summary>
/// <param name="p_oSource">First object to compare.</param>
/// <param name="p_oTarget">Second object to compare.</param>
/// <returns>True if objects are equal</returns>
public static bool operator ==(PollingParameters p_oSource, PollingParameters p_oTarget)
{
if (p_oSource is null && p_oTarget is null)
{
// Both object are null
return true;
}
if (p_oSource is null ^ p_oTarget is null)
{
// Only one object is null.
return false;
}
return p_oSource.Periode == p_oTarget.Periode
&& p_oSource.IsActivated == p_oTarget.IsActivated;
}
/// <summary>Defines equality of thwo polling parameter objects.</summary>
/// <param name="p_oSource">First object to compare.</param>
/// <param name="p_oTarget">Second object to compare.</param>
/// <returns>True if objects are equal</returns>
public static bool operator !=(PollingParameters p_oSource, PollingParameters p_oTarget)
{
return (p_oSource == p_oTarget) == false;
}
}
}