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.

115 lines
5.2 KiB

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