using System; using System.Globalization; namespace Umbraco.Cms.Core.Models { /// /// Represents a range with a minimum and maximum value. /// /// The type of the minimum and maximum values. /// public class Range : IEquatable> where T : IComparable { /// /// Gets or sets the minimum value. /// /// /// The minimum value. /// public T Minimum { get; set; } /// /// Gets or sets the maximum value. /// /// /// The maximum value. /// public T Maximum { get; set; } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() => this.ToString("{0},{1}", CultureInfo.InvariantCulture); /// /// Returns a that represents this instance. /// /// A composite format string for a single value (minimum and maximum are equal). Use {0} for the minimum and {1} for the maximum value. /// A composite format string for the range values. Use {0} for the minimum and {1} for the maximum value. /// An object that supplies culture-specific formatting information. /// /// A that represents this instance. /// public string ToString(string format, string formatRange, IFormatProvider provider = null) => this.ToString(this.Minimum.CompareTo(this.Maximum) == 0 ? format : formatRange, provider); /// /// Returns a that represents this instance. /// /// A composite format string for the range values. Use {0} for the minimum and {1} for the maximum value. /// An object that supplies culture-specific formatting information. /// /// A that represents this instance. /// public string ToString(string format, IFormatProvider provider = null) => string.Format(provider, format, this.Minimum, this.Maximum); /// /// Determines whether this range is valid (the minimum value is lower than or equal to the maximum value). /// /// /// true if this range is valid; otherwise, false. /// public bool IsValid() => this.Minimum.CompareTo(this.Maximum) <= 0; /// /// Determines whether this range contains the specified value. /// /// The value. /// /// true if this range contains the specified value; otherwise, false. /// public bool ContainsValue(T value) => this.Minimum.CompareTo(value) <= 0 && value.CompareTo(this.Maximum) <= 0; /// /// Determines whether this range is inside the specified range. /// /// The range. /// /// true if this range is inside the specified range; otherwise, false. /// public bool IsInsideRange(Range range) => this.IsValid() && range.IsValid() && range.ContainsValue(this.Minimum) && range.ContainsValue(this.Maximum); /// /// Determines whether this range contains the specified range. /// /// The range. /// /// true if this range contains the specified range; otherwise, false. /// public bool ContainsRange(Range range) => this.IsValid() && range.IsValid() && this.ContainsValue(range.Minimum) && this.ContainsValue(range.Maximum); /// /// Determines whether the specified , is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) => obj is Range other && this.Equals(other); /// /// Indicates whether the current object is equal to another object of the same type. /// /// An object to compare with this object. /// /// if the current object is equal to the parameter; otherwise, . /// public bool Equals(Range other) => other != null && this.Equals(other.Minimum, other.Maximum); /// /// Determines whether the specified and values are equal to this instance values. /// /// The minimum value. /// The maximum value. /// /// true if the specified and values are equal to this instance values; otherwise, false. /// public bool Equals(T minimum, T maximum) => this.Minimum.CompareTo(minimum) == 0 && this.Maximum.CompareTo(maximum) == 0; /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() => (this.Minimum, this.Maximum).GetHashCode(); } }