Skip to content

Commit a4e627f

Browse files
author
Petr Sramek
committed
refactoring, cleanup
1 parent 83702af commit a4e627f

File tree

15 files changed

+88
-63
lines changed

15 files changed

+88
-63
lines changed

benchmarks/PolylineAlgorithm.Benchmarks/PolylineDecoderBenchmark.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ namespace PolylineAlgorithm.Benchmarks {
77
using BenchmarkDotNet.Attributes;
88
using BenchmarkDotNet.Engines;
99
using BenchmarkDotNet.Order;
10-
using PolylineAlgorithm.Internal;
1110

1211
[MemoryDiagnoser]
1312
[MarkdownExporter]
1413
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
1514
public class PolylineDecoderBenchmark {
1615
private readonly Consumer _consumer = new();
1716

18-
public PolylineDecoder Decoder { get; set; }
17+
public IPolylineDecoder Decoder { get; set; }
1918

2019
public static IEnumerable<string> GetPolylines() {
2120
yield return "mz}lHssngJj`gqSnx~lEcovfTnms{Zdy~qQj_deI";
@@ -25,7 +24,7 @@ public static IEnumerable<string> GetPolylines() {
2524

2625
[GlobalSetup]
2726
public void Setup() {
28-
Decoder = new PolylineDecoder();
27+
Decoder = new PolylineDecoder(new CoordinateValidator());
2928
}
3029

3130
[Benchmark]

benchmarks/PolylineAlgorithm.Benchmarks/PolylineEncoderBenchmark.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ namespace PolylineAlgorithm.Benchmarks {
77
using BenchmarkDotNet.Attributes;
88
using BenchmarkDotNet.Engines;
99
using BenchmarkDotNet.Order;
10-
using PolylineAlgorithm.Internal;
1110

1211
[MemoryDiagnoser]
1312
[MarkdownExporter]
1413
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
1514
public class PolylineEncoderBenchmark {
1615
private readonly Consumer _consumer = new();
1716

18-
public PolylineEncoder Encoder { get; set; }
17+
public IPolylineEncoder Encoder { get; set; }
1918

2019
public IEnumerable<IEnumerable<(double, double)>> GetCoordinates() {
2120
yield return new[] { (49.47383, 59.06250), (-58.37407, 25.31250), (52.99363, -120.93750), (-44.49024, -174.37500) };
@@ -25,7 +24,7 @@ public class PolylineEncoderBenchmark {
2524

2625
[GlobalSetup]
2726
public void Setup() {
28-
Encoder = new PolylineEncoder();
27+
Encoder = new PolylineEncoder(new CoordinateValidator());
2928
}
3029

3130
[Benchmark]

benchmarks/PolylineAlgorithm.Implementation.Benchmarks/PolylineDecoderBenchmark.cs.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace PolylineAlgorithm.Implementation.Benchmarks {
22
using BenchmarkDotNet.Attributes;
33
using BenchmarkDotNet.Engines;
4-
using PolylineAlgorithm.Internal;
4+
using PolylineAlgorithm;
55
using System;
66
using System.Runtime.CompilerServices;
77

@@ -10,6 +10,7 @@
1010
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.Declared)]
1111
public class PolylineDecoderBenchmark {
1212
private readonly Consumer _consumer = new();
13+
private readonly static CoordinateValidator _validator = new();
1314
public static IEnumerable<string> Polylines() {
1415
yield return "mz}lHssngJj`gqSnx~lEcovfTnms{Zdy~qQj_deI";
1516
yield return "}vwdGjafcRsvjKi}pxUhsrtCngtcAjjgzEdqvtLrscbKj}nr@wetlUc`nq]}_kfCyrfaK~wluUl`u}|@wa{lUmmuap@va{lU~oihCu||bF`|era@wsnnIjny{DxamaScqxza@dklDf{}kb@mtpeCavfzGqhx`Wyzzkm@jm`d@dba~Pppkg@h}pxU|rtnHp|flA|~xaPuykyN}fhv[h}pxUx~p}Ymx`sZih~iB{edwB";
@@ -71,7 +72,7 @@ private class Current {
7172
var coordinate = (GetCoordinate(latitude), GetCoordinate(longitude));
7273

7374
// Validating decoded coordinate. If not valid exception is thrown
74-
if (!CoordinateValidator.IsValid(coordinate)) {
75+
if (!_validator.IsValid(coordinate)) {
7576
throw new InvalidOperationException(String.Empty);
7677
}
7778

@@ -542,7 +543,7 @@ private class V6 {
542543
var coordinate = (GetCoordinate(latitude), GetCoordinate(longitude));
543544

544545
// Validating decoded coordinate. If not valid exception is thrown
545-
if (!CoordinateValidator.IsValid(coordinate)) {
546+
if (!_validator.IsValid(coordinate)) {
546547
throw new InvalidOperationException(String.Empty);
547548
}
548549

src/PolylineAlgorithm.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,11 @@ public static class ServiceCollectionExtensions {
1212
/// </summary>
1313
/// <param name="services">Instance of <seealso cref="IServiceCollection"/></param>
1414
/// <returns>nstance of <seealso cref="IServiceCollection"/></returns>
15-
public static IServiceCollection AddPolylineEncoder(this IServiceCollection services) {
16-
return services;
17-
//.AddSingleton<DefaultPolylineEncoding, DefaultPolylineEncoding>();
18-
}
19-
20-
/// <summary>
21-
/// Registers singleton instance of <seealso cref="TImplementation" /> to dependency container.
22-
/// </summary>
23-
/// <param name="services">Instance of <seealso cref="IServiceCollection"/></param>
24-
/// <returns>nstance of <seealso cref="IServiceCollection"/></returns>
25-
public static IServiceCollection AddPolylineEncoder<TService, TImplementation>(this IServiceCollection services)
26-
where TService : class
27-
where TImplementation : class, TService {
15+
public static IServiceCollection AddPolylineAlgorithm(this IServiceCollection services) {
2816
return services
29-
.AddSingleton<TService, TImplementation>();
17+
.AddSingleton<ICoordinateValidator, CoordinateValidator>()
18+
.AddSingleton<IPolylineEncoder, PolylineEncoder>()
19+
.AddSingleton<IPolylineDecoder, PolylineDecoder>();
3020
}
3121
}
3222
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
namespace PolylineAlgorithm.Internal {
1+
using PolylineAlgorithm.Internal;
2+
3+
namespace PolylineAlgorithm {
24
/// <summary>
35
/// Performs coordinate validation
46
/// </summary>
5-
internal class CoordinateValidator {
6-
#region Methods
7-
7+
public sealed class CoordinateValidator : ICoordinateValidator {
88
/// <summary>
99
/// Performs coordinate validation
1010
/// </summary>
1111
/// <param name="coordinate">Coordinate to validate</param>
1212
/// <returns>Returns validation result. If valid then true, otherwise false.</returns>
13-
public static bool IsValid((double Latitude, double Longitude) coordinate) {
14-
return IsValidLatitude(coordinate.Latitude) && IsValidLongitude(coordinate.Longitude);
13+
public bool IsValid((double Latitude, double Longitude) coordinate) {
14+
return IsValidLatitude(ref coordinate.Latitude) && IsValidLongitude(ref coordinate.Longitude);
1515
}
1616

17-
public static bool IsValidLatitude(double latitude) {
17+
private static bool IsValidLatitude(ref readonly double latitude) {
1818
return latitude >= Constants.Coordinate.MinLatitude && latitude <= Constants.Coordinate.MaxLatitude;
1919
}
2020

21-
public static bool IsValidLongitude(double longitude) {
21+
private static bool IsValidLongitude(ref readonly double longitude) {
2222
return longitude >= Constants.Coordinate.MinLongitude && longitude <= Constants.Coordinate.MaxLongitude;
2323
}
24-
25-
#endregion
2624
}
2725
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace PolylineAlgorithm;
2+
3+
public interface ICoordinateValidator {
4+
bool IsValid((double Latitude, double Longitude) coordinate);
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace PolylineAlgorithm;
2+
3+
using System.Collections.Generic;
4+
5+
public interface IPolylineDecoder {
6+
IEnumerable<(double Latitude, double Longitude)> Decode(string polyline);
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Copyright (c) Petr Šrámek. 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;
7+
8+
using System.Collections.Generic;
9+
10+
public interface IPolylineEncoder {
11+
string Encode(IEnumerable<(double Latitude, double Longitude)> coordinates);
12+
}

src/PolylineAlgorithm/Internal/PolylineDecoder.cs renamed to src/PolylineAlgorithm/PolylineDecoder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
namespace PolylineAlgorithm.Internal;
1+
namespace PolylineAlgorithm;
22

3-
using System;
4-
using System.Collections.Generic;
3+
using PolylineAlgorithm.Internal;
54

6-
public sealed class PolylineDecoder {
5+
public sealed class PolylineDecoder(ICoordinateValidator validator) : IPolylineDecoder {
6+
public ICoordinateValidator Validator { get; } = validator ?? throw new ArgumentNullException(nameof(validator));
77

88
/// <summary>
99
/// Method decodes polyline encoded representation to coordinates.
@@ -38,7 +38,7 @@ public sealed class PolylineDecoder {
3838
var coordinate = (GetCoordinate(latitude), GetCoordinate(longitude));
3939

4040
// Validating decoded coordinate. If not valid exception is thrown
41-
if (!CoordinateValidator.IsValid(coordinate)) {
41+
if (!Validator.IsValid(coordinate)) {
4242
throw new InvalidOperationException(string.Empty);
4343
}
4444

src/PolylineAlgorithm/Internal/PolylineEncoder.cs renamed to src/PolylineAlgorithm/PolylineEncoder.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
44
//
55

6-
namespace PolylineAlgorithm.Internal {
6+
using PolylineAlgorithm.Internal;
7+
8+
namespace PolylineAlgorithm {
79
using System;
810
using System.Collections.Generic;
911
using System.Linq;
1012

1113
/// <summary>
1214
/// Performs polyline algorithm decoding and encoding
1315
/// </summary>
14-
public sealed class PolylineEncoder {
16+
public sealed class PolylineEncoder(ICoordinateValidator validator) : IPolylineEncoder {
17+
public ICoordinateValidator Validator { get; } = validator ?? throw new ArgumentNullException(nameof(validator));
18+
1519
/// <summary>
1620
/// Method encodes coordinates to polyline encoded representation
1721
/// </summary>
@@ -55,9 +59,9 @@ public string Encode(IEnumerable<(double Latitude, double Longitude)> coordinate
5559
return buffer[..index].ToString();
5660
}
5761

58-
private static bool TryValidate(IEnumerable<(double Latitude, double Longitude)> collection, ref ICollection<CoordinateValidationException> exceptions) {
62+
private bool TryValidate(IEnumerable<(double Latitude, double Longitude)> collection, ref ICollection<CoordinateValidationException> exceptions) {
5963
foreach (var item in collection) {
60-
if (!CoordinateValidator.IsValid(item)) {
64+
if (!Validator.IsValid(item)) {
6165
exceptions.Add(new CoordinateValidationException(item.Latitude, item.Longitude));
6266
}
6367
}

0 commit comments

Comments
 (0)