Convert type checks and null checks into pattern matching

This commit is contained in:
Patrick Barron 2020-04-05 12:07:43 -04:00
parent 29539174a3
commit 5966ee6d87
9 changed files with 24 additions and 54 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;