public T Clamp(T value)
        {
            Code.AssertArgument(IsNotEmpty, nameof(value), "Cannot fit the value into empty range.");
            Code.AssertArgument(
                !From.IsExclusiveBoundary, nameof(value), "The clamp range boundary From is exclusive and has no value.");
            Code.AssertArgument(
                !To.IsExclusiveBoundary, nameof(value), "The clamp range boundary To is exclusive and has no value.");

            // case for the positive infinity
            if (!RangeBoundaryFrom <T> .IsValid(value))
            {
                if (To < RangeBoundaryTo <T> .PositiveInfinity)
                {
                    return(To.Value);
                }
                return(value);
            }

            if (From > value)
            {
                return(From.Value);
            }

            if (To < value)
            {
                return(To.Value);
            }

            return(value);
        }
Exemplo n.º 2
0
        /// <summary>Adjusts the specified value so that it fits into a range specified.</summary>
        /// <typeparam name="T">The type of the range values.</typeparam>
        /// <param name="range">The range the value will be fitted to.</param>
        /// <param name="value">The value to be adjusted.</param>
        /// <exception cref="ArgumentException">The range is empty or any of its boundaries is exclusive.</exception>
        /// <returns>A new value that fits into a range specified</returns>
        public static T Adjust <T>(this Range <T> range, T value)
        {
            Code.AssertArgument(
                range.IsNotEmpty, nameof(range), "Cannot fit the value into empty range.");
            Code.AssertArgument(
                !range.From.IsExclusiveBoundary, nameof(range), "The boundary From is exclusive and has no value.");
            Code.AssertArgument(
                !range.To.IsExclusiveBoundary, nameof(range), "The boundary To is exclusive and has no value.");

            // case for the positive infinity
            if (!RangeBoundaryFrom <T> .IsValid(value))
            {
                if (range.To < RangeBoundaryTo <T> .PositiveInfinity)
                {
                    return(range.To.Value);
                }
                return(value);
            }

            if (range.From > value)
            {
                return(range.From.Value);
            }

            if (range.To < value)
            {
                return(range.To.Value);
            }

            return(value);
        }
Exemplo n.º 3
0
 private static Range <T> TryCreateCore <T>(
     T from, RangeBoundaryFromKind fromKind,
     T to, RangeBoundaryToKind toKind) =>
 RangeBoundaryFrom <T> .IsValid(from) && RangeBoundaryTo <T> .IsValid(to)
                                 ? TryCreate(
     RangeBoundaryFrom <T> .AdjustAndCreate(from, fromKind),
     RangeBoundaryTo <T> .AdjustAndCreate(to, toKind))
                                 : Range <T> .Empty;
Exemplo n.º 4
0
 public static bool StartsAfter <T>(this Range <T> range, T value) =>
 RangeBoundaryFrom <T> .IsValid(value) && range.From > Range.BoundaryFrom(value);
Exemplo n.º 5
0
 public static bool Contains <T>(this Range <T> range, T value) =>
 RangeBoundaryFrom <T> .IsValid(value)
                         ? Contains(range, Range.BoundaryFrom(value))
                         : Contains(range, Range.BoundaryTo(value));
 public bool StartsAfter(T value) =>
 RangeBoundaryFrom <T> .IsValid(value) && From > Range.BoundaryFrom(value);
 public bool Contains(T value) =>
 RangeBoundaryFrom <T> .IsValid(value)
                         ? Contains(Range.BoundaryFrom(value))
                         : Contains(Range.BoundaryTo(value));
Exemplo n.º 8
0
 public static bool IsValid <T>(T from, T to) =>
 RangeBoundaryFrom <T> .IsValid(from) &&
 RangeBoundaryTo <T> .IsValid(to) &&
 IsValid(BoundaryFrom(from), BoundaryTo(to));