Skip to content

Commit

Permalink
Merge pull request #68 from maherkassim/optimize_expanded_comparison
Browse files Browse the repository at this point in the history
Added optimization for comparisons involving lowest- and highest-expanded postal codes
  • Loading branch information
maherkassim authored Feb 5, 2018
2 parents 0ca066d + d26d07c commit 8e0f3df
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
97 changes: 94 additions & 3 deletions src/PostalCodes/PostalCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ protected internal string PostalCodeString
get { return _backingPostalCode; }
}

/// <summary>
/// The lowest-expanded postal code (if the postal code is short)
/// </summary>
private readonly string _lowestExpandedPostalCode;

/// <summary>
/// Gets the lowest-expanded postal code string.
/// </summary>
/// <value>The lowest-expanded postal code string.</value>
protected internal string LowestExpandedPostalCodeString
{
get { return _lowestExpandedPostalCode; }
}

/// <summary>
/// The highest-expanded postal code (if the postal code is short)
/// </summary>
private readonly string _highestExpandedPostalCode;

/// <summary>
/// Gets the highest-expanded postal code string.
/// </summary>
/// <value>The highest-expanded postal code string.</value>
protected internal string HighestExpandedPostalCodeString
{
get { return _highestExpandedPostalCode; }
}

/// <summary>
/// The current format.
/// </summary>
Expand Down Expand Up @@ -96,6 +124,9 @@ internal PostalCode(PostalCodeFormat[] formats, string redundantCharacters, stri
}

_backingPostalCode = String.Intern(Normalize(nonWhiteSpaceCode));

_lowestExpandedPostalCode = ExpandLowest();
_highestExpandedPostalCode = ExpandHighest();
}

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

/// <summary>
/// Compares the current object with another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <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>
public virtual int CompareTo(string other)
{
return other == null ? 1 : PostalCodeStringComparer.Default.Compare(PostalCodeString, other);
}

#endregion

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

/// <summary>
/// Implements the &lt;=.
/// </summary>
/// <param name="left">The left (as postal code).</param>
/// <param name="right">The right (as string).</param>
/// <returns>The result of the operator.</returns>
public static bool operator <=(PostalCode left, string right)
{
if (left == null)
{
return true;
}
return left.CompareTo(right) <= 0;
}

/// <summary>
/// Implements the &gt;=.
/// </summary>
/// <param name="left">The left (as postal code).</param>
/// <param name="right">The right (as string).</param>
/// <returns>The result of the operator.</returns>
public static bool operator >=(PostalCode left, PostalCode right)
{
if (left == null)
{
return right == null;
}
return left.CompareTo(right) >= 0;
}

/// <summary>
/// Implements the &gt;=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator >=(PostalCode left, PostalCode right)
public static bool operator >=(PostalCode left, string right)
{
if (left == null)
{
Expand Down Expand Up @@ -324,6 +395,26 @@ public virtual string ToHumanReadableString()
return ToHumanReadableString(outputFormat);
}

private string ExpandLowest()
{
if (_currentFormatType == FormatType.Short && _currentFormat.ShortExpansionAsLowestInRange != null)
{
return _backingPostalCode + _currentFormat.ShortExpansionAsLowestInRange;
}

return _backingPostalCode;
}

private string ExpandHighest()
{
if (_currentFormatType == FormatType.Short && _currentFormat.ShortExpansionAsHighestInRange != null)
{
return _backingPostalCode + _currentFormat.ShortExpansionAsHighestInRange;
}

return _backingPostalCode;
}

/// <summary>
/// Expands the postal code as lowest in range.
/// </summary>
Expand All @@ -342,7 +433,7 @@ public PostalCode ExpandPostalCodeAsLowestInRange()
}
}

return CreatePostalCode(ToString(), _allowConvertToShort);
return this;
}

/// <summary>
Expand All @@ -363,7 +454,7 @@ public PostalCode ExpandPostalCodeAsHighestInRange()
}
}

return CreatePostalCode(ToString(), _allowConvertToShort);
return this;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/PostalCodes/PostalCodeRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public static bool Contains(PostalCodeRange range, PostalCode specificCode)
}

return range.IsDefault ||
((range.Start <= specificCode.ExpandPostalCodeAsLowestInRange()) && (specificCode.ExpandPostalCodeAsHighestInRange() <= range.End));
((range.Start <= specificCode.LowestExpandedPostalCodeString) && (range.End >= specificCode.HighestExpandedPostalCodeString));
}

/// <summary>
Expand Down

0 comments on commit 8e0f3df

Please sign in to comment.