using System; namespace TINK.Model { public class Position : IPosition { private const double PRECISSION_LATITUDE_LONGITUDE = 0.000000000000001; internal Position(double latitude, double longitude) { if (!GetIsValid(latitude, longitude)) throw new ArgumentNullException(); Latitude = latitude; Longitude = longitude; } public double Latitude { get; private set; } public double Longitude { get; private set; } /// /// Compares position with a target position. /// /// Target position to compare with. /// True if positions are equal. public override bool Equals(object target) { if (!(target is IPosition targetPosition)) return false; return GetEquals(this, targetPosition); } public bool Equals(IPosition other) => (other is Position position) && position.Equals(this as object); public override int GetHashCode() { var hashCode = -1416534245; hashCode = hashCode * -1521134295 + Latitude.GetHashCode(); hashCode = hashCode * -1521134295 + Longitude.GetHashCode(); return hashCode; } public bool IsValid => GetIsValid(Latitude, Longitude); public static bool GetEquals(IPosition source, IPosition target) { if (source == null || target == null) return source == null ^ target == null; if (!source.IsValid || !target.IsValid) return !source.IsValid ^ !target.IsValid; return Math.Abs(source.Latitude - target.Latitude) < PRECISSION_LATITUDE_LONGITUDE && Math.Abs(source.Longitude - target.Longitude) < PRECISSION_LATITUDE_LONGITUDE; } public static bool GetIsValid(double latitude, double longitude) => !double.IsNaN(latitude) && !double.IsNaN(longitude); } }