From c73a7bda1d13d08de3ed716792dc5c04782aa119 Mon Sep 17 00:00:00 2001 From: "Aleksandar J. Mitev" Date: Fri, 29 Oct 2021 08:51:07 +0300 Subject: [PATCH] Fixing issue #13 --- FileTypeChecker/FileTypeValidator.cs | 34 ++++++++++++++++------------ FileTypeChecker/MatchScore.cs | 16 +++++++++++++ 2 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 FileTypeChecker/MatchScore.cs diff --git a/FileTypeChecker/FileTypeValidator.cs b/FileTypeChecker/FileTypeValidator.cs index 2bf5309..14eff32 100644 --- a/FileTypeChecker/FileTypeValidator.cs +++ b/FileTypeChecker/FileTypeValidator.cs @@ -140,38 +140,44 @@ private static void RegisterTypes(IEnumerable assemblies) private static IFileType FindBestMatch(Stream fileContent, IEnumerable result) { - var scoreboard = new Dictionary(); + var scoreboard = CreateScoreboard(fileContent, result); + + return FindMaxScore(scoreboard); + } + + private static IEnumerable CreateScoreboard(Stream fileContent, IEnumerable result) + { + var scoreboard = new List(); 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 dictinalry) + private static IFileType FindMaxScore(IEnumerable 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; } } } \ No newline at end of file diff --git a/FileTypeChecker/MatchScore.cs b/FileTypeChecker/MatchScore.cs new file mode 100644 index 0000000..fea5ff6 --- /dev/null +++ b/FileTypeChecker/MatchScore.cs @@ -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; } + } +}