Skip to content

Commit 67727ad

Browse files
committed
refactored encoding options, updated tests and comments
1 parent 873dcee commit 67727ad

12 files changed

+41
-53
lines changed

src/PolylineAlgorithm/Abstraction/AbstractPolylineEncoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ int GetMaxBufferLength(int count) {
140140

141141
int requestedBufferLength = count * Defaults.Polyline.Block.Length.Max;
142142

143-
Debug.Assert(Options.MaxBufferLength > 0, "Max buffer length must be greater than zero.");
143+
Debug.Assert(Options.MaxPolylineLength > 0, "Max buffer length must be greater than zero.");
144144
Debug.Assert(requestedBufferLength > 0, "Requested buffer length must be greater than zero.");
145145

146-
if (requestedBufferLength > Options.MaxBufferLength) {
146+
if (requestedBufferLength > Options.MaxPolylineLength) {
147147
logger
148-
.LogRequestedBufferSizeExceedsMaxBufferLengthWarning(requestedBufferLength, Options.MaxBufferLength);
148+
.LogRequestedBufferSizeExceedsMaxBufferLengthWarning(requestedBufferLength, Options.MaxPolylineLength);
149149

150-
return Options.MaxBufferLength;
150+
return Options.MaxPolylineLength;
151151
}
152152

153153
return requestedBufferLength;

src/PolylineAlgorithm/PolylineEncodingOptions.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ namespace PolylineAlgorithm;
1818
[DebuggerDisplay("MaxBufferSize: {MaxBufferSize}, MaxBufferLength: {MaxBufferLength}, LoggerFactoryType: {LoggerFactory.GetType().Name}")]
1919
public sealed class PolylineEncodingOptions {
2020
/// <summary>
21-
/// Gets the maximum buffer size for encoding operations.
21+
/// Gets or sets the maximum size of the buffer used for encoding.
2222
/// </summary>
2323
/// <remarks>
24-
/// The default maximum buffer size is 64,000 bytes (64 KB). This can be adjusted based on the expected size of the polyline data, but should be enough for common cases.
24+
/// The default value is 1,024 characters.
2525
/// </remarks>
26-
public int MaxBufferSize { get; internal set; } = 64_000;
26+
public int MaxPolylineLength { get; internal set; } = 1_024;
2727

2828
/// <summary>
2929
/// Gets or sets the precision for encoding coordinates.
@@ -32,13 +32,4 @@ public sealed class PolylineEncodingOptions {
3232
/// The default logger factory is <see cref="NullLoggerFactory"/>, which does not log any messages.
3333
/// </remarks>
3434
public ILoggerFactory LoggerFactory { get; internal set; } = NullLoggerFactory.Instance;
35-
36-
37-
/// <summary>
38-
/// Gets the maximum length of the encoded polyline string.
39-
/// </summary>
40-
/// <remarks>
41-
/// The maximum length is calculated based on the buffer size divided by the size of a character.
42-
/// </remarks>
43-
internal int MaxBufferLength => MaxBufferSize / sizeof(char);
4435
}

src/PolylineAlgorithm/PolylineEncodingOptionsBuilder.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace PolylineAlgorithm;
1313
/// Provides a builder for configuring options for polyline encoding operations.
1414
/// </summary>
1515
public class PolylineEncodingOptionsBuilder {
16-
private int _bufferSize = 64_000;
16+
private int _maxPolylineLength = 1_024;
1717
private ILoggerFactory _loggerFactory = NullLoggerFactory.Instance;
1818

1919
private PolylineEncodingOptionsBuilder() { }
@@ -36,23 +36,25 @@ public static PolylineEncodingOptionsBuilder Create() {
3636
/// </returns>
3737
public PolylineEncodingOptions Build() {
3838
return new PolylineEncodingOptions {
39-
MaxBufferSize = _bufferSize,
39+
MaxPolylineLength = _maxPolylineLength,
4040
LoggerFactory = _loggerFactory
4141
};
4242
}
4343

4444
/// <summary>
45-
/// Sets the buffer size for encoding operations.
45+
/// Sets the maximum length of the polyline string that can be encoded.
4646
/// </summary>
47-
/// <param name="bufferSize">
48-
/// The maximum buffer size. Must be greater than 11.
47+
/// <param name="maxLength">
48+
/// The maximum length of the polyline string in characters.
4949
/// </param>
5050
/// <returns>
51-
/// The current builder instance.
51+
/// The current builder instance, allowing for method chaining.
5252
/// </returns>
53-
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="bufferSize"/> is less than or equal to 11.</exception>
54-
public PolylineEncodingOptionsBuilder WithMaxBufferSize(int bufferSize) {
55-
_bufferSize = bufferSize > 11 ? bufferSize : throw new ArgumentOutOfRangeException(nameof(bufferSize), string.Format(ExceptionMessageResource.BufferSizeMustBeGreaterThanMessageFormat, 11));
53+
/// <exception cref="ArgumentOutOfRangeException">
54+
/// Thrown when <paramref name="maxLength"/> is less than 1024 characters.
55+
/// </exception>
56+
public PolylineEncodingOptionsBuilder WithMaxPolylineLength(int maxLength) {
57+
_maxPolylineLength = maxLength >= 1024 ? maxLength : throw new ArgumentOutOfRangeException(nameof(maxLength), string.Format(ExceptionMessageResource.BufferSizeMustBeGreaterThanMessageFormat, 11));
5658

5759
return this;
5860
}
@@ -64,12 +66,12 @@ public PolylineEncodingOptionsBuilder WithMaxBufferSize(int bufferSize) {
6466
/// The instance of a logger factory.
6567
/// </param>
6668
/// <returns>
67-
/// The current builder instance.
69+
/// The current builder instance, allowing for method chaining.
6870
/// </returns>
6971
/// <exception cref="ArgumentNullException">
7072
/// Thrown when <paramref name="loggerFactory"/> is <see langword="null"/>.
7173
/// </exception>
72-
public PolylineEncodingOptionsBuilder WithLoggerFactory(ILoggerFactory loggerFactory) {
74+
public PolylineEncodingOptionsBuilder UseLoggerFactory(ILoggerFactory loggerFactory) {
7375
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
7476

7577
return this;

tests/PolylineAlgorithm.Tests/AbstractPolylineDecoderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace PolylineAlgorithm.Tests;
1515
public class AbstractPolylineDecoderTest {
1616
private static readonly PolylineDecoder _decoder = new();
1717

18-
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100], [1_000]];
18+
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100]];
1919

2020
public static IEnumerable<(double, double)> NotANumberAndInfinityCoordinates => StaticValueProvider.Invalid.GetNotANumberAndInfinityCoordinates();
2121

tests/PolylineAlgorithm.Tests/AbstractPolylineEncoderTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace PolylineAlgorithm.Tests;
1515
public class AbstractPolylineEncoderTest {
1616
private static readonly PolylineEncoder _encoder = new();
1717

18-
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100], [1_000]];
18+
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100]];
1919

2020
public static IEnumerable<(double, double)> NotANumberAndInfinityCoordinates => StaticValueProvider.Invalid.GetNotANumberAndInfinityCoordinates();
2121

@@ -88,7 +88,7 @@ public void Encode_EmptyCoordinates_Throws_ArgumentException() {
8888
[TestMethod]
8989
public void Encode_BufferTooSmall_Throws_InternalBufferOverflowException() {
9090
// Arrange
91-
PolylineEncoder _encoder = new(new PolylineEncodingOptions { MaxBufferSize = 12 });
91+
PolylineEncoder _encoder = new(new PolylineEncodingOptions { MaxPolylineLength = 12 });
9292
IEnumerable<(double Latitude, double Longitude)> coordinates = RandomValueProvider.GetCoordinates(2);
9393

9494
// Act

tests/PolylineAlgorithm.Tests/PolylineDecoderExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace PolylineAlgorithm.Tests;
1414
public class PolylineDecoderExtensionsTest {
1515
private readonly PolylineDecoder _decoder = new();
1616

17-
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100], [1_000]];
17+
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100]];
1818

1919
[TestMethod]
2020
public void Decode_Null_Decoder_Null_String_Throws_ArgumentNullException() {

tests/PolylineAlgorithm.Tests/PolylineDecoderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace PolylineAlgorithm.Tests;
1313
/// </summary>
1414
[TestClass]
1515
public class PolylineDecoderTest {
16-
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100], [1_000]];
16+
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100]];
1717

1818
/// <summary>
1919
/// The instance of the <see cref="PolylineDecoder"/> used for testing.

tests/PolylineAlgorithm.Tests/PolylineEncoderExtensionsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace PolylineAlgorithm.Tests;
1414
public class PolylineEncoderExtensionsTest {
1515
private readonly PolylineEncoder _encoder = new();
1616

17-
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100], [1_000]];
17+
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100]];
1818

1919
[TestMethod]
2020
public void Encode_Null_Encoder_Empty_List_Throws_ArgumentNullException() {

tests/PolylineAlgorithm.Tests/PolylineEncoderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace PolylineAlgorithm.Tests;
1212
/// </summary>
1313
[TestClass]
1414
public class PolylineEncoderTest {
15-
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100], [1_000]];
15+
public static IEnumerable<object[]> CoordinateCount => [[1], [10], [100]];
1616

1717
/// <summary>
1818
/// The instance of the <see cref="PolylineEncoder"/> used for testing.

tests/PolylineAlgorithm.Tests/PolylineEncodingOptionsTest.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,24 @@ public void Constructor_Parameterless_Ok() {
1717
var options = new PolylineEncodingOptions();
1818

1919
// Assert
20-
Assert.AreEqual(64_000, options.MaxBufferSize);
21-
Assert.AreEqual(64_000 / sizeof(char), options.MaxBufferLength);
20+
Assert.AreEqual(1_024, options.MaxPolylineLength);
2221
Assert.IsInstanceOfType<NullLoggerFactory>(options.LoggerFactory);
2322
}
2423

2524
[TestMethod]
26-
public void Constructor_ValidOptions_Ok() {
25+
public void Property_Initializer_Ok() {
2726
// Arrange
28-
var bufferSize = 32_000;
27+
var maxPolylineLength = 32_000;
2928
var loggerFactory = new FakeLoggerFactory(new FakeLoggerProvider());
3029

3130
// Act
3231
var options = new PolylineEncodingOptions() {
33-
MaxBufferSize = bufferSize,
32+
MaxPolylineLength = maxPolylineLength,
3433
LoggerFactory = loggerFactory
3534
};
3635

3736
// Assert
38-
Assert.AreEqual(bufferSize, options.MaxBufferSize);
39-
Assert.AreEqual(bufferSize / sizeof(char), options.MaxBufferLength);
37+
Assert.AreEqual(maxPolylineLength, options.MaxPolylineLength);
4038
Assert.IsInstanceOfType<FakeLoggerFactory>(options.LoggerFactory);
4139
}
4240
}

0 commit comments

Comments
 (0)