Skip to content

Commit c369d06

Browse files
committed
refactoring, code documentation, clean up
1 parent 268ae2e commit c369d06

27 files changed

Lines changed: 1024 additions & 40 deletions

samples/PolylineAlgorithm.NetTopologySuite.Sample/NetTopologyPolylineDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Buffers;
77

88
internal class NetTopologyPolylineDecoder : PolylineDecoder<Point, string> {
9-
public override PolylineEncodingOptions<Point> Options { get; } = PolylineEncodingOptions<Point>.Default;
9+
public override PolylineEncodingOptions<Point> Options { get; } = new PolylineEncodingOptions<Point>();
1010

1111
protected override Point CreateCoordinate(double latitude, double longitude) {
1212
return new Point(latitude, longitude);

samples/PolylineAlgorithm.NetTopologySuite.Sample/NetTopologyPolylineEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Text;
77

88
internal class NetTopologyPolylineEncoder : PolylineEncoder<Point, string> {
9-
public override PolylineEncodingOptions<Point> Options { get; } = PolylineEncodingOptions<Point>.Default;
9+
public override PolylineEncodingOptions<Point> Options { get; } = new PolylineEncodingOptions<Point>();
1010

1111
protected override string CreatePolyline(ReadOnlySequence<char> sequence) {
1212
if (sequence.IsEmpty) {

src/PolylineAlgorithm.Abstraction/InvalidPolylineException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private InvalidPolylineException(string message)
3434
/// <exception cref="InvalidPolylineException">
3535
/// Always thrown to indicate that the polyline is malformed at the specified position.
3636
/// </exception>
37-
internal static void Throw(long position) {
37+
public static void Throw(long position) {
3838
throw new InvalidPolylineException(string.Format(ExceptionMessageResource.PolylineStringIsMalformedMessage, position.ToString()));
3939
}
4040
}

src/PolylineAlgorithm.Abstraction/PolylineDecoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ public IEnumerable<TCoordinate> Decode(TPolyline polyline) {
6060
position = 0;
6161
sequence = enumerator.Current;
6262

63-
while (PolylineEncoding.Default.TryReadValue(ref latitude, ref sequence, ref position)
64-
&& PolylineEncoding.Default.TryReadValue(ref longitude, ref sequence, ref position)
63+
while (PolylineEncoding.TryReadValue(ref latitude, ref sequence, ref position)
64+
&& PolylineEncoding.TryReadValue(ref longitude, ref sequence, ref position)
6565
) {
66-
coordinate = (PolylineEncoding.Default.Denormalize(latitude), PolylineEncoding.Default.Denormalize(longitude));
66+
coordinate = (PolylineEncoding.Denormalize(latitude), PolylineEncoding.Denormalize(longitude));
6767

6868
if(!Options.Validator.IsValidLatitude(coordinate.Latitude) && !Options.Validator.IsValidLongitude(coordinate.Longitude)) {
6969
throw new Exception();
7070
}
7171

72-
yield return CreateCoordinate(PolylineEncoding.Default.Denormalize(latitude), PolylineEncoding.Default.Denormalize(longitude));
72+
yield return CreateCoordinate(PolylineEncoding.Denormalize(latitude), PolylineEncoding.Denormalize(longitude));
7373
}
7474

7575
consumed += position;

src/PolylineAlgorithm.Abstraction/PolylineEncoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ public TPolyline Encode(IEnumerable<TCoordinate> coordinates) {
6666

6767
while (enumerator.MoveNext()) {
6868
variance
69-
.Next(PolylineEncoding.Default.Normalize(GetLatitude(enumerator.Current)), PolylineEncoding.Default.Normalize(GetLongitude(enumerator.Current)));
69+
.Next(PolylineEncoding.Normalize(GetLatitude(enumerator.Current)), PolylineEncoding.Normalize(GetLongitude(enumerator.Current)));
7070

7171
if (GetRemainingBufferSize(position, buffer.Length) < GetRequiredLength(variance)) {
7272
throw new InternalBufferOverflowException();
7373
}
7474

75-
if (!PolylineEncoding.Default.TryWriteValue(variance.Latitude, ref buffer, ref position)
76-
|| !PolylineEncoding.Default.TryWriteValue(variance.Longitude, ref buffer, ref position)
75+
if (!PolylineEncoding.TryWriteValue(variance.Latitude, ref buffer, ref position)
76+
|| !PolylineEncoding.TryWriteValue(variance.Longitude, ref buffer, ref position)
7777
) {
7878
throw new InvalidOperationException();
7979
}
@@ -91,7 +91,7 @@ public TPolyline Encode(IEnumerable<TCoordinate> coordinates) {
9191

9292
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9393
static int GetRequiredLength(CoordinateVariance variance) =>
94-
PolylineEncoding.Default.GetCharCount(variance.Latitude) + PolylineEncoding.Default.GetCharCount(variance.Longitude);
94+
PolylineEncoding.GetCharCount(variance.Latitude) + PolylineEncoding.GetCharCount(variance.Longitude);
9595

9696
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9797
static int GetRemainingBufferSize(int position, int length) => length - position;

src/PolylineAlgorithm.Abstraction/PolylineEncoding.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ namespace PolylineAlgorithm.Abstraction;
1313
/// Provides methods for encoding and decoding geographic coordinates to and from polyline strings.
1414
/// Supports normalization, denormalization, and efficient value encoding/decoding for polyline algorithms.
1515
/// </summary>
16-
public class PolylineEncoding {
17-
/// <summary>
18-
/// Gets the default singleton instance of the <see cref="PolylineEncoding"/> class.
19-
/// </summary>
20-
public static readonly PolylineEncoding Default = new();
21-
16+
public static class PolylineEncoding {
2217
/// <summary>
2318
/// Attempts to read an encoded value from the specified buffer and update the provided variance.
2419
/// </summary>
@@ -35,7 +30,7 @@ public class PolylineEncoding {
3530
/// <see langword="true"/> if a value was successfully read; otherwise, <see langword="false"/>.
3631
/// </returns>
3732
[MethodImpl(MethodImplOptions.AggressiveInlining)]
38-
public bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffer, ref int position) {
33+
public static bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffer, ref int position) {
3934
if (position == buffer.Length) {
4035
return false;
4136
}
@@ -70,7 +65,7 @@ public bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffer, ref
7065
/// The denormalized double value.
7166
/// </returns>
7267
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73-
public double Denormalize(int value) => Math.Truncate((double)value) / Defaults.Algorithm.Precision;
68+
public static double Denormalize(int value) => Math.Truncate((double)value) / Defaults.Algorithm.Precision;
7469

7570
/// <summary>
7671
/// Attempts to write an encoded value to the specified buffer.
@@ -88,7 +83,7 @@ public bool TryReadValue(ref int variance, ref ReadOnlyMemory<char> buffer, ref
8883
/// <see langword="true"/> if the value was successfully written; otherwise, <see langword="false"/>.
8984
/// </returns>
9085
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91-
public bool TryWriteValue(int variance, ref Span<char> buffer, ref int position) {
86+
public static bool TryWriteValue(int variance, ref Span<char> buffer, ref int position) {
9287
if (buffer.Length < position + GetCharCount(variance)) {
9388
return false;
9489
}
@@ -119,7 +114,7 @@ public bool TryWriteValue(int variance, ref Span<char> buffer, ref int position)
119114
/// The normalized integer value.
120115
/// </returns>
121116
[MethodImpl(MethodImplOptions.AggressiveInlining)]
122-
public int Normalize(double value) => (int)Math.Round(value * Defaults.Algorithm.Precision);
117+
public static int Normalize(double value) => (int)Math.Round(value * Defaults.Algorithm.Precision);
123118

124119
/// <summary>
125120
/// Calculates the number of characters required to encode a given variance value.
@@ -131,7 +126,7 @@ public bool TryWriteValue(int variance, ref Span<char> buffer, ref int position)
131126
/// The number of characters required to encode the variance.
132127
/// </returns>
133128
[MethodImpl(MethodImplOptions.AggressiveInlining)]
134-
public int GetCharCount(int variance) => variance switch {
129+
public static int GetCharCount(int variance) => variance switch {
135130
// DO NOT CHANGE THE ORDER. We are skipping inside exclusive ranges as those are covered by previous statements.
136131
>= -16 and <= +15 => 1,
137132
>= -512 and <= +511 => 2,

src/PolylineAlgorithm.Abstraction/PolylineEncodingOptions.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,30 @@ namespace PolylineAlgorithm.Abstraction;
1212
/// <summary>
1313
/// Options for configuring polyline encoding.
1414
/// </summary>
15+
/// <typeparam name="TCoordinate">The type representing a coordinate.</typeparam>
1516
public class PolylineEncodingOptions<TCoordinate> {
16-
public static PolylineEncodingOptions<TCoordinate> Default => new();
17-
1817
/// <summary>
1918
/// Gets the maximum buffer size for encoding operations.
2019
/// </summary>
2120
public int MaxBufferSize { get; } = 64_000;
2221

23-
public CoordinateValidator<TCoordinate> Validator { get; } = new NullValidator<TCoordinate>();
22+
/// <summary>
23+
/// Gets the validator used to validate coordinates, latitude, and longitude values.
24+
/// </summary>
25+
public Validator<TCoordinate> Validator { get; } = new NullValidator<TCoordinate>();
2426

27+
/// <summary>
28+
/// Gets the maximum number of characters that can be used in the encoding buffer.
29+
/// </summary>
30+
/// <returns>The maximum character count based on the buffer size.</returns>
2531
internal int GetMaxCharCount() => MaxBufferSize / sizeof(char);
2632

33+
/// <summary>
34+
/// Validates the encoding options to ensure they are within acceptable limits.
35+
/// </summary>
36+
/// <exception cref="ArgumentException">
37+
/// Thrown if <see cref="MaxBufferSize"/> is less than the minimum required buffer size.
38+
/// </exception>
2739
internal void Validate() {
2840
if (MaxBufferSize < Defaults.Polyline.MaxEncodedCoordinateLength * sizeof(char)) {
2941
throw new ArgumentException();

src/PolylineAlgorithm.Abstraction/Validation/Abstraction/CoordinateValidator.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright © Pete Sramek. All rights reserved.
3+
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace PolylineAlgorithm.Abstraction.Validation.Abstraction;
7+
8+
/// <summary>
9+
/// Provides an abstract base class for validating coordinates and their latitude/longitude components.
10+
/// </summary>
11+
/// <typeparam name="TCoordinate">The type representing a coordinate.</typeparam>
12+
public abstract class Validator<TCoordinate> {
13+
/// <summary>
14+
/// Determines whether the specified coordinate is valid.
15+
/// </summary>
16+
/// <param name="coordinate">The coordinate to validate.</param>
17+
/// <returns><see langword="true"/> if the coordinate is valid; otherwise, <see langword="false"/>.</returns>
18+
public abstract bool IsValid(TCoordinate coordinate);
19+
20+
/// <summary>
21+
/// Determines whether the specified latitude value is valid.
22+
/// </summary>
23+
/// <param name="latitude">The latitude value to validate.</param>
24+
/// <returns><see langword="true"/> if the latitude is valid; otherwise, <see langword="false"/>.</returns>
25+
public abstract bool IsValidLatitude(double latitude);
26+
27+
/// <summary>
28+
/// Determines whether the specified longitude value is valid.
29+
/// </summary>
30+
/// <param name="longitude">The longitude value to validate.</param>
31+
/// <returns><see langword="true"/> if the longitude is valid; otherwise, <see langword="false"/>.</returns>
32+
public abstract bool IsValidLongitude(double longitude);
33+
}

src/PolylineAlgorithm.Abstraction/Validation/NullValidator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
using PolylineAlgorithm.Abstraction.Validation.Abstraction;
44

5-
public sealed class NullValidator<TCoordinate> : CoordinateValidator<TCoordinate> {
5+
6+
/// <summary>
7+
/// A validator that always returns <see langword="true"/> for any coordinate, latitude, or longitude.
8+
/// </summary>
9+
/// <typeparam name="TCoordinate">The type representing a coordinate.</typeparam>
10+
public sealed class NullValidator<TCoordinate> : Validator<TCoordinate> {
11+
/// <inheritdoc/>
612
public override bool IsValid(TCoordinate coordinate) {
713
return true;
814
}
915

16+
/// <inheritdoc/>
1017
public override bool IsValidLatitude(double latitude) {
1118
return true;
1219
}
1320

21+
/// <inheritdoc/>
1422
public override bool IsValidLongitude(double longitude) {
1523
return true;
1624
}

0 commit comments

Comments
 (0)