Skip to content

Commit 8e0f3df

Browse files
authored
Merge pull request #68 from maherkassim/optimize_expanded_comparison
Added optimization for comparisons involving lowest- and highest-expanded postal codes
2 parents 0ca066d + d26d07c commit 8e0f3df

2 files changed

Lines changed: 95 additions & 4 deletions

File tree

src/PostalCodes/PostalCode.cs

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ protected internal string PostalCodeString
3939
get { return _backingPostalCode; }
4040
}
4141

42+
/// <summary>
43+
/// The lowest-expanded postal code (if the postal code is short)
44+
/// </summary>
45+
private readonly string _lowestExpandedPostalCode;
46+
47+
/// <summary>
48+
/// Gets the lowest-expanded postal code string.
49+
/// </summary>
50+
/// <value>The lowest-expanded postal code string.</value>
51+
protected internal string LowestExpandedPostalCodeString
52+
{
53+
get { return _lowestExpandedPostalCode; }
54+
}
55+
56+
/// <summary>
57+
/// The highest-expanded postal code (if the postal code is short)
58+
/// </summary>
59+
private readonly string _highestExpandedPostalCode;
60+
61+
/// <summary>
62+
/// Gets the highest-expanded postal code string.
63+
/// </summary>
64+
/// <value>The highest-expanded postal code string.</value>
65+
protected internal string HighestExpandedPostalCodeString
66+
{
67+
get { return _highestExpandedPostalCode; }
68+
}
69+
4270
/// <summary>
4371
/// The current format.
4472
/// </summary>
@@ -96,6 +124,9 @@ internal PostalCode(PostalCodeFormat[] formats, string redundantCharacters, stri
96124
}
97125

98126
_backingPostalCode = String.Intern(Normalize(nonWhiteSpaceCode));
127+
128+
_lowestExpandedPostalCode = ExpandLowest();
129+
_highestExpandedPostalCode = ExpandHighest();
99130
}
100131

101132
/// <summary>
@@ -158,6 +189,16 @@ public virtual int CompareTo(PostalCode other)
158189
return other == null ? 1 : PostalCodeStringComparer.Default.Compare(PostalCodeString, other.PostalCodeString);
159190
}
160191

192+
/// <summary>
193+
/// Compares the current object with another object of the same type.
194+
/// </summary>
195+
/// <param name="other">An object to compare with this object.</param>
196+
/// <returns>A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other" /> parameter.Zero This object is equal to <paramref name="other" />. Greater than zero This object is greater than <paramref name="other" />.</returns>
197+
public virtual int CompareTo(string other)
198+
{
199+
return other == null ? 1 : PostalCodeStringComparer.Default.Compare(PostalCodeString, other);
200+
}
201+
161202
#endregion
162203

163204
#region Implementation of IEquatable<PostalCode>
@@ -280,13 +321,43 @@ public int CompareTo(object obj)
280321
return left.CompareTo(right) <= 0;
281322
}
282323

324+
/// <summary>
325+
/// Implements the &lt;=.
326+
/// </summary>
327+
/// <param name="left">The left (as postal code).</param>
328+
/// <param name="right">The right (as string).</param>
329+
/// <returns>The result of the operator.</returns>
330+
public static bool operator <=(PostalCode left, string right)
331+
{
332+
if (left == null)
333+
{
334+
return true;
335+
}
336+
return left.CompareTo(right) <= 0;
337+
}
338+
339+
/// <summary>
340+
/// Implements the &gt;=.
341+
/// </summary>
342+
/// <param name="left">The left (as postal code).</param>
343+
/// <param name="right">The right (as string).</param>
344+
/// <returns>The result of the operator.</returns>
345+
public static bool operator >=(PostalCode left, PostalCode right)
346+
{
347+
if (left == null)
348+
{
349+
return right == null;
350+
}
351+
return left.CompareTo(right) >= 0;
352+
}
353+
283354
/// <summary>
284355
/// Implements the &gt;=.
285356
/// </summary>
286357
/// <param name="left">The left.</param>
287358
/// <param name="right">The right.</param>
288359
/// <returns>The result of the operator.</returns>
289-
public static bool operator >=(PostalCode left, PostalCode right)
360+
public static bool operator >=(PostalCode left, string right)
290361
{
291362
if (left == null)
292363
{
@@ -324,6 +395,26 @@ public virtual string ToHumanReadableString()
324395
return ToHumanReadableString(outputFormat);
325396
}
326397

398+
private string ExpandLowest()
399+
{
400+
if (_currentFormatType == FormatType.Short && _currentFormat.ShortExpansionAsLowestInRange != null)
401+
{
402+
return _backingPostalCode + _currentFormat.ShortExpansionAsLowestInRange;
403+
}
404+
405+
return _backingPostalCode;
406+
}
407+
408+
private string ExpandHighest()
409+
{
410+
if (_currentFormatType == FormatType.Short && _currentFormat.ShortExpansionAsHighestInRange != null)
411+
{
412+
return _backingPostalCode + _currentFormat.ShortExpansionAsHighestInRange;
413+
}
414+
415+
return _backingPostalCode;
416+
}
417+
327418
/// <summary>
328419
/// Expands the postal code as lowest in range.
329420
/// </summary>
@@ -342,7 +433,7 @@ public PostalCode ExpandPostalCodeAsLowestInRange()
342433
}
343434
}
344435

345-
return CreatePostalCode(ToString(), _allowConvertToShort);
436+
return this;
346437
}
347438

348439
/// <summary>
@@ -363,7 +454,7 @@ public PostalCode ExpandPostalCodeAsHighestInRange()
363454
}
364455
}
365456

366-
return CreatePostalCode(ToString(), _allowConvertToShort);
457+
return this;
367458
}
368459

369460
/// <summary>

src/PostalCodes/PostalCodeRange.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public static bool Contains(PostalCodeRange range, PostalCode specificCode)
359359
}
360360

361361
return range.IsDefault ||
362-
((range.Start <= specificCode.ExpandPostalCodeAsLowestInRange()) && (specificCode.ExpandPostalCodeAsHighestInRange() <= range.End));
362+
((range.Start <= specificCode.LowestExpandedPostalCodeString) && (range.End >= specificCode.HighestExpandedPostalCodeString));
363363
}
364364

365365
/// <summary>

0 commit comments

Comments
 (0)