Beispiel #1
0
        /// <summary>
        /// Performs a comparison of the current object to another object and returns a value
        /// indicating whether the object is less than, greater than, or equal to the other.
        /// </summary>
        /// <param name="other">The <see cref="ApiVersion">other</see> object to compare to.</param>
        /// <returns>Zero if the objects are equal, one if the current object is greater than the
        /// <paramref name="other"/> object, or negative one if the current object is less than the
        /// <paramref name="other"/> object.</returns>
        /// <remarks>The version <see cref="P:Status">status</see> is not included in comparisons.</remarks>
        public virtual int CompareTo(ApiVersion other)
        {
            if (other == null)
            {
                return(1);
            }

            var result = Nullable.Compare(GroupVersion, other.GroupVersion);

            if (result == 0)
            {
                result = Nullable.Compare(MajorVersion, other.MajorVersion);

                if (result == 0)
                {
                    result = ImpliedMinorVersion.CompareTo(other.ImpliedMinorVersion);

                    if (result == 0)
                    {
                        result = StringComparer.OrdinalIgnoreCase.Compare(Status, other.Status);

                        if (result != 0)
                        {
                            result = result < 0 ? -1 : 1;
                        }
                    }
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Gets a hash code for the current instance.
        /// </summary>
        /// <returns>A hash code.</returns>
        /// <remarks>The hash code is based on the uppercase, invariant hash of the
        /// <see cref="ToString()">text representation</see> of the object.</remarks>
        public override int GetHashCode()
        {
            var hashes = new List <int>(4);

            if (GroupVersion != null)
            {
                hashes.Add(GroupVersion.Value.GetHashCode());
            }

            if (MajorVersion != null)
            {
                hashes.Add(MajorVersion.Value.GetHashCode());
                hashes.Add(ImpliedMinorVersion.GetHashCode());
            }

            if (!IsNullOrEmpty(Status))
            {
                hashes.Add(StringComparer.OrdinalIgnoreCase.GetHashCode(Status));
            }

            var hash = hashes[0];

            for (var i = 1; i < hashes.Count; i++)
            {
                hash = (hash * 397) ^ hashes[i];
            }

            return(hash);
        }
        /// <summary>
        /// Performs a comparison of the current object to another object and returns a value
        /// indicating whether the object is less than, greater than, or equal to the other.
        /// </summary>
        /// <param name="other">The <see cref="ApiVersion">other</see> object to compare to.</param>
        /// <returns>Zero if the objects are equal, one if the current object is greater than the
        /// <paramref name="other"/> object, or negative one if the current object is less than the
        /// <paramref name="other"/> object.</returns>
        /// <remarks>The version <see cref="Status">status</see> is not included in comparisons.</remarks>
        public virtual int CompareTo(ApiVersion other)
        {
            if (other == null)
            {
                return(1);
            }

            var result = Nullable.Compare(GroupVersion, other.GroupVersion);

            if (result != 0)
            {
                return(result);
            }

            result = Nullable.Compare(MajorVersion, other.MajorVersion);

            if (result != 0)
            {
                return(result);
            }

            result = ImpliedMinorVersion.CompareTo(other.ImpliedMinorVersion);

            if (result != 0)
            {
                return(result);
            }

            if (IsNullOrEmpty(Status))
            {
                if (!IsNullOrEmpty(other.Status))
                {
                    result = 1;
                }
            }
            else if (IsNullOrEmpty(other.Status))
            {
                result = -1;
            }
            else
            {
                result = StringComparer.OrdinalIgnoreCase.Compare(Status, other.Status);

                if (result < 0)
                {
                    result = -1;
                }
                else if (result > 0)
                {
                    result = 1;
                }
            }

            return(result);
        }
        /// <summary>
        /// Determines whether the current object equals another object.
        /// </summary>
        /// <param name="other">The <see cref="ApiVersion">other</see> to evaluate.</param>
        /// <returns>True if the specified objet is equal to the current instance; otherwise, false.</returns>
        public virtual bool Equals(ApiVersion other)
        {
            if (other == null)
            {
                return(false);
            }

            return(Nullable.Equals(GroupVersion, other.GroupVersion) &&
                   Nullable.Equals(MajorVersion, other.MajorVersion) &&
                   ImpliedMinorVersion.Equals(other.ImpliedMinorVersion) &&
                   string.Equals(Status, other.Status, StringComparison.OrdinalIgnoreCase));
        }
Beispiel #5
0
        /// <summary>
        /// Gets a hash code for the current instance.
        /// </summary>
        /// <returns>A hash code.</returns>
        /// <remarks>The hash code is based on the uppercase, invariant hash of the
        /// <see cref="ToString()">text representation</see> of the object.</remarks>
        public override int GetHashCode()
        {
            // perf: api version is used in a lot sets and as a dictionary keys
            // since it's immutable, calculate the hash code once and reuse it
            if (hashCode != default)
            {
                return(hashCode);
            }

            var hashes = new List <int>(capacity: 4);

            if (GroupVersion != null)
            {
                hashes.Add(GroupVersion.Value.GetHashCode());
            }

            if (MajorVersion != null)
            {
                hashes.Add(MajorVersion.Value.GetHashCode());
                hashes.Add(ImpliedMinorVersion.GetHashCode());
            }

            if (!IsNullOrEmpty(Status))
            {
                hashes.Add(StringComparer.OrdinalIgnoreCase.GetHashCode(Status));
            }

            var hash = hashes[0];

            for (var i = 1; i < hashes.Count; i++)
            {
                hash = (hash * Prime) ^ hashes[i];
            }

            return(hashCode = hash);
        }