From 6f2dd5d2faebc3247533ae4b3c698fae703a6222 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Mon, 25 Jul 2016 23:43:06 +0200 Subject: [PATCH] Improved Quality lookup. --- src/NzbDrone.Core/Qualities/Quality.cs | 92 ++++++++++++++------------ 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/src/NzbDrone.Core/Qualities/Quality.cs b/src/NzbDrone.Core/Qualities/Quality.cs index b4d6d6e97..64ea2cd56 100644 --- a/src/NzbDrone.Core/Qualities/Quality.cs +++ b/src/NzbDrone.Core/Qualities/Quality.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using NzbDrone.Common.Extensions; using NzbDrone.Core.Datastore; namespace NzbDrone.Core.Qualities @@ -72,63 +73,66 @@ public override bool Equals(object obj) //public static Quality WEBRip720p { get { return new Quality(14, "WEBRip-720p"); } } //public static Quality WEBRip1080p { get { return new Quality(15, "WEBRip-1080p"); } } public static Quality HDTV2160p { get { return new Quality(16, "HDTV-2160p"); } } - //public static Quality WEBRip2160p { get { return new Quality(17, "WEBRip-1080p"); } } + //public static Quality WEBRip2160p { get { return new Quality(17, "WEBRip-2160p"); } } public static Quality WEBDL2160p { get { return new Quality(18, "WEBDL-2160p"); } } public static Quality Bluray2160p { get { return new Quality(19, "Bluray-2160p"); } } - public static List All + static Quality() { - get + All = new List { - return new List - { - Unknown, - SDTV, - DVD, - WEBDL1080p, - HDTV720p, - WEBDL720p, - Bluray720p, - Bluray1080p, - WEBDL480p, - HDTV1080p, - RAWHD, - HDTV2160p, - WEBDL2160p, - Bluray2160p, - }; + Unknown, + SDTV, + DVD, + WEBDL1080p, + HDTV720p, + WEBDL720p, + Bluray720p, + Bluray1080p, + WEBDL480p, + HDTV1080p, + RAWHD, + HDTV2160p, + WEBDL2160p, + Bluray2160p, + }; + + AllLookup = new Quality[All.Select(v => v.Id).Max() + 1]; + foreach (var quality in All) + { + AllLookup[quality.Id] = quality; } + + DefaultQualityDefinitions = new HashSet + { + new QualityDefinition(Quality.Unknown) { Weight = 1, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.SDTV) { Weight = 2, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.WEBDL480p) { Weight = 3, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.DVD) { Weight = 4, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.HDTV720p) { Weight = 5, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.HDTV1080p) { Weight = 6, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.RAWHD) { Weight = 7, MinSize = 0, MaxSize = null }, + new QualityDefinition(Quality.WEBDL720p) { Weight = 8, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.Bluray720p) { Weight = 9, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.WEBDL1080p) { Weight = 10, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.Bluray1080p) { Weight = 11, MinSize = 0, MaxSize = 100 }, + new QualityDefinition(Quality.HDTV2160p) { Weight = 12, MinSize = 0, MaxSize = null }, + new QualityDefinition(Quality.WEBDL2160p) { Weight = 13, MinSize = 0, MaxSize = null }, + new QualityDefinition(Quality.Bluray2160p) { Weight = 14, MinSize = 0, MaxSize = null }, + }; } - public static HashSet DefaultQualityDefinitions - { - get - { - return new HashSet - { - new QualityDefinition(Quality.Unknown) { Weight = 1, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.SDTV) { Weight = 2, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.WEBDL480p) { Weight = 3, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.DVD) { Weight = 4, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.HDTV720p) { Weight = 5, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.HDTV1080p) { Weight = 6, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.RAWHD) { Weight = 7, MinSize = 0, MaxSize = null }, - new QualityDefinition(Quality.WEBDL720p) { Weight = 8, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.Bluray720p) { Weight = 9, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.WEBDL1080p) { Weight = 10, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.Bluray1080p) { Weight = 11, MinSize = 0, MaxSize = 100 }, - new QualityDefinition(Quality.HDTV2160p) { Weight = 12, MinSize = 0, MaxSize = null }, - new QualityDefinition(Quality.WEBDL2160p) { Weight = 13, MinSize = 0, MaxSize = null }, - new QualityDefinition(Quality.Bluray2160p) { Weight = 14, MinSize = 0, MaxSize = null }, - }; - } - } + public static readonly List All; + + public static readonly Quality[] AllLookup; + + public static readonly HashSet DefaultQualityDefinitions; public static Quality FindById(int id) { if (id == 0) return Unknown; - Quality quality = All.FirstOrDefault(v => v.Id == id); + var quality = AllLookup[id]; if (quality == null) throw new ArgumentException("ID does not match a known quality", "id");