Skip to content

Commit f7f68cb

Browse files
committed
extracted validation to local functions
1 parent 20b4693 commit f7f68cb

1 file changed

Lines changed: 42 additions & 26 deletions

File tree

src/PolylineAlgorithm.Abstraction/AbstractPolylineEncoder.cs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,13 @@ public TPolyline Encode(IEnumerable<TCoordinate> coordinates) {
7171

7272
Debug.Assert(coordinates is not null, "Coordinates cannot be null.");
7373

74-
if (coordinates is null) {
75-
logger
76-
.LogNullArgumentWarning(nameof(coordinates));
77-
logger
78-
.LogOperationFailedInfo(nameof(Encode));
79-
80-
throw new ArgumentNullException(nameof(coordinates));
81-
}
74+
ValidateNullCoordinates(coordinates, logger);
8275

8376
int count = GetCount(coordinates);
8477

8578
Debug.Assert(count >= 0, "Count must be non-negative.");
8679

87-
if (count < 1) {
88-
logger
89-
.LogEmptyArgumentWarning(nameof(coordinates));
90-
logger
91-
.LogOperationFailedInfo(nameof(Encode));
92-
93-
throw new ArgumentException(ExceptionMessageResource.ArgumentCannotBeEmptyEnumerationMessage, nameof(coordinates));
94-
}
80+
ValidateEmptyCoordinates(logger, count);
9581

9682
CoordinateVariance variance = new();
9783

@@ -105,17 +91,12 @@ public TPolyline Encode(IEnumerable<TCoordinate> coordinates) {
10591

10692
while (enumerator.MoveNext()) {
10793
variance
108-
.Next(PolylineEncoding.Normalize(GetLatitude(enumerator.Current), CoordinateValueType.Latitude), PolylineEncoding.Normalize(GetLongitude(enumerator.Current), CoordinateValueType.Longitude));
109-
110-
if (GetRemainingBufferSize(position, buffer.Length) < GetRequiredLength(variance)) {
111-
logger
112-
.LogInternalBufferOverflowWarning(position, buffer.Length, GetRequiredLength(variance));
113-
logger
114-
.LogOperationFailedInfo(nameof(Encode));
94+
.Next(
95+
PolylineEncoding.Normalize(GetLatitude(enumerator.Current), CoordinateValueType.Latitude),
96+
PolylineEncoding.Normalize(GetLongitude(enumerator.Current), CoordinateValueType.Longitude)
97+
);
11598

116-
117-
throw new InternalBufferOverflowException();
118-
}
99+
ValidateBuffer(logger, variance, position, buffer);
119100

120101
if (!PolylineEncoding.TryWriteValue(variance.Latitude, ref buffer, ref position)
121102
|| !PolylineEncoding.TryWriteValue(variance.Longitude, ref buffer, ref position)
@@ -175,6 +156,41 @@ int GetBufferLength(int count) {
175156

176157
return requestedBufferLength;
177158
}
159+
160+
static void ValidateNullCoordinates(IEnumerable<TCoordinate> coordinates, ILogger<AbstractPolylineDecoder<TPolyline, TCoordinate>> logger) {
161+
if (coordinates is null) {
162+
logger
163+
.LogNullArgumentWarning(nameof(coordinates));
164+
logger
165+
.LogOperationFailedInfo(nameof(Encode));
166+
167+
throw new ArgumentNullException(nameof(coordinates));
168+
}
169+
}
170+
171+
static void ValidateEmptyCoordinates(ILogger<AbstractPolylineDecoder<TPolyline, TCoordinate>> logger, int count) {
172+
if (count < 1) {
173+
logger
174+
.LogEmptyArgumentWarning(nameof(coordinates));
175+
logger
176+
.LogOperationFailedInfo(nameof(Encode));
177+
178+
throw new ArgumentException(ExceptionMessageResource.ArgumentCannotBeEmptyEnumerationMessage, nameof(coordinates));
179+
}
180+
}
181+
182+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
183+
static void ValidateBuffer(ILogger<AbstractPolylineDecoder<TPolyline, TCoordinate>> logger, CoordinateVariance variance, int position, Span<char> buffer) {
184+
if (GetRemainingBufferSize(position, buffer.Length) < GetRequiredLength(variance)) {
185+
logger
186+
.LogInternalBufferOverflowWarning(position, buffer.Length, GetRequiredLength(variance));
187+
logger
188+
.LogOperationFailedInfo(nameof(Encode));
189+
190+
191+
throw new InternalBufferOverflowException();
192+
}
193+
}
178194
}
179195

180196
/// <summary>

0 commit comments

Comments
 (0)