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.

124 lines
5.3 KiB

  1. #nullable enable
  2. using IPA.Utilities;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace IPA.Config.Data
  8. {
  9. /// <summary>
  10. /// A list of <see cref="Value"/>s for serialization by an <see cref="IConfigProvider"/>.
  11. /// Use <see cref="Value.List"/> or <see cref="Value.From(IEnumerable{Value})"/> to create.
  12. /// </summary>
  13. public sealed class List : Value, IList<Value?>
  14. {
  15. private readonly List<Value?> values = new();
  16. internal List() { }
  17. /// <summary>
  18. /// Gets the value at the given index in this <see cref="List"/>.
  19. /// </summary>
  20. /// <param name="index">the index to retrieve the <see cref="Value"/> at</param>
  21. /// <returns>the <see cref="Value"/> at <paramref name="index"/></returns>
  22. /// <seealso cref="IList{T}.this[int]"/>
  23. public Value? this[int index] { get => values[index]; set => values[index] = value; }
  24. /// <summary>
  25. /// Gets the number of elements in the <see cref="List"/>.
  26. /// </summary>
  27. /// <seealso cref="ICollection{T}.Count"/>
  28. public int Count => values.Count;
  29. bool ICollection<Value?>.IsReadOnly => ((IList<Value?>)values).IsReadOnly;
  30. /// <summary>
  31. /// Adds a <see cref="Value"/> to the end of this <see cref="List"/>.
  32. /// </summary>
  33. /// <param name="item">the <see cref="Value"/> to add</param>
  34. /// <seealso cref="ICollection{T}.Add(T)"/>
  35. public void Add(Value? item) => values.Add(item);
  36. /// <summary>
  37. /// Adds a range of <see cref="Value"/>s to the end of this <see cref="List"/>.
  38. /// </summary>
  39. /// <param name="vals">the range of <see cref="Value"/>s to add</param>
  40. public void AddRange(IEnumerable<Value?> vals)
  41. {
  42. if (vals is null) throw new ArgumentNullException(nameof(vals));
  43. foreach (var val in vals) Add(val);
  44. }
  45. /// <summary>
  46. /// Clears the <see cref="List"/>.
  47. /// </summary>
  48. /// <seealso cref="ICollection{T}.Clear"/>
  49. public void Clear() => values.Clear();
  50. /// <summary>
  51. /// Checks if the <see cref="List"/> contains a certian item.
  52. /// </summary>
  53. /// <param name="item">the <see cref="Value"/> to check for</param>
  54. /// <returns><see langword="true"/> if the item was founc, otherwise <see langword="false"/></returns>
  55. /// <seealso cref="ICollection{T}.Contains(T)"/>
  56. public bool Contains(Value? item) => values.Contains(item);
  57. /// <summary>
  58. /// Copies the <see cref="Value"/>s in the <see cref="List"/> to the <see cref="Array"/> in <paramref name="array"/>.
  59. /// </summary>
  60. /// <param name="array">the <see cref="Array"/> to copy to</param>
  61. /// <param name="arrayIndex">the starting index to copy to</param>
  62. /// <seealso cref="ICollection{T}.CopyTo(T[], int)"/>
  63. public void CopyTo(Value?[] array, int arrayIndex) => values.CopyTo(array, arrayIndex);
  64. /// <summary>
  65. /// Gets an enumerator to enumerate the <see cref="List"/>.
  66. /// </summary>
  67. /// <returns>an <see cref="IEnumerator{T}"/> for this <see cref="List"/></returns>
  68. /// <seealso cref="IEnumerable{T}.GetEnumerator"/>
  69. public IEnumerator<Value?> GetEnumerator() => ((IList<Value?>)values).GetEnumerator();
  70. /// <summary>
  71. /// Gets the index that a given <see cref="Value"/> is in the <see cref="List"/>.
  72. /// </summary>
  73. /// <param name="item">the <see cref="Value"/> to search for</param>
  74. /// <returns>the index that the <paramref name="item"/> was at, or -1.</returns>
  75. /// <seealso cref="IList{T}.IndexOf(T)"/>
  76. public int IndexOf(Value? item) => values.IndexOf(item);
  77. /// <summary>
  78. /// Inserts a <see cref="Value"/> at an index.
  79. /// </summary>
  80. /// <param name="index">the index to insert at</param>
  81. /// <param name="item">the <see cref="Value"/> to insert</param>
  82. /// <seealso cref="IList{T}.Insert(int, T)"/>
  83. public void Insert(int index, Value? item) => values.Insert(index, item);
  84. /// <summary>
  85. /// Removes a <see cref="Value"/> from the <see cref="List"/>.
  86. /// </summary>
  87. /// <param name="item">the <see cref="Value"/> to remove</param>
  88. /// <returns><see langword="true"/> if the item was removed, <see langword="false"/> otherwise</returns>
  89. /// <seealso cref="ICollection{T}.Remove(T)"/>
  90. public bool Remove(Value? item) => values.Remove(item);
  91. /// <summary>
  92. /// Removes a <see cref="Value"/> at an index.
  93. /// </summary>
  94. /// <param name="index">the index to remove a <see cref="Value"/> at</param>
  95. /// <seealso cref="IList{T}.RemoveAt(int)"/>
  96. public void RemoveAt(int index) => values.RemoveAt(index);
  97. /// <summary>
  98. /// Converts this <see cref="Value"/> into a human-readable format.
  99. /// </summary>
  100. /// <returns>a comma-seperated list of the result of <see cref="Value.ToString"/> wrapped in square brackets</returns>
  101. public override string ToString()
  102. => $"[{string.Join(",",this.Select(v => v?.ToString() ?? "null").StrJP())}]";
  103. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  104. }
  105. }