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.

122 lines
5.2 KiB

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