You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
5.3 KiB

  1. using IPA.Utilities;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace IPA.Config.Data
  6. {
  7. /// <summary>
  8. /// A map of <see cref="string"/> to <see cref="Value"/> for serialization by an <see cref="IConfigProvider"/>.
  9. /// Use <see cref="Value.Map"/> or <see cref="Value.From(IDictionary{string, Value})"/> to create.
  10. /// </summary>
  11. public sealed class Map : Value, IDictionary<string, Value>
  12. {
  13. private readonly Dictionary<string, Value> values = new Dictionary<string, Value>();
  14. internal Map() { }
  15. /// <summary>
  16. /// Accesses the <see cref="Value"/> at <paramref name="key"/> in the map.
  17. /// </summary>
  18. /// <param name="key">the key to get the value associated with</param>
  19. /// <returns>the value associated with the <paramref name="key"/></returns>
  20. /// <seealso cref="IDictionary{TKey, TValue}.this[TKey]"/>
  21. public Value this[string key] { get => values[key]; set => values[key] = value; }
  22. /// <summary>
  23. /// Gets a collection of the keys for the <see cref="Map"/>.
  24. /// </summary>
  25. /// <seealso cref="IDictionary{TKey, TValue}.Keys"/>
  26. public ICollection<string> Keys => values.Keys;
  27. /// <summary>
  28. /// Gets a collection of the values in the <see cref="Map"/>.
  29. /// </summary>
  30. /// <seealso cref="IDictionary{TKey, TValue}.Values"/>
  31. public ICollection<Value> Values => values.Values;
  32. /// <summary>
  33. /// Gets the number of key-value pairs in this <see cref="Map"/>.
  34. /// </summary>
  35. /// <seealso cref="ICollection{T}.Count"/>
  36. public int Count => values.Count;
  37. bool ICollection<KeyValuePair<string, Value>>.IsReadOnly => ((IDictionary<string, Value>)values).IsReadOnly;
  38. /// <summary>
  39. /// Adds a new <see cref="Value"/> with a given key.
  40. /// </summary>
  41. /// <param name="key">the key to put the value at</param>
  42. /// <param name="value">the <see cref="Value"/> to add</param>
  43. /// <seealso cref="IDictionary{TKey, TValue}.Add(TKey, TValue)"/>
  44. public void Add(string key, Value value) => values.Add(key, value);
  45. void ICollection<KeyValuePair<string, Value>>.Add(KeyValuePair<string, Value> item)
  46. => ((IDictionary<string, Value>)values).Add(item);
  47. /// <summary>
  48. /// Clears the <see cref="Map"/> of its key-value pairs.
  49. /// </summary>
  50. /// <seealso cref="ICollection{T}.Clear"/>
  51. public void Clear() => values.Clear();
  52. bool ICollection<KeyValuePair<string, Value>>.Contains(KeyValuePair<string, Value> item)
  53. => ((IDictionary<string, Value>)values).Contains(item);
  54. /// <summary>
  55. /// Checks if the <see cref="Map"/> contains a given <paramref name="key"/>.
  56. /// </summary>
  57. /// <param name="key">the key to check for</param>
  58. /// <returns><see langword="true"/> if the key exists, otherwise <see langword="false"/></returns>
  59. /// <seealso cref="IDictionary{TKey, TValue}.ContainsKey(TKey)"/>
  60. public bool ContainsKey(string key) => values.ContainsKey(key);
  61. void ICollection<KeyValuePair<string, Value>>.CopyTo(KeyValuePair<string, Value>[] array, int arrayIndex)
  62. => ((IDictionary<string, Value>)values).CopyTo(array, arrayIndex);
  63. /// <summary>
  64. /// Enumerates the <see cref="Map"/>'s key-value pairs.
  65. /// </summary>
  66. /// <returns>an <see cref="IEnumerator{T}"/> of key-value pairs in this <see cref="Map"/></returns>
  67. /// <seealso cref="IEnumerable{T}.GetEnumerator()"/>
  68. public IEnumerator<KeyValuePair<string, Value>> GetEnumerator() => values.GetEnumerator();
  69. /// <summary>
  70. /// Removes the object associated with a key in this <see cref="Map"/>.
  71. /// </summary>
  72. /// <param name="key">the key to remove</param>
  73. /// <returns><see langword="true"/> if the key existed, <see langword="false"/> otherwise</returns>
  74. /// <seealso cref="IDictionary{TKey, TValue}.Remove(TKey)"/>
  75. public bool Remove(string key) => values.Remove(key);
  76. bool ICollection<KeyValuePair<string, Value>>.Remove(KeyValuePair<string, Value> item)
  77. => ((IDictionary<string, Value>)values).Remove(item);
  78. /// <summary>
  79. /// Gets the value associated with the specified key.
  80. /// </summary>
  81. /// <param name="key">the key of the value to get</param>
  82. /// <param name="value">the target location of the retrieved object</param>
  83. /// <returns><see langword="true"/> if the key was found and <paramref name="value"/> set, <see langword="false"/> otherwise</returns>
  84. /// <seealso cref="IDictionary{TKey, TValue}.TryGetValue(TKey, out TValue)"/>
  85. public bool TryGetValue(string key, out Value value) => values.TryGetValue(key, out value);
  86. IEnumerator IEnumerable.GetEnumerator() => ((IDictionary<string, Value>)values).GetEnumerator();
  87. /// <summary>
  88. /// Converts this <see cref="Value"/> into a human-readable format.
  89. /// </summary>
  90. /// <returns>a JSON-like set of key-value pairs</returns>
  91. public override string ToString()
  92. => $"{{{string.Join(",", this.Select(p => $"\"{p.Key}\":{p.Value?.ToString() ?? "null"}").StrJP())}}}";
  93. }
  94. }