/// <summary> /// Finds the contiguous time series entry that is the most close to the upper interval boundary. /// </summary> /// <param name="interval">The interface to the upper interval boundary to find the upper entry.</param> /// <param name="origin">The origin upper entry position to start the search from.</param> /// <returns>The contiguous time series entry if found; otherwise, null.</returns> public T FindUpper(IInterval <DateTime> interval, T origin) { if (interval != null) { var entry = origin = origin ?? Head; if (entry != null) { if (origin.Parent == this) { var reference = interval.IsUpperLimit(entry.Sample); while (entry != null) { switch (reference) { case bool last when last: entry = origin.Next; if (entry != null) { switch (interval.IsUpperLimit(entry.Sample)) { case bool next when next: origin = entry; reference = next; break; default: entry = null; break; } } break; case bool last when !last: entry = origin = origin.Prev; if (entry != null) { switch (interval.IsUpperLimit(entry.Sample)) { case bool next when !next: reference = next; break; default: entry = null; break; } } break; } } if (origin != null && !interval.IsLowerLimit(origin.Sample)) { origin = null; } } else { throw new ArgumentException($"The origin time series entry is bound to other container: {origin}", nameof(origin)); } } return(origin); } else { throw new ArgumentNullException(nameof(interval)); } }