using System;
namespace TINK.Settings
{
/// Holds polling parameters.
public sealed class PollingParameters : IEquatable
{
/// Holds default polling parameters.
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);
/// Holds polling parameters which represent polling off (empty polling object).
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);
/// Constructs a polling parameter object.
/// Polling period.
/// True if polling is activated.
public PollingParameters(TimeSpan period, bool activated)
{
Periode = period; // Can not be null because is a struct.
IsActivated = activated;
}
/// Holds the polling period.
public TimeSpan Periode { get; }
/// Holds value whether polling is activated or not.
public bool IsActivated { get; }
/// Checks equallity.
/// Object to compare with.
/// True if objects are equal.
public bool Equals(PollingParameters other)
{
return this == other;
}
/// Checks equallity.
/// Object to compare with.
/// True if objects are equal.
public override bool Equals(object obj)
{
var l_oParameters = obj as PollingParameters;
if (l_oParameters == null)
{
return false;
}
return this == l_oParameters;
}
/// Gets the has code of object.
///
public override int GetHashCode()
{
return Periode.GetHashCode() ^ IsActivated.GetHashCode();
}
/// Gets the string representation of the object.
///
public override string ToString()
{
return $"Polling is on={IsActivated}, polling interval={Periode.TotalSeconds}[sec].";
}
/// Defines equality of thwo polling parameter objects.
/// First object to compare.
/// Second object to compare.
/// True if objects are equal
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;
}
/// Defines equality of thwo polling parameter objects.
/// First object to compare.
/// Second object to compare.
/// True if objects are equal
public static bool operator !=(PollingParameters p_oSource, PollingParameters p_oTarget)
{
return (p_oSource == p_oTarget) == false;
}
}
}