From 5966ee6d87cf4abbe6cf327286788e7e03fe0592 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Sun, 5 Apr 2020 12:07:43 -0400 Subject: [PATCH] Convert type checks and null checks into pattern matching --- MediaBrowser.Api/ItemUpdateService.cs | 27 +++++++------------ MediaBrowser.Api/LiveTv/LiveTvService.cs | 3 +-- .../Playback/Hls/DynamicHlsService.cs | 6 ++--- MediaBrowser.Api/PluginService.cs | 4 +-- .../ScheduledTasks/ScheduledTaskService.cs | 8 ++---- MediaBrowser.Api/SearchService.cs | 17 ++++-------- MediaBrowser.Api/TvShowsService.cs | 4 +-- .../UserLibrary/BaseItemsByNameService.cs | 3 +-- MediaBrowser.Api/UserLibrary/ItemsService.cs | 6 ++--- 9 files changed, 24 insertions(+), 54 deletions(-) diff --git a/MediaBrowser.Api/ItemUpdateService.cs b/MediaBrowser.Api/ItemUpdateService.cs index c81e89ca3..962e4ddf4 100644 --- a/MediaBrowser.Api/ItemUpdateService.cs +++ b/MediaBrowser.Api/ItemUpdateService.cs @@ -263,8 +263,7 @@ namespace MediaBrowser.Api item.Overview = request.Overview; item.Genres = request.Genres; - var episode = item as Episode; - if (episode != null) + if (item is Episode episode) { episode.AirsAfterSeasonNumber = request.AirsAfterSeasonNumber; episode.AirsBeforeEpisodeNumber = request.AirsBeforeEpisodeNumber; @@ -302,14 +301,12 @@ namespace MediaBrowser.Api item.PreferredMetadataCountryCode = request.PreferredMetadataCountryCode; item.PreferredMetadataLanguage = request.PreferredMetadataLanguage; - var hasDisplayOrder = item as IHasDisplayOrder; - if (hasDisplayOrder != null) + if (item is IHasDisplayOrder hasDisplayOrder) { hasDisplayOrder.DisplayOrder = request.DisplayOrder; } - var hasAspectRatio = item as IHasAspectRatio; - if (hasAspectRatio != null) + if (item is IHasAspectRatio hasAspectRatio) { hasAspectRatio.AspectRatio = request.AspectRatio; } @@ -337,16 +334,14 @@ namespace MediaBrowser.Api item.ProviderIds = request.ProviderIds; - var video = item as Video; - if (video != null) + if (item is Video video) { video.Video3DFormat = request.Video3DFormat; } if (request.AlbumArtists != null) { - var hasAlbumArtists = item as IHasAlbumArtist; - if (hasAlbumArtists != null) + if (item is IHasAlbumArtist hasAlbumArtists) { hasAlbumArtists.AlbumArtists = request .AlbumArtists @@ -357,8 +352,7 @@ namespace MediaBrowser.Api if (request.ArtistItems != null) { - var hasArtists = item as IHasArtist; - if (hasArtists != null) + if (item is IHasArtist hasArtists) { hasArtists.Artists = request .ArtistItems @@ -367,20 +361,17 @@ namespace MediaBrowser.Api } } - var song = item as Audio; - if (song != null) + if (item is Audio song) { song.Album = request.Album; } - var musicVideo = item as MusicVideo; - if (musicVideo != null) + if (item is MusicVideo musicVideo) { musicVideo.Album = request.Album; } - var series = item as Series; - if (series != null) + if (item is Series series) { series.Status = GetSeriesStatus(request); diff --git a/MediaBrowser.Api/LiveTv/LiveTvService.cs b/MediaBrowser.Api/LiveTv/LiveTvService.cs index 4b4496139..c8bbbde46 100644 --- a/MediaBrowser.Api/LiveTv/LiveTvService.cs +++ b/MediaBrowser.Api/LiveTv/LiveTvService.cs @@ -1050,8 +1050,7 @@ namespace MediaBrowser.Api.LiveTv { query.IsSeries = true; - var series = _libraryManager.GetItemById(request.LibrarySeriesId) as Series; - if (series != null) + if (_libraryManager.GetItemById(request.LibrarySeriesId) is Series series) { query.Name = series.Name; } diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 3348a3187..8b8fcb05e 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -438,8 +438,7 @@ namespace MediaBrowser.Api.Playback.Hls { var segmentId = "0"; - var segmentRequest = request as GetHlsVideoSegment; - if (segmentRequest != null) + if (request is GetHlsVideoSegment segmentRequest) { segmentId = segmentRequest.SegmentId; } @@ -690,8 +689,7 @@ namespace MediaBrowser.Api.Playback.Hls return false; } - var request = state.Request as IMasterHlsRequest; - if (request != null && !request.EnableAdaptiveBitrateStreaming) + if (state.Request is IMasterHlsRequest request && !request.EnableAdaptiveBitrateStreaming) { return false; } diff --git a/MediaBrowser.Api/PluginService.cs b/MediaBrowser.Api/PluginService.cs index 16d3268b9..7f74511ee 100644 --- a/MediaBrowser.Api/PluginService.cs +++ b/MediaBrowser.Api/PluginService.cs @@ -243,9 +243,7 @@ namespace MediaBrowser.Api // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs var id = Guid.Parse(GetPathValue(1)); - var plugin = _appHost.Plugins.First(p => p.Id == id) as IHasPluginConfiguration; - - if (plugin == null) + if (!(_appHost.Plugins.First(p => p.Id == id) is IHasPluginConfiguration plugin)) { throw new FileNotFoundException(); } diff --git a/MediaBrowser.Api/ScheduledTasks/ScheduledTaskService.cs b/MediaBrowser.Api/ScheduledTasks/ScheduledTaskService.cs index 2bd387229..e08a8482e 100644 --- a/MediaBrowser.Api/ScheduledTasks/ScheduledTaskService.cs +++ b/MediaBrowser.Api/ScheduledTasks/ScheduledTaskService.cs @@ -123,9 +123,7 @@ namespace MediaBrowser.Api.ScheduledTasks { var isHidden = false; - var configurableTask = i.ScheduledTask as IConfigurableScheduledTask; - - if (configurableTask != null) + if (i.ScheduledTask is IConfigurableScheduledTask configurableTask) { isHidden = configurableTask.IsHidden; } @@ -142,9 +140,7 @@ namespace MediaBrowser.Api.ScheduledTasks { var isEnabled = true; - var configurableTask = i.ScheduledTask as IConfigurableScheduledTask; - - if (configurableTask != null) + if (i.ScheduledTask is IConfigurableScheduledTask configurableTask) { isEnabled = configurableTask.IsEnabled; } diff --git a/MediaBrowser.Api/SearchService.cs b/MediaBrowser.Api/SearchService.cs index 0a3dc19dc..f1df01a09 100644 --- a/MediaBrowser.Api/SearchService.cs +++ b/MediaBrowser.Api/SearchService.cs @@ -234,20 +234,17 @@ namespace MediaBrowser.Api SetThumbImageInfo(result, item); SetBackdropImageInfo(result, item); - var program = item as LiveTvProgram; - if (program != null) + if (item is LiveTvProgram program) { result.StartDate = program.StartDate; } - var hasSeries = item as IHasSeries; - if (hasSeries != null) + if (item is IHasSeries hasSeries) { result.Series = hasSeries.SeriesName; } - var series = item as Series; - if (series != null) + if (item is Series series) { if (series.Status.HasValue) { @@ -255,17 +252,13 @@ namespace MediaBrowser.Api } } - var album = item as MusicAlbum; - - if (album != null) + if (item is MusicAlbum album) { result.Artists = album.Artists; result.AlbumArtist = album.AlbumArtist; } - var song = item as Audio; - - if (song != null) + if (item is Audio song) { result.AlbumArtist = song.AlbumArtists.FirstOrDefault(); result.Artists = song.Artists; diff --git a/MediaBrowser.Api/TvShowsService.cs b/MediaBrowser.Api/TvShowsService.cs index 334d1db51..1282bba8a 100644 --- a/MediaBrowser.Api/TvShowsService.cs +++ b/MediaBrowser.Api/TvShowsService.cs @@ -424,9 +424,7 @@ namespace MediaBrowser.Api if (!string.IsNullOrWhiteSpace(request.SeasonId)) { - var season = _libraryManager.GetItemById(new Guid(request.SeasonId)) as Season; - - if (season == null) + if (!(_libraryManager.GetItemById(new Guid(request.SeasonId)) is Season season)) { throw new ResourceNotFoundException("No season exists with Id " + request.SeasonId); } diff --git a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs index 9fa222d32..7e4e589ea 100644 --- a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs +++ b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs @@ -82,8 +82,7 @@ namespace MediaBrowser.Api.UserLibrary { var parent = GetParentItem(request); - var collectionFolder = parent as IHasCollectionType; - if (collectionFolder != null) + if (parent is IHasCollectionType collectionFolder) { return collectionFolder.CollectionType; } diff --git a/MediaBrowser.Api/UserLibrary/ItemsService.cs b/MediaBrowser.Api/UserLibrary/ItemsService.cs index c7b505171..5e6cad787 100644 --- a/MediaBrowser.Api/UserLibrary/ItemsService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemsService.cs @@ -199,14 +199,12 @@ namespace MediaBrowser.Api.UserLibrary item = _libraryManager.GetUserRootFolder(); } - Folder folder = item as Folder; - if (folder == null) + if (!(item is Folder folder)) { folder = _libraryManager.GetUserRootFolder(); } - var hasCollectionType = folder as IHasCollectionType; - if (hasCollectionType != null + if (folder is IHasCollectionType hasCollectionType && string.Equals(hasCollectionType.CollectionType, CollectionType.Playlists, StringComparison.OrdinalIgnoreCase)) { request.Recursive = true;