Skip to content

Commit 2622add

Browse files
RenderMichaelsandeepsuryaprasad
authored andcommitted
[dotnet] Add nullability to IniFileReader (SeleniumHQ#14929)
1 parent 8834781 commit 2622add

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

dotnet/src/webdriver/Firefox/Internal/IniFileReader.cs

+21-16
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
using System.Collections.ObjectModel;
2323
using System.IO;
2424

25+
#nullable enable
26+
2527
namespace OpenQA.Selenium.Firefox.Internal
2628
{
2729
/// <summary>
2830
/// Parses and reads an INI file.
2931
/// </summary>
30-
internal class IniFileReader
32+
internal sealed class IniFileReader
3133
{
32-
private Dictionary<string, Dictionary<string, string>> iniFileStore = new Dictionary<string, Dictionary<string, string>>();
34+
private readonly Dictionary<string, Dictionary<string, string>> iniFileStore = new Dictionary<string, Dictionary<string, string>>();
3335

3436
/// <summary>
3537
/// Initializes a new instance of the <see cref="IniFileReader"/> class.
3638
/// </summary>
3739
/// <param name="fileName">The full path to the .INI file to be read.</param>
40+
/// <exception cref="ArgumentNullException">If <paramref name="fileName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
41+
/// <exception cref="FileNotFoundException">If no file exists at file path <paramref name="fileName"/>.</exception>
3842
public IniFileReader(string fileName)
3943
{
4044
if (string.IsNullOrEmpty(fileName))
@@ -53,7 +57,7 @@ public IniFileReader(string fileName)
5357
string[] iniFileContent = File.ReadAllLines(fileName);
5458
foreach (string iniFileLine in iniFileContent)
5559
{
56-
if (!string.IsNullOrEmpty(iniFileLine.Trim()) && !iniFileLine.StartsWith(";", StringComparison.OrdinalIgnoreCase))
60+
if (!string.IsNullOrWhiteSpace(iniFileLine) && !iniFileLine.StartsWith(";", StringComparison.OrdinalIgnoreCase))
5761
{
5862
if (iniFileLine.StartsWith("[", StringComparison.OrdinalIgnoreCase) && iniFileLine.EndsWith("]", StringComparison.OrdinalIgnoreCase))
5963
{
@@ -86,21 +90,24 @@ public IniFileReader(string fileName)
8690
/// <summary>
8791
/// Gets a <see cref="ReadOnlyCollection{T}"/> containing the names of the sections in the .INI file.
8892
/// </summary>
89-
public ReadOnlyCollection<string> SectionNames
90-
{
91-
get
92-
{
93-
List<string> keyList = new List<string>(this.iniFileStore.Keys);
94-
return new ReadOnlyCollection<string>(keyList);
95-
}
96-
}
93+
public ReadOnlyCollection<string> SectionNames => new ReadOnlyCollection<string>(new List<string>(this.iniFileStore.Keys));
9794

9895
/// <summary>
9996
/// Gets a value from the .INI file.
10097
/// </summary>
10198
/// <param name="sectionName">The section in which to find the key-value pair.</param>
10299
/// <param name="valueName">The key of the key-value pair.</param>
103100
/// <returns>The value associated with the given section and key.</returns>
101+
/// <exception cref="ArgumentNullException">
102+
/// <para>If <paramref name="sectionName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</para>
103+
/// <para>-or-</para>
104+
/// <para>If <paramref name="valueName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</para>
105+
/// </exception>
106+
/// <exception cref="ArgumentException">
107+
/// <para>If no section named <paramref name="sectionName"/> exists.</para>
108+
/// <para>-or-</para>
109+
///<para>If the section does not contain a value named <paramref name="valueName"/>.</para>
110+
/// </exception>
104111
public string GetValue(string sectionName, string valueName)
105112
{
106113
if (string.IsNullOrEmpty(sectionName))
@@ -117,19 +124,17 @@ public string GetValue(string sectionName, string valueName)
117124

118125
string lowerCaseValueName = valueName.ToUpperInvariant();
119126

120-
if (!this.iniFileStore.ContainsKey(lowerCaseSectionName))
127+
if (!this.iniFileStore.TryGetValue(lowerCaseSectionName, out Dictionary<string, string>? section))
121128
{
122129
throw new ArgumentException("Section does not exist: " + sectionName, nameof(sectionName));
123130
}
124131

125-
Dictionary<string, string> section = this.iniFileStore[lowerCaseSectionName];
126-
127-
if (!section.ContainsKey(lowerCaseValueName))
132+
if (!section.TryGetValue(lowerCaseValueName, out string? value))
128133
{
129134
throw new ArgumentException("Value does not exist: " + valueName, nameof(valueName));
130135
}
131136

132-
return section[lowerCaseValueName];
137+
return value;
133138
}
134139
}
135140
}

0 commit comments

Comments
 (0)