Skip to content

Commit

Permalink
Making sure we accept that candidates can be null due to ProtoBuf ini…
Browse files Browse the repository at this point in the history
…tialization.
  • Loading branch information
AddictedCS committed Feb 23, 2024
1 parent 2bd5136 commit 5cdfde3
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/SoundFingerprinting/LCS/Candidates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class Candidates
{
[ProtoMember(1)]
private readonly ConcurrentDictionary<IModelReference, List<MatchedWith>> candidates;
private readonly ConcurrentDictionary<IModelReference, List<MatchedWith>>? candidates;

/// <summary>
/// Initializes a new instance of the <see cref="Candidates"/> class.
Expand All @@ -27,7 +27,12 @@ public Candidates()
/// <summary>
/// Gets count of candidates.
/// </summary>
public int Count => candidates.Count;
public int Count => candidates?.Count ?? 0;

/// <summary>
/// Gets a value indicating whether candidates are empty.
/// </summary>
public bool IsEmpty => candidates?.IsEmpty ?? true;

/// <summary>
/// Initializes a new instance of the <see cref="Candidates"/> class.
Expand All @@ -48,7 +53,7 @@ public Candidates(IModelReference trackReference, IEnumerable<MatchedWith> candi
/// <returns>Get all matched tracks.</returns>
public IEnumerable<IModelReference> GetMatchedTracks()
{
return candidates.Keys;
return candidates?.Keys ?? Enumerable.Empty<IModelReference>();
}

/// <summary>
Expand All @@ -58,7 +63,7 @@ public IEnumerable<IModelReference> GetMatchedTracks()
/// <returns>List of matched withs.</returns>
public IEnumerable<MatchedWith> GetMatchesForTrack(IModelReference trackReference)
{
return candidates.TryGetValue(trackReference, out var matchedWith) ? matchedWith : Enumerable.Empty<MatchedWith>();
return (candidates?.TryGetValue(trackReference, out var matchedWith) ?? false) ? matchedWith : Enumerable.Empty<MatchedWith>();
}

/// <summary>
Expand All @@ -68,7 +73,7 @@ public IEnumerable<MatchedWith> GetMatchesForTrack(IModelReference trackReferenc
/// <param name="match">An instance of <see cref="MatchedWith"/>.</param>
public void AddNewMatchForTrack(IModelReference trackReference, MatchedWith match)
{
candidates.AddOrUpdate(trackReference, _ => new List<MatchedWith> {match}, (_, old) =>
candidates?.AddOrUpdate(trackReference, _ => new List<MatchedWith> {match}, (_, old) =>
{
old.Add(match);
return old;
Expand Down

0 comments on commit 5cdfde3

Please sign in to comment.