< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Sorting.AiredEpisodeOrderComparer
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs
Line coverage
97%
Covered lines: 48
Uncovered lines: 1
Coverable lines: 49
Total lines: 160
Line coverage: 97.9%
Branch coverage
94%
Covered branches: 34
Total branches: 36
Branch coverage: 94.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Type()100%11100%
Compare(...)100%66100%
Compare(...)100%88100%
CompareEpisodeToSpecial(...)100%1212100%
CompareSpecials(...)100%11100%
GetSpecialCompareValue(...)50%4.07483.33%
CompareEpisodes(...)100%66100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using Jellyfin.Data.Enums;
 5using MediaBrowser.Controller.Entities;
 6using MediaBrowser.Controller.Entities.TV;
 7using MediaBrowser.Controller.Sorting;
 8using MediaBrowser.Model.Querying;
 9
 10namespace Emby.Server.Implementations.Sorting
 11{
 12    public class AiredEpisodeOrderComparer : IBaseItemComparer
 13    {
 14        /// <summary>
 15        /// Gets the name.
 16        /// </summary>
 17        /// <value>The name.</value>
 118        public ItemSortBy Type => ItemSortBy.AiredEpisodeOrder;
 19
 20        /// <summary>
 21        /// Compares the specified x.
 22        /// </summary>
 23        /// <param name="x">The x.</param>
 24        /// <param name="y">The y.</param>
 25        /// <returns>System.Int32.</returns>
 26        public int Compare(BaseItem? x, BaseItem? y)
 27        {
 4828            ArgumentNullException.ThrowIfNull(x);
 29
 4730            ArgumentNullException.ThrowIfNull(y);
 31
 4632            var episode1 = x as Episode;
 4633            var episode2 = y as Episode;
 34
 4635            if (episode1 is null)
 36            {
 337                if (episode2 is null)
 38                {
 239                    return 0;
 40                }
 41
 142                return 1;
 43            }
 44
 4345            if (episode2 is null)
 46            {
 147                return -1;
 48            }
 49
 4250            return Compare(episode1, episode2);
 51        }
 52
 53        private int Compare(Episode x, Episode y)
 54        {
 4255            var isXSpecial = (x.ParentIndexNumber ?? -1) == 0;
 4256            var isYSpecial = (y.ParentIndexNumber ?? -1) == 0;
 57
 4258            if (isXSpecial && isYSpecial)
 59            {
 460                return CompareSpecials(x, y);
 61            }
 62
 3863            if (!isXSpecial && !isYSpecial)
 64            {
 1465                return CompareEpisodes(x, y);
 66            }
 67
 2468            if (!isXSpecial)
 69            {
 1270                return CompareEpisodeToSpecial(x, y);
 71            }
 72
 1273            return CompareEpisodeToSpecial(y, x) * -1;
 74        }
 75
 76        private static int CompareEpisodeToSpecial(Episode x, Episode y)
 77        {
 78            // http://thetvdb.com/wiki/index.php?title=Special_Episodes
 79
 2480            var xSeason = x.ParentIndexNumber ?? -1;
 2481            var ySeason = y.AirsAfterSeasonNumber ?? y.AirsBeforeSeasonNumber ?? -1;
 82
 2483            if (xSeason != ySeason)
 84            {
 1485                return xSeason.CompareTo(ySeason);
 86            }
 87
 88            // Special comes after episode
 1089            if (y.AirsAfterSeasonNumber.HasValue)
 90            {
 291                return -1;
 92            }
 93
 894            var yEpisode = y.AirsBeforeEpisodeNumber;
 95
 96            // Special comes before the season
 897            if (!yEpisode.HasValue)
 98            {
 299                return 1;
 100            }
 101
 102            // Compare episode number
 6103            var xEpisode = x.IndexNumber;
 104
 6105            if (!xEpisode.HasValue)
 106            {
 107                // Can't really compare if this happens
 2108                return 0;
 109            }
 110
 111            // Special comes before episode
 4112            if (xEpisode.Value == yEpisode.Value)
 113            {
 2114                return 1;
 115            }
 116
 2117            return xEpisode.Value.CompareTo(yEpisode.Value);
 118        }
 119
 120        private int CompareSpecials(Episode x, Episode y)
 121        {
 4122            return GetSpecialCompareValue(x).CompareTo(GetSpecialCompareValue(y));
 123        }
 124
 125        private static long GetSpecialCompareValue(Episode item)
 126        {
 127            // First sort by season number
 128            // Since there are three sort orders, pad with 9 digits (3 for each, figure 1000 episode buffer should be en
 8129            var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000L;
 130
 131            // Second sort order is if it airs after the season
 8132            if (item.AirsAfterSeasonNumber.HasValue)
 133            {
 0134                val += 1000000;
 135            }
 136
 137            // Third level is the episode number
 8138            val += (item.AirsBeforeEpisodeNumber ?? 0) * 1000;
 139
 140            // Finally, if that's still the same, last resort is the special number itself
 8141            val += item.IndexNumber ?? 0;
 142
 8143            return val;
 144        }
 145
 146        private static int CompareEpisodes(Episode x, Episode y)
 147        {
 14148            var xValue = ((x.ParentIndexNumber ?? -1) * 1000) + (x.IndexNumber ?? -1);
 14149            var yValue = ((y.ParentIndexNumber ?? -1) * 1000) + (y.IndexNumber ?? -1);
 14150            var comparisonResult = xValue.CompareTo(yValue);
 151            // If equal, compare premiere dates
 14152            if (comparisonResult == 0 && x.PremiereDate.HasValue && y.PremiereDate.HasValue)
 153            {
 6154                comparisonResult = DateTime.Compare(x.PremiereDate.Value, y.PremiereDate.Value);
 155            }
 156
 14157            return comparisonResult;
 158        }
 159    }
 160}