Skip to content

Commit

Permalink
Fixing issue #13
Browse files Browse the repository at this point in the history
  • Loading branch information
AJMitev committed Oct 29, 2021
1 parent 1901e7b commit c73a7bd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
34 changes: 20 additions & 14 deletions FileTypeChecker/FileTypeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,38 +140,44 @@ private static void RegisterTypes(IEnumerable<Assembly> assemblies)

private static IFileType FindBestMatch(Stream fileContent, IEnumerable<IFileType> result)
{
var scoreboard = new Dictionary<IFileType, int>();
var scoreboard = CreateScoreboard(fileContent, result);

return FindMaxScore(scoreboard);
}

private static IEnumerable<MatchScore> CreateScoreboard(Stream fileContent, IEnumerable<IFileType> result)
{
var scoreboard = new List<MatchScore>();

for (int typeIndex = 0; typeIndex < result.Count(); typeIndex++)
{
var currentType = result.ElementAt(typeIndex) as FileType;
var currentScore = currentType.GetMatchingNumber(fileContent);

if (!scoreboard.ContainsKey(currentType))
scoreboard.Add(currentType, currentType.GetMatchingNumber(fileContent));

scoreboard.Add(new MatchScore(currentType, currentScore));
}

return FindMaxScore(scoreboard);
return scoreboard;
}

private static IFileType FindMaxScore(IDictionary<IFileType, int> dictinalry)
private static IFileType FindMaxScore(IEnumerable<MatchScore> matches)
{
if (dictinalry.Count == 0)
if (matches.Count() == 0)
throw new InvalidOperationException(EmptyCollectionErrorMessage);

int maxAge = int.MinValue;
IFileType element = null;
int maxScore = int.MinValue;
IFileType bestMatch = null;

foreach (var type in dictinalry)
foreach (var match in matches)
{
if (!(type.Value > maxAge))
if (!(match.Score > maxScore))
continue;

maxAge = type.Value;
element = type.Key;
maxScore = match.Score;
bestMatch = match.Type;
}

return element;
return bestMatch;
}
}
}
16 changes: 16 additions & 0 deletions FileTypeChecker/MatchScore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace FileTypeChecker
{
using FileTypeChecker.Abstracts;

internal class MatchScore
{
internal MatchScore(IFileType fileType, int score)
{
this.Type = fileType;
this.Score = score;
}

public IFileType Type { get; set; }
public int Score { get; set; }
}
}

0 comments on commit c73a7bd

Please sign in to comment.