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.

121 lines
5.2 KiB

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