Skip to content

Commit 2908ba5

Browse files
committed
refact
1 parent 82008f5 commit 2908ba5

File tree

15 files changed

+56
-39
lines changed

15 files changed

+56
-39
lines changed

samples/PolylineAlgorithm.NetTopologySuite.Sample/NetTopologyPolylineDecoder.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ namespace PolylineAlgorithm.NetTopologySuite.Sample;
1313
/// Represents a polyline decoder that converts encoded polyline strings into a collection of geographic coordinates using NetTopologySuite.
1414
/// </summary>
1515
public sealed class NetTopologyPolylineDecoder : AbstractPolylineDecoder<string, Point> {
16+
/// <summary>
17+
/// Creates a coordinate instance from the given latitude and longitude values.
18+
/// </summary>
19+
/// <param name="latitude">
20+
/// The latitude value.
21+
/// </param>
22+
/// <param name="longitude">
23+
/// The longitude value.
24+
/// </param>
25+
/// <returns>
26+
/// A coordinate instance of type <typeparamref name="TCoordinate"/>.
27+
/// </returns>
1628
protected override Point CreateCoordinate(double latitude, double longitude) {
1729
return new Point(latitude, longitude);
1830
}
@@ -30,10 +42,6 @@ protected override Point CreateCoordinate(double latitude, double longitude) {
3042
/// Thrown when the provided polyline string is null, empty, or consists only of whitespace characters.
3143
/// </exception>
3244
protected override ReadOnlyMemory<char> GetReadOnlyMemory(string polyline) {
33-
if (string.IsNullOrWhiteSpace(polyline)) {
34-
throw new ArgumentException("Value cannot be null, empty or whitespace.", nameof(polyline));
35-
}
36-
3745
return polyline.AsMemory();
3846
}
3947
}

src/PolylineAlgorithm/Abstraction/AbstractPolylineDecoder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public IEnumerable<TCoordinate> Decode(TPolyline polyline) {
7171
logger.
7272
LogOperationStartedInfo(nameof(Decode));
7373

74-
ValidatePolyline(polyline, logger);
74+
ValidateNullPolyline(polyline, logger);
7575

7676
ReadOnlyMemory<char> sequence = GetReadOnlyMemory(polyline);
7777

78-
ValidateSequence(logger, sequence);
78+
ValidateEmptySequence(logger, sequence);
7979

8080
int position = 0;
8181
int latitude = 0;
@@ -105,7 +105,7 @@ public IEnumerable<TCoordinate> Decode(TPolyline polyline) {
105105
logger
106106
.LogOperationFinishedInfo(nameof(Decode));
107107

108-
static void ValidateSequence(ILogger<AbstractPolylineDecoder<TPolyline, TCoordinate>> logger, ReadOnlyMemory<char> sequence) {
108+
static void ValidateEmptySequence(ILogger<AbstractPolylineDecoder<TPolyline, TCoordinate>> logger, ReadOnlyMemory<char> sequence) {
109109
if (sequence.Length < Defaults.Polyline.Block.Length.Min) {
110110
logger
111111
.LogPolylineCannotBeShorterThanWarning(nameof(sequence), sequence.Length, Defaults.Polyline.Block.Length.Min);
@@ -116,7 +116,7 @@ static void ValidateSequence(ILogger<AbstractPolylineDecoder<TPolyline, TCoordin
116116
}
117117
}
118118

119-
static void ValidatePolyline(TPolyline polyline, ILogger<AbstractPolylineDecoder<TPolyline, TCoordinate>> logger) {
119+
static void ValidateNullPolyline(TPolyline polyline, ILogger<AbstractPolylineDecoder<TPolyline, TCoordinate>> logger) {
120120
if (polyline is null) {
121121
logger
122122
.LogNullArgumentWarning(nameof(polyline));
@@ -135,7 +135,7 @@ static void ValidatePolyline(TPolyline polyline, ILogger<AbstractPolylineDecoder
135135
/// <returns>
136136
/// A <see cref="ReadOnlyMemory{T}"/> representing the encoded polyline data.
137137
/// </returns>
138-
protected abstract ReadOnlyMemory<char> GetReadOnlyMemory(TPolyline polyline);
138+
protected abstract ReadOnlyMemory<char> GetReadOnlyMemory([NotNull]TPolyline polyline);
139139

140140
/// <summary>
141141
/// Creates a coordinate instance from the given latitude and longitude values.

src/PolylineAlgorithm/Abstraction/AbstractPolylineEncoder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace PolylineAlgorithm.Abstraction;
1414
using System.Collections;
1515
using System.Collections.Generic;
1616
using System.Diagnostics;
17-
using System.Runtime.CompilerServices;
1817

1918
/// <summary>
2019
/// Provides functionality to encode a collection of geographic coordinates into an encoded polyline string.
@@ -108,7 +107,7 @@ public TPolyline Encode(IEnumerable<TCoordinate> coordinates) {
108107
logger
109108
.LogOperationFailedInfo(nameof(Encode));
110109

111-
throw new InvalidOperationException();
110+
throw new InvalidOperationException(ExceptionMessageResource.CouldNotWriteEncodedValueToTheBuffer);
112111
}
113112

114113
consumed++;
@@ -196,7 +195,7 @@ static void ValidateBuffer(ILogger<AbstractPolylineDecoder<TPolyline, TCoordinat
196195
/// <returns>
197196
/// An instance of <typeparamref name="TPolyline"/> representing the encoded polyline.
198197
/// </returns>
199-
198+
200199
protected abstract TPolyline CreatePolyline(ReadOnlyMemory<char> polyline);
201200

202201
/// <summary>
@@ -206,7 +205,7 @@ static void ValidateBuffer(ILogger<AbstractPolylineDecoder<TPolyline, TCoordinat
206205
/// <returns>
207206
/// The longitude value as a <see cref="double"/>.
208207
/// </returns>
209-
208+
210209
protected abstract double GetLongitude(TCoordinate current);
211210

212211
/// <summary>
@@ -216,7 +215,7 @@ static void ValidateBuffer(ILogger<AbstractPolylineDecoder<TPolyline, TCoordinat
216215
/// <returns>
217216
/// The latitude value as a <see cref="double"/>.
218217
/// </returns>
219-
218+
220219
protected abstract double GetLatitude(TCoordinate current);
221220
}
222221

src/PolylineAlgorithm/Internal/CoordinateVariance.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace PolylineAlgorithm.Internal;
77

88
using System;
99
using System.Diagnostics;
10-
using System.Runtime.CompilerServices;
1110
using System.Runtime.InteropServices;
1211

1312
/// <summary>
@@ -23,7 +22,7 @@ internal struct CoordinateVariance {
2322
/// Initializes a new instance of the <see cref="CoordinateVariance"/> struct with the default latitude and longitude deltas.
2423
/// </summary>
2524
public CoordinateVariance() {
26-
_current = (0, 0);
25+
_current =(default, default);
2726
}
2827

2928
/// <summary>
@@ -37,16 +36,17 @@ public CoordinateVariance() {
3736
public int Longitude { get; private set; }
3837

3938
/// <summary>
40-
/// Updates the variance values based on the next latitude and longitude, and sets the current coordinate.
39+
/// Updates the variance values based on the next latitude and longitude, and sets the current coordinate as next variance baseline.
4140
/// </summary>
4241
/// <param name="latitude">The next latitude value.</param>
4342
/// <param name="longitude">The next longitude value.</param>
44-
43+
4544
public void Next(int latitude, int longitude) {
4645
Latitude = Variance(_current.Latitude, latitude);
4746
Longitude = Variance(_current.Longitude, longitude);
4847

49-
_current = (latitude, longitude);
48+
_current.Latitude = latitude;
49+
_current.Longitude = longitude;
5050
}
5151

5252
/// <summary>
@@ -58,7 +58,7 @@ public void Next(int latitude, int longitude) {
5858
/// <param name="initial">The previous coordinate value.</param>
5959
/// <param name="next">The next coordinate value.</param>
6060
/// <returns>The computed variance between <paramref name="initial"/> and <paramref name="next"/>.</returns>
61-
61+
6262
private static int Variance(int initial, int next) => (initial, next) switch {
6363
(0, 0) => 0,
6464
(0, _) => next,

src/PolylineAlgorithm/Internal/Defaults.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ namespace PolylineAlgorithm.Internal;
1313
/// </summary>
1414
[ExcludeFromCodeCoverage]
1515
internal static class Defaults {
16+
/// <summary>
17+
/// Contains default values and constants specific to the polyline encoding logging.
18+
/// </summary>
19+
internal static class Logging {
20+
/// <summary>
21+
/// Log level multiplier used to distinguish event identification for each log level
22+
/// </summary>
23+
internal const int LogLevelMultiplier = 100;
24+
}
1625
/// <summary>
1726
/// Contains default values and constants specific to the polyline encoding algorithm.
1827
/// </summary>

src/PolylineAlgorithm/Internal/Logging/LogInfoExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PolylineAlgorithm.Internal.Logging;
99

1010
internal static partial class LogInfoExtensions {
1111
private const LogLevel LOG_LEVEL = LogLevel.Information;
12-
private const int EVENT_ID_BASE = (int)LOG_LEVEL * LoggingDefaults.LogLevelMultiplier;
12+
private const int EVENT_ID_BASE = (int)LOG_LEVEL * Defaults.Logging.LogLevelMultiplier;
1313

1414
[LoggerMessage(EVENT_ID_BASE + 1, LOG_LEVEL, "Operation {operationName} has started.")]
1515
internal static partial void LogOperationStartedInfo(this ILogger logger, string operationName);

src/PolylineAlgorithm/Internal/Logging/LogWarningExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PolylineAlgorithm.Internal.Logging;
99

1010
internal static partial class LogWarningExtensions {
1111
private const LogLevel LOG_LEVEL = LogLevel.Warning;
12-
private const int EVENT_ID_BASE = (int)LOG_LEVEL * LoggingDefaults.LogLevelMultiplier;
12+
private const int EVENT_ID_BASE = (int)LOG_LEVEL * Defaults.Logging.LogLevelMultiplier;
1313

1414
[LoggerMessage(EVENT_ID_BASE + 1, LOG_LEVEL, "Argument {argumentName} is null.")]
1515
internal static partial void LogNullArgumentWarning(this ILogger logger, string argumentName);

src/PolylineAlgorithm/Internal/Logging/LoggingDefaults.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/PolylineAlgorithm/PolylineDecoder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace PolylineAlgorithm;
77

88
using PolylineAlgorithm.Abstraction;
9-
using System.Runtime.CompilerServices;
109

1110
/// <inheritdoc cref="AbstractPolylineDecoder{TPolyline, TCoordinate}" />
1211
public sealed class PolylineDecoder : AbstractPolylineDecoder<Polyline, Coordinate> {

src/PolylineAlgorithm/PolylineEncoder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace PolylineAlgorithm;
77

88
using PolylineAlgorithm.Abstraction;
9-
using System.Runtime.CompilerServices;
109

1110
/// <inheritdoc cref="AbstractPolylineEncoder{TCoordinate, TPolyline}" />
1211
public sealed class PolylineEncoder : AbstractPolylineEncoder<Coordinate, Polyline> {

0 commit comments

Comments
 (0)