diff --git a/SabreTools.Models/Internal/DatItem.cs b/SabreTools.Models/Internal/DatItem.cs index ea6076ec..8fc6ca2b 100644 --- a/SabreTools.Models/Internal/DatItem.cs +++ b/SabreTools.Models/Internal/DatItem.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Xml.Serialization; using Newtonsoft.Json; @@ -7,7 +6,7 @@ namespace SabreTools.Models.Internal /// /// Format-agnostic representation of item data /// - public class DatItem : Dictionary + public class DatItem : DictionaryBase { #region Common Keys @@ -24,57 +23,5 @@ namespace SabreTools.Models.Internal get => ContainsKey(TypeKey) ? this[TypeKey] as ItemType? : null; set => this[TypeKey] = value; } - - #region Reading Helpers - - /// - /// Read a key as a bool, returning null on error - /// - public bool? ReadBool(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as bool?; - } - - /// - /// Read a key as a double, returning null on error - /// - public double? ReadDouble(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as double?; - } - - /// - /// Read a key as a long, returning null on error - /// - public long? ReadLong(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as long?; - } - - /// - /// Read a key as a string, returning null on error - /// - public string? ReadString(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as string; - } - - #endregion } } \ No newline at end of file diff --git a/SabreTools.Models/Internal/DictionaryBase.cs b/SabreTools.Models/Internal/DictionaryBase.cs new file mode 100644 index 00000000..f8a67e65 --- /dev/null +++ b/SabreTools.Models/Internal/DictionaryBase.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; + +namespace SabreTools.Models.Internal +{ + /// + /// Specialized dictionary base for item types + /// + public abstract class DictionaryBase : Dictionary + { + /// + /// Read a key as the specified type, returning null on error + /// + public T? Read(string key) + { + if (string.IsNullOrWhiteSpace(key)) + return default; + if (!ContainsKey(key)) + return default; + return (T?)this[key]; + } + + /// + /// Read a key as a bool, returning null on error + /// + public bool? ReadBool(string key) => Read(key); + + /// + /// Read a key as a double, returning null on error + /// + public double? ReadDouble(string key) => Read(key); + + /// + /// Read a key as a long, returning null on error + /// + public long? ReadLong(string key) => Read(key); + + /// + /// Read a key as a string, returning null on error + /// + public string? ReadString(string key) => Read(key); + + /// + /// Read a key as a string[], returning null on error + /// + public string[]? ReadStringArray(string key) => Read(key); + } +} \ No newline at end of file diff --git a/SabreTools.Models/Internal/Machine.cs b/SabreTools.Models/Internal/Machine.cs index 6da26f2c..61d0a0cc 100644 --- a/SabreTools.Models/Internal/Machine.cs +++ b/SabreTools.Models/Internal/Machine.cs @@ -1,11 +1,9 @@ -using System.Collections.Generic; - namespace SabreTools.Models.Internal { /// /// Format-agnostic representation of game, machine, and set data /// - public class Machine : Dictionary + public class Machine : DictionaryBase { #region Keys @@ -239,69 +237,5 @@ namespace SabreTools.Models.Internal public const string YearKey = "year"; #endregion - - #region Reading Helpers - - /// - /// Read a key as a bool, returning null on error - /// - public bool? ReadBool(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as bool?; - } - - /// - /// Read a key as a double, returning null on error - /// - public double? ReadDouble(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as double?; - } - - /// - /// Read a key as a long, returning null on error - /// - public long? ReadLong(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as long?; - } - - /// - /// Read a key as a string, returning null on error - /// - public string? ReadString(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as string; - } - - /// - /// Read a key as a string[], returning null on error - /// - public string[]? ReadStringArray(string key) - { - if (string.IsNullOrWhiteSpace(key)) - return null; - if (!ContainsKey(key)) - return null; - return this[key] as string[]; - } - - #endregion } } \ No newline at end of file