sharee.bike-App/SharedBusinessLogic/Model/Position.cs

65 lines
1.7 KiB
C#
Raw Permalink Normal View History

2022-01-22 18:16:10 +01:00
using System;
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Model
2022-01-22 18:16:10 +01:00
{
2022-09-06 16:08:19 +02:00
public class Position : IPosition
{
private const double PRECISSION_LATITUDE_LONGITUDE = 0.000000000000001;
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
internal Position(double latitude, double longitude)
{
if (!GetIsValid(latitude, longitude))
throw new ArgumentNullException();
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
Latitude = latitude;
Longitude = longitude;
}
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
public double Latitude { get; private set; }
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
public double Longitude { get; private set; }
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Compares position with a target position.
/// </summary>
/// <param name="target">Target position to compare with.</param>
/// <returns>True if positions are equal.</returns>
public override bool Equals(object target)
{
if (!(target is IPosition targetPosition))
return false;
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
return GetEquals(this, targetPosition);
}
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
public bool Equals(IPosition other)
=> (other is Position position) && position.Equals(this as object);
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
public override int GetHashCode()
{
var hashCode = -1416534245;
hashCode = hashCode * -1521134295 + Latitude.GetHashCode();
hashCode = hashCode * -1521134295 + Longitude.GetHashCode();
return hashCode;
}
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
public bool IsValid => GetIsValid(Latitude, Longitude);
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
public static bool GetEquals(IPosition source, IPosition target)
{
if (source == null || target == null)
return source == null ^ target == null;
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
if (!source.IsValid || !target.IsValid)
return !source.IsValid ^ !target.IsValid;
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
return Math.Abs(source.Latitude - target.Latitude) < PRECISSION_LATITUDE_LONGITUDE
&& Math.Abs(source.Longitude - target.Longitude) < PRECISSION_LATITUDE_LONGITUDE;
}
2022-01-22 18:16:10 +01:00
2022-09-06 16:08:19 +02:00
public static bool GetIsValid(double latitude, double longitude)
=> !double.IsNaN(latitude) && !double.IsNaN(longitude);
}
2022-01-22 18:16:10 +01:00
}