diff --git a/src/PostalCodes.UnitTests/PostalCodeRangeTests.cs b/src/PostalCodes.UnitTests/PostalCodeRangeTests.cs index 35039f2..d909fe3 100644 --- a/src/PostalCodes.UnitTests/PostalCodeRangeTests.cs +++ b/src/PostalCodes.UnitTests/PostalCodeRangeTests.cs @@ -21,7 +21,14 @@ private static PostalCodeRange MakeRangeUS(string start, string end) var endPc = end != null ? new USPostalCode(end) : null; return new PostalCodeRange(startPc, endPc); } - + + private static PostalCodeRange MakeRangeJP(string start, string end) + { + var startPc = start != null ? new JPPostalCode(start) : null; + var endPc = end != null ? new JPPostalCode(end) : null; + return new PostalCodeRange(startPc, endPc); + } + public static IEnumerable CoincidesWithTestCases { get @@ -231,6 +238,11 @@ public static IEnumerable Contains_WithPostalCode_TestCases yield return new TestCaseData(rangeUS2, new USPostalCode("28000 1235")).Returns(true); yield return new TestCaseData(rangeUS3, new USPostalCode("28000")).Returns(true); yield return new TestCaseData(rangeUS3, new USPostalCode("28000 1235")).Returns(true); + + var rangeJP = MakeRangeJP("9000001", "9000006"); + yield return new TestCaseData(rangeJP, new JPPostalCode("9000005")).Returns(true); + yield return new TestCaseData(rangeJP, new JPPostalCode("9000-005")).Returns(true); + yield return new TestCaseData(rangeJP, new JPPostalCode("9000007")).Returns(false); } } diff --git a/src/PostalCodes/PostalCode.cs b/src/PostalCodes/PostalCode.cs index d09a751..523acdc 100644 --- a/src/PostalCodes/PostalCode.cs +++ b/src/PostalCodes/PostalCode.cs @@ -143,7 +143,7 @@ internal PostalCode(PostalCodeFormat[] formats, string redundantCharacters, stri /// /// The other. /// true if [is adjacent to] [the specified other]; otherwise, false. - public bool IsAdjacentTo(PostalCode other) + private bool IsAdjacentTo(PostalCode other) { return other != null && Predecessor == other || Successor == other; } @@ -152,19 +152,13 @@ public bool IsAdjacentTo(PostalCode other) /// Gets the predecessor. /// /// The predecessor. - public PostalCode Predecessor - { - get { return PredecessorImpl; } - } + public PostalCode Predecessor => PredecessorImpl; /// /// Gets the successor. /// /// The successor. - public PostalCode Successor - { - get { return SuccessorImpl; } - } + public PostalCode Successor => SuccessorImpl; /// /// Gets the predecessor implementation. @@ -474,11 +468,7 @@ public PostalCode ExpandPostalCodeAsHighestInRange() /// true if XXXX, false otherwise. public static bool AreAdjacent(PostalCode left, PostalCode right) { - if (left == null || right == null) - { - return false; - } - return left.IsAdjacentTo(right) && right.IsAdjacentTo(left); + return left != null && right != null && left.IsAdjacentTo(right) && right.IsAdjacentTo(left); } /// diff --git a/src/PostalCodes/PostalCodeRange.cs b/src/PostalCodes/PostalCodeRange.cs index af5b1c3..e635f3b 100644 --- a/src/PostalCodes/PostalCodeRange.cs +++ b/src/PostalCodes/PostalCodeRange.cs @@ -8,11 +8,6 @@ namespace PostalCodes /// public class PostalCodeRange : IEquatable, IComparable, IComparable { - /// - /// The lazy default - /// - private static readonly Lazy LazyDefault = new Lazy(() => new PostalCodeRange(null, null)); - private PostalCode _start; private PostalCode _end; @@ -20,10 +15,7 @@ public class PostalCodeRange : IEquatable, IComparable /// The default. - public static PostalCodeRange Default - { - get { return LazyDefault.Value; } - } + public static readonly PostalCodeRange Default = new PostalCodeRange(null, null); /// /// Initializes a new instance of the class. @@ -35,19 +27,17 @@ public PostalCodeRange(PostalCode start, PostalCode end) { if (start != null && end != null && start.GetType() != end.GetType()) { - throw new ArgumentException(String.Format( - "The start and the end of the range are from incompatible types ('{0}' & '{1}')", - start.GetType(), end.GetType())); + throw new ArgumentException( + $"The start and the end of the range are from incompatible types ('{start.GetType()}' & '{end.GetType()}')"); } if (end != null && start != null && start > end) { - throw new ArgumentException(String.Format( - "PostalCodeRange end ({0}) can't be before start ({1})", end, start)); + throw new ArgumentException($"PostalCodeRange end ({end}) can't be before start ({start})"); } - _start = start != null ? start.ExpandPostalCodeAsLowestInRange() : null; - _end = end != null ? end.ExpandPostalCodeAsHighestInRange() : null; + _start = start?.ExpandPostalCodeAsLowestInRange(); + _end = end?.ExpandPostalCodeAsHighestInRange(); } ///