コード例 #1
0
        /// <summary>
        /// Returns whether <paramref name="element"/> is between <paramref name="lowerBound"/> and <paramref name="upperBound"/>.
        /// </summary>
        /// <typeparam name="T">The type of the values.</typeparam>
        /// <param name="element">The element to check.</param>
        /// <param name="lowerBound">The first boundary.</param>
        /// <param name="upperBound">The second boundary.</param>
        /// <param name="lowerInclusivity">The lower inclusivity.</param>
        /// <param name="upperInclusivity">The upper inclusivity.</param>
        /// <returns><see langword="true"/> if <paramref name="element"/> is between <paramref name="lowerBound"/> and <paramref name="upperBound"/>,
        /// <see langword="false"/> otherwise.</returns>
        /// <exception cref="InvalidOperationException">Thrown if <paramref name="lowerBound"/> equals <paramref name="upperBound"/> when <paramref name="lowerInclusivity"/> does not equal <paramref name="upperInclusivity"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="element"/> is <see langword="null"/></exception>
        /// <exception cref="Exception">A delegate callback throws an exception.</exception>
        public static bool IsBetween <T>([DisallowNull] this T element, [DisallowNull] T lowerBound, [DisallowNull] T upperBound,
                                         EInclusivity lowerInclusivity = EInclusivity.Inclusive, EInclusivity upperInclusivity = EInclusivity.Inclusive)
            where T : IComparable <T>
        {
            if (element is null)
            {
                // No Guard - missing class constraint
                ThrowHelper.ThrowArgumentNullException(nameof(element));
            }
            if (lowerBound is null)
            {
                // No Guard - missing class constraint
                ThrowHelper.ThrowArgumentNullException(nameof(lowerBound));
            }
            if (upperBound is null)
            {
                // No Guard - missing class constraint
                ThrowHelper.ThrowArgumentNullException(nameof(upperBound));
            }

            if (lowerBound.CompareTo(upperBound) > 0)
            {
                ThrowArgumentOrderException();
            }

            GuardEx.IsValid(lowerInclusivity, nameof(lowerInclusivity));
            GuardEx.IsValid(upperInclusivity, nameof(upperInclusivity));

            switch (lowerInclusivity)
            {
            case EInclusivity.Exclusive when upperInclusivity == EInclusivity.Exclusive:
                return(element.CompareTo(lowerBound) > 0 && element.CompareTo(upperBound) < 0);

            case EInclusivity.Inclusive when upperInclusivity == EInclusivity.Exclusive:
                if (lowerBound.CompareTo(upperBound) == 0)
                {
                    throw new InvalidOperationException($"{nameof(lowerBound)} cannot equal {nameof(upperBound)} when {nameof(lowerInclusivity)} does not equal {nameof(upperInclusivity)}.");
                }
                return(element.CompareTo(lowerBound) >= 0 && element.CompareTo(upperBound) < 0);

            case EInclusivity.Exclusive when upperInclusivity == EInclusivity.Inclusive:
                if (lowerBound.CompareTo(upperBound) == 0)
                {
                    throw new InvalidOperationException($"{nameof(lowerBound)} cannot equal {nameof(upperBound)} when {nameof(lowerInclusivity)} does not equal {nameof(upperInclusivity)}.");
                }
                return(element.CompareTo(lowerBound) > 0 && element.CompareTo(upperBound) <= 0);

            case EInclusivity.Inclusive when upperInclusivity == EInclusivity.Inclusive:
            default:
                return(element.CompareTo(lowerBound) >= 0 && element.CompareTo(upperBound) <= 0);
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns whether this <see cref="VersionRange"/> contains the specified <see cref="Version"/>.
        /// </summary>
        /// <param name="version">The version to check is in the range of this <see cref="VersionRange"/>.</param>
        /// <param name="lowerInclusivity">The lower inclusivity.</param>
        /// <param name="upperInclusivity">The upper inclusivity.</param>
        public bool Contains(Version version, EInclusivity lowerInclusivity = EInclusivity.Inclusive, EInclusivity upperInclusivity = EInclusivity.Inclusive)
        {
            Guard.IsNotNull(version, nameof(version));
            GuardEx.IsValid(lowerInclusivity, nameof(lowerInclusivity));
            GuardEx.IsValid(upperInclusivity, nameof(upperInclusivity));

            return(lowerInclusivity switch
            {
                EInclusivity.Exclusive when upperInclusivity == EInclusivity.Exclusive => Minimum < version && version < Maximum,
                EInclusivity.Exclusive when upperInclusivity == EInclusivity.Inclusive => Minimum < version && version <= Maximum,
                EInclusivity.Inclusive when upperInclusivity == EInclusivity.Exclusive => Minimum <= version && version < Maximum,
                _ => Minimum <= version && version <= Maximum
            });