[DatFile] Extract ensuring methods

This commit is contained in:
Matt Nadareski
2017-12-05 12:03:53 -08:00
parent 0a09952eda
commit 27cbf22fc9

View File

@@ -1125,19 +1125,13 @@ namespace SabreTools.Library.DatFiles
{ {
get get
{ {
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
lock (_items) lock (_items)
{ {
// If the key is missing from the dictionary, add it // Ensure the key exists
if (!_items.ContainsKey(key)) EnsureKey(key);
{
_items.Add(key, new List<DatItem>());
}
// Now return the value // Now return the value
return _items[key]; return _items[key];
@@ -1151,19 +1145,13 @@ namespace SabreTools.Library.DatFiles
/// <param name="key">Key in the dictionary to add</param> /// <param name="key">Key in the dictionary to add</param>
public void Add(string key) public void Add(string key)
{ {
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
lock (_items) lock (_items)
{ {
// If the key is missing from the dictionary, add it // Ensure the key exists
if (!_items.ContainsKey(key)) EnsureKey(key);
{
_items.Add(key, new List<DatItem>());
}
} }
} }
@@ -1174,11 +1162,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="value">Value to add to the dictionary</param> /// <param name="value">Value to add to the dictionary</param>
public void Add(string key, DatItem value) public void Add(string key, DatItem value)
{ {
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
// Add the key, if necessary // Add the key, if necessary
Add(key); Add(key);
@@ -1200,11 +1185,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="value">Value to add to the dictionary</param> /// <param name="value">Value to add to the dictionary</param>
public void AddRange(string key, List<DatItem> value) public void AddRange(string key, List<DatItem> value)
{ {
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
// Add the key, if necessary // Add the key, if necessary
Add(key); Add(key);
@@ -1231,11 +1213,8 @@ namespace SabreTools.Library.DatFiles
{ {
bool contains = false; bool contains = false;
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
// If the key is null, we return false since keys can't be null // If the key is null, we return false since keys can't be null
if (key == null) if (key == null)
@@ -1261,11 +1240,8 @@ namespace SabreTools.Library.DatFiles
{ {
bool contains = false; bool contains = false;
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
// If the key is null, we return false since keys can't be null // If the key is null, we return false since keys can't be null
if (key == null) if (key == null)
@@ -1292,11 +1268,8 @@ namespace SabreTools.Library.DatFiles
{ {
get get
{ {
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
lock (_items) lock (_items)
{ {
@@ -1311,11 +1284,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="key">Key in the dictionary to remove</param> /// <param name="key">Key in the dictionary to remove</param>
public void Remove(string key) public void Remove(string key)
{ {
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
// If the key doesn't exist, return // If the key doesn't exist, return
if (!Contains(key)) if (!Contains(key))
@@ -1343,11 +1313,8 @@ namespace SabreTools.Library.DatFiles
/// <param name="value">Value to remove from the dictionary</param> /// <param name="value">Value to remove from the dictionary</param>
public void Remove(string key, DatItem value) public void Remove(string key, DatItem value)
{ {
// If the dictionary is null, create it // Ensure the dictionary is created
if (_items == null) EnsureDictionary();
{
_items = new SortedDictionary<string, List<DatItem>>();
}
// If the key and value doesn't exist, return // If the key and value doesn't exist, return
if (!Contains(key, value)) if (!Contains(key, value))
@@ -1380,6 +1347,31 @@ namespace SabreTools.Library.DatFiles
Remove(key, item); Remove(key, item);
} }
} }
/// <summary>
/// Ensure the items dictionary
/// </summary>
private void EnsureDictionary()
{
// If the dictionary is null, create it
if (_items == null)
{
_items = new SortedDictionary<string, List<DatItem>>();
}
}
/// <summary>
/// Ensure the key exists in the items dictionary
/// </summary>
/// <param name="key">Key to ensure</param>
private void EnsureKey(string key)
{
// If the key is missing from the dictionary, add it
if (!_items.ContainsKey(key))
{
_items.Add(key, new List<DatItem>());
}
}
#endregion #endregion