From d26d07c9701748e54508107aa5c5580675c0e163 Mon Sep 17 00:00:00 2001 From: Maher Kassim Date: Sun, 4 Feb 2018 18:12:52 -0500 Subject: [PATCH] Added optimization for comparisons involving lowest- and highest-expanded postal codes --- src/PostalCodes/PostalCode.cs | 97 +++++++++++++++++++++++++++++- src/PostalCodes/PostalCodeRange.cs | 2 +- 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/PostalCodes/PostalCode.cs b/src/PostalCodes/PostalCode.cs index f6d0512..a612d88 100644 --- a/src/PostalCodes/PostalCode.cs +++ b/src/PostalCodes/PostalCode.cs @@ -39,6 +39,34 @@ protected internal string PostalCodeString get { return _backingPostalCode; } } + /// + /// The lowest-expanded postal code (if the postal code is short) + /// + private readonly string _lowestExpandedPostalCode; + + /// + /// Gets the lowest-expanded postal code string. + /// + /// The lowest-expanded postal code string. + protected internal string LowestExpandedPostalCodeString + { + get { return _lowestExpandedPostalCode; } + } + + /// + /// The highest-expanded postal code (if the postal code is short) + /// + private readonly string _highestExpandedPostalCode; + + /// + /// Gets the highest-expanded postal code string. + /// + /// The highest-expanded postal code string. + protected internal string HighestExpandedPostalCodeString + { + get { return _highestExpandedPostalCode; } + } + /// /// The current format. /// @@ -96,6 +124,9 @@ internal PostalCode(PostalCodeFormat[] formats, string redundantCharacters, stri } _backingPostalCode = String.Intern(Normalize(nonWhiteSpaceCode)); + + _lowestExpandedPostalCode = ExpandLowest(); + _highestExpandedPostalCode = ExpandHighest(); } /// @@ -158,6 +189,16 @@ public virtual int CompareTo(PostalCode other) return other == null ? 1 : PostalCodeStringComparer.Default.Compare(PostalCodeString, other.PostalCodeString); } + /// + /// Compares the current object with another object of the same type. + /// + /// An object to compare with this object. + /// 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 parameter.Zero This object is equal to . Greater than zero This object is greater than . + public virtual int CompareTo(string other) + { + return other == null ? 1 : PostalCodeStringComparer.Default.Compare(PostalCodeString, other); + } + #endregion #region Implementation of IEquatable @@ -280,13 +321,43 @@ public int CompareTo(object obj) return left.CompareTo(right) <= 0; } + /// + /// Implements the <=. + /// + /// The left (as postal code). + /// The right (as string). + /// The result of the operator. + public static bool operator <=(PostalCode left, string right) + { + if (left == null) + { + return true; + } + return left.CompareTo(right) <= 0; + } + + /// + /// Implements the >=. + /// + /// The left (as postal code). + /// The right (as string). + /// The result of the operator. + public static bool operator >=(PostalCode left, PostalCode right) + { + if (left == null) + { + return right == null; + } + return left.CompareTo(right) >= 0; + } + /// /// Implements the >=. /// /// The left. /// The right. /// The result of the operator. - public static bool operator >=(PostalCode left, PostalCode right) + public static bool operator >=(PostalCode left, string right) { if (left == null) { @@ -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; + } + /// /// Expands the postal code as lowest in range. /// @@ -342,7 +433,7 @@ public PostalCode ExpandPostalCodeAsLowestInRange() } } - return CreatePostalCode(ToString(), _allowConvertToShort); + return this; } /// @@ -363,7 +454,7 @@ public PostalCode ExpandPostalCodeAsHighestInRange() } } - return CreatePostalCode(ToString(), _allowConvertToShort); + return this; } /// diff --git a/src/PostalCodes/PostalCodeRange.cs b/src/PostalCodes/PostalCodeRange.cs index ceb32f2..af5b1c3 100644 --- a/src/PostalCodes/PostalCodeRange.cs +++ b/src/PostalCodes/PostalCodeRange.cs @@ -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)); } ///