/// <summary> /// Determines if a reporting period is greater than and adjacent to a second reporting period. /// </summary> /// <remarks> /// For this method to return true, the first reporting period's Start must be greater than the /// second reporting period's End, and they must be adjacent (subtracting one unit in the same granularity /// to the first reporting period's Start should make it equal to the second reporting period's End) /// For example, 3Q2017-4Q2017 is greater than and adjacent to 1Q2017-2Q2017. /// For example, 3Q2017-4Q2017 is NOT greater than 1Q2017-3Q2017, because they overlap. /// For example, 4Q2017-4Q2017 is NOT adjacent to 1Q2017-2Q2017, because there's a gap of 3Q2017. /// </remarks> /// <param name="reportingPeriod1">A reporting period to check if greater than and adjacent to a second reportin period.</param> /// <param name="reportingPeriod2">The second reporting period.</param> /// <returns> /// true if the first reporting period is greater than and adjacent to the second reporting period; false, otherwise. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="reportingPeriod1"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="reportingPeriod2"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="reportingPeriod1"/> cannot be compared against <paramref name="reportingPeriod2"/> because they represent different <see cref="UnitOfTime.UnitOfTimeKind"/>.</exception> public static bool IsGreaterThanAndAdjacentTo( this IReportingPeriod <UnitOfTime> reportingPeriod1, IReportingPeriod <UnitOfTime> reportingPeriod2) { if (reportingPeriod1 == null) { throw new ArgumentNullException(nameof(reportingPeriod1)); } if (reportingPeriod2 == null) { throw new ArgumentNullException(nameof(reportingPeriod2)); } if (reportingPeriod1.GetUnitOfTimeKind() != reportingPeriod2.GetUnitOfTimeKind()) { throw new ArgumentException(Invariant($"{nameof(reportingPeriod1)} cannot be compared against {nameof(reportingPeriod2)} because they represent different {nameof(UnitOfTimeKind)}")); } var mostGranularReportingPeriod1 = reportingPeriod1.ToMostGranular(); var mostGranularReportingPeriod2 = reportingPeriod2.ToMostGranular(); var result = mostGranularReportingPeriod1.Start.Plus(-1).Equals(mostGranularReportingPeriod2.End); return(result); }
/// <summary> /// Determines if an <see cref="IReportingPeriod{T}"/> is contained within another <see cref="IReportingPeriod{T}"/> /// For example, 1Q2017-3Q2017 contains 2Q2017-3Q2017. /// </summary> /// <remarks> /// If an endpoint in the second reporting period equals an endpoint in the first reporting period, that endpoint /// is considered to be contained within the first reporting period. Of course, both endpoints must be contained /// within the reporting period for this method to return true. /// </remarks> /// <param name="reportingPeriod1">A reporting period.</param> /// <param name="reportingPeriod2">A second reporting period to check for containment within the first reporting period.</param> /// <returns> /// true if the first reporting period contains the second one; false if not. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="reportingPeriod1"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="reportingPeriod2"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="reportingPeriod1"/> cannot be compared against <paramref name="reportingPeriod2"/> because they represent different <see cref="UnitOfTime.UnitOfTimeKind"/>.</exception> public static bool Contains( this IReportingPeriod <UnitOfTime> reportingPeriod1, IReportingPeriod <UnitOfTime> reportingPeriod2) { if (reportingPeriod1 == null) { throw new ArgumentNullException(nameof(reportingPeriod1)); } if (reportingPeriod2 == null) { throw new ArgumentNullException(nameof(reportingPeriod2)); } if (reportingPeriod1.GetUnitOfTimeKind() != reportingPeriod2.GetUnitOfTimeKind()) { throw new ArgumentException(Invariant($"{nameof(reportingPeriod1)} cannot be compared against {nameof(reportingPeriod2)} because they represent different {nameof(UnitOfTimeKind)}")); } reportingPeriod1 = reportingPeriod1.ToMostGranular(); reportingPeriod2 = reportingPeriod2.ToMostGranular(); bool startIsContained; if (reportingPeriod1.Start.UnitOfTimeGranularity == UnitOfTimeGranularity.Unbounded) { startIsContained = true; } else { if (reportingPeriod2.Start.UnitOfTimeGranularity == UnitOfTimeGranularity.Unbounded) { startIsContained = false; } else { startIsContained = reportingPeriod1.Start.CompareTo(reportingPeriod2.Start) <= 0; } } bool endIsContained; if (reportingPeriod1.End.UnitOfTimeGranularity == UnitOfTimeGranularity.Unbounded) { endIsContained = true; } else { if (reportingPeriod2.End.UnitOfTimeGranularity == UnitOfTimeGranularity.Unbounded) { endIsContained = false; } else { endIsContained = reportingPeriod1.End.CompareTo(reportingPeriod2.End) >= 0; } } var result = startIsContained && endIsContained; return(result); }