NameValueList.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. #pragma warning disable CS8603 // Possible null reference return.
  7. #pragma warning disable CS8601 // Possible null reference assignment.
  8. namespace EasyDevCore.Common.Wrapper
  9. {
  10. /// <summary>
  11. /// Defines common methods for INameValueList and IReadOnlyNameValueList.
  12. /// </summary>
  13. public interface INameValueListBase<TValue>
  14. {
  15. /// <summary>
  16. /// Returns the first Value of the given Name if one exists, otherwise null or default value.
  17. /// </summary>
  18. TValue FirstOrDefault(string name);
  19. /// <summary>
  20. /// Gets the first Value of the given Name, if one exists.
  21. /// </summary>
  22. /// <returns>true if any item of the given name is found, otherwise false.</returns>
  23. bool TryGetFirst(string name, out TValue value);
  24. /// <summary>
  25. /// Gets all Values of the given Name.
  26. /// </summary>
  27. IEnumerable<TValue> GetAll(string name);
  28. /// <summary>
  29. /// True if any items with the given Name exist.
  30. /// </summary>
  31. bool Contains(string name);
  32. /// <summary>
  33. /// True if any item with the given Name and Value exists.
  34. /// </summary>
  35. bool Contains(string name, TValue value);
  36. }
  37. /// <summary>
  38. /// Defines an ordered collection of Name/Value pairs where duplicate names are allowed but aren't typical.
  39. /// </summary>
  40. public interface INameValueList<TValue> : IList<(string Name, TValue Value)>, INameValueListBase<TValue>
  41. {
  42. /// <summary>
  43. /// Adds a new Name/Value pair.
  44. /// </summary>
  45. void Add(string name, TValue value);
  46. /// <summary>
  47. /// Replaces the first occurrence of the given Name with the given Value and removes any others,
  48. /// or adds a new Name/Value pair if none exist.
  49. /// </summary>
  50. void AddOrReplace(string name, TValue value);
  51. /// <summary>
  52. /// Removes all items of the given Name.
  53. /// </summary>
  54. /// <returns>true if any item of the given name is found, otherwise false.</returns>
  55. bool Remove(string name);
  56. }
  57. /// <summary>
  58. /// Defines a read-only ordered collection of Name/Value pairs where duplicate names are allowed but aren't typical.
  59. /// </summary>
  60. public interface IReadOnlyNameValueList<TValue> : IReadOnlyList<(string Name, TValue Value)>, INameValueListBase<TValue>
  61. {
  62. }
  63. /// <summary>
  64. /// An ordered collection of Name/Value pairs where duplicate names are allowed but aren't typical.
  65. /// Useful for things where a dictionary would work great if not for those pesky edge cases (headers, cookies, etc).
  66. /// </summary>
  67. public class NameValueList<TValue> : List<(string Name, TValue Value)>, INameValueList<TValue>, IReadOnlyNameValueList<TValue>
  68. {
  69. private StringComparison _ComparisonType;
  70. /// <summary>
  71. /// Instantiates a new empty NameValueList.
  72. /// </summary>
  73. public NameValueList(StringComparison comparisonType)
  74. {
  75. _ComparisonType = comparisonType;
  76. }
  77. /// <summary>
  78. /// Instantiates a new NameValueList with the Name/Value pairs provided.
  79. /// </summary>
  80. public NameValueList(IEnumerable<(string Name, TValue Value)> items, StringComparison comparisonType)
  81. {
  82. _ComparisonType = comparisonType;
  83. AddRange(items);
  84. }
  85. /// <inheritdoc />
  86. public void Add(string name, TValue value) => Add((name, value));
  87. /// <inheritdoc />
  88. public void AddOrReplace(string name, TValue value)
  89. {
  90. var i = 0;
  91. var replaced = false;
  92. while (i < this.Count)
  93. {
  94. if (!string.Equals(this[i].Name, name, _ComparisonType))
  95. i++;
  96. else if (replaced)
  97. this.RemoveAt(i);
  98. else
  99. {
  100. this[i] = (name, value);
  101. replaced = true;
  102. i++;
  103. }
  104. }
  105. if (!replaced)
  106. this.Add(name, value);
  107. }
  108. /// <inheritdoc />
  109. public bool Remove(string name) => RemoveAll(x => string.Equals(x.Name, name, _ComparisonType)) > 0;
  110. /// <inheritdoc />
  111. public TValue FirstOrDefault(string name) => GetAll(name).FirstOrDefault();
  112. /// <inheritdoc />
  113. public bool TryGetFirst(string name, out TValue value)
  114. {
  115. foreach (var v in GetAll(name))
  116. {
  117. value = v;
  118. return true;
  119. }
  120. value = default;
  121. return false;
  122. }
  123. /// <inheritdoc />
  124. public IEnumerable<TValue> GetAll(string name) => this
  125. .Where(x => string.Equals(x.Name, name, _ComparisonType))
  126. .Select(x => x.Value);
  127. /// <inheritdoc />
  128. public bool Contains(string name) => this.Any(x => string.Equals(x.Name, name, _ComparisonType));
  129. /// <inheritdoc />
  130. public bool Contains(string name, TValue value) => Contains((name, value));
  131. }
  132. }