Ejemplo n.º 1
0
        public int CompareTo(MediaType other)
        {
            if (other == null)
            {
                return(MOVE_UP);
            }
            if (Equals(other))
            {
                return(0);
            }

            // first, always move down */*
            if (IsWildCard)
            {
                if (other.IsWildCard)
                {
                    return(0);
                }
                return(MOVE_DOWN);
            }

            // then sort by quality
            if (Quality != other.Quality)
            {
                return(Quality > other.Quality ? MOVE_UP : MOVE_DOWN);
            }

            // then, if the quality is the same, always move application/xml at the end
            if (MediaType == "application/xml")
            {
                return(MOVE_DOWN);
            }
            if (other.MediaType == "application/xml")
            {
                return(MOVE_UP);
            }

            if (TopLevelMediaType != other.TopLevelMediaType)
            {
                if (IsTopLevelWildcard)
                {
                    return(MOVE_DOWN);
                }
                if (other.IsTopLevelWildcard)
                {
                    return(MOVE_UP);
                }
                return(TopLevelMediaType.CompareTo(other.TopLevelMediaType));
            }

            if (Subtype != other.Subtype)
            {
                if (IsSubtypeWildcard)
                {
                    return(MOVE_DOWN);
                }
                if (other.IsSubtypeWildcard)
                {
                    return(MOVE_UP);
                }
                return(Subtype.CompareTo(other.Subtype));
            }
            return(0);
        }
Ejemplo n.º 2
0
        public int CompareTo(MimeType other)
        {
            var comp = Type.CompareTo(other.Type);

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

            comp = Subtype.CompareTo(other.Subtype);
            if (comp != 0)
            {
                return(comp);
            }

            comp = Parameters.Count - other.Parameters.Count;
            if (comp != 0)
            {
                return(comp);
            }

            var thisAttributes  = new SortedSet <string>(Parameters.Keys, StringComparer.InvariantCultureIgnoreCase);
            var otherAttributes = new SortedSet <string>(other.Parameters.Keys, StringComparer.InvariantCultureIgnoreCase);

            var thisAttributesIterator  = thisAttributes.GetEnumerator();
            var otherAttributesIterator = otherAttributes.GetEnumerator();
            var comparer = StringComparer.InvariantCultureIgnoreCase;

            while (thisAttributesIterator.MoveNext())
            {
                otherAttributesIterator.MoveNext();

                var thisAttribute  = thisAttributesIterator.Current;
                var otherAttribute = otherAttributesIterator.Current;

                comp = comparer.Compare(thisAttribute, otherAttribute);
                if (comp != 0)
                {
                    return(comp);
                }

                if (PARAM_CHARSET.Equals(thisAttribute))
                {
                    var thisCharset  = Encoding;
                    var otherCharset = other.Encoding;
                    if (thisCharset != otherCharset)
                    {
                        if (thisCharset == null)
                        {
                            return(-1);
                        }

                        if (otherCharset == null)
                        {
                            return(1);
                        }

                        comp = comparer.Compare(thisCharset.BodyName, otherCharset.BodyName);
                        if (comp != 0)
                        {
                            return(comp);
                        }
                    }
                }
                else
                {
                    var thisValue  = Parameters[thisAttribute];
                    var otherValue = other.Parameters[otherAttribute];
                    if (otherValue == null)
                    {
                        otherValue = string.Empty;
                    }

                    comp = thisValue.CompareTo(otherValue);
                    if (comp != 0)
                    {
                        return(comp);
                    }
                }
            }

            return(0);
        }