using System; using System.Diagnostics; using System.Collections.Generic; namespace MadMilkman.Ini { /// /// Represents a class of mapped s and their results, used in methods. /// /// /// can be accessed through property. /// Mapped value results have priority over parsing the value. /// For more information see IniKey's Value Parsing. /// /// IniKey's Value Parsing public sealed class IniValueMappings { [DebuggerBrowsable(DebuggerBrowsableState.Never)] private static readonly Predicate MappedTypeVerifier = IniKey.IsSupportedValueType; private readonly IDictionary mappings; internal IniValueMappings() { this.mappings = new Dictionary(StringComparer.OrdinalIgnoreCase); } /// /// Adds a new mapping of to resulting object of parse methods. /// /// The key's value. /// The object that represents parsed . /// Type of the object that represents parsed . /// /// The key's value cannot be . /// The mapped result's type must be one of the supported types for parsing, see the remarks of method. /// Collection cannot contain multiple entries of same key's value, value comparison is case-insensitive. /// public void Add(string value, T mappedResult) { if (value == null) throw new ArgumentNullException("value"); if (this.Contains(value) || !IniValueMappings.MappedTypeVerifier(typeof(T))) throw new InvalidOperationException(); this.mappings.Add(value, mappedResult); } /// /// Determines whether the collection contains a mapping for a specified key's value. /// /// The key's value to locate in the collection. /// if the collection contains a mapping for a specified key's value. public bool Contains(string value) { return this.mappings.ContainsKey(value); } /// /// Removes a mapping for a specified key's value in the collection. /// /// The key's value to remove in the collection. /// if a mapping for a specified key's value is successfully found and removed. public bool Remove(string value) { return this.mappings.Remove(value); } internal bool TryGetResult(string value, out T result) { if (!string.IsNullOrEmpty(value)) { object mappedResult; if (this.mappings.TryGetValue(value, out mappedResult) && mappedResult.GetType() == typeof(T)) { result = (T)mappedResult; return true; } } result = default(T); return false; } } }