HttpHeaderValueCollection.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // HttpHeaderValueCollection.cs
  3. //
  4. // Authors:
  5. // Marek Safar <[email protected]>
  6. //
  7. // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections.Generic;
  29. using System.Collections;
  30. namespace System.Net.Http.Headers
  31. {
  32. public sealed class HttpHeaderValueCollection<T> : ICollection<T> where T : class
  33. {
  34. readonly List<T> list;
  35. readonly HttpHeaders headers;
  36. readonly HeaderInfo headerInfo;
  37. internal HttpHeaderValueCollection (HttpHeaders headers, HeaderInfo headerInfo)
  38. {
  39. list = new List<T> ();
  40. this.headers = headers;
  41. this.headerInfo = headerInfo;
  42. }
  43. public int Count {
  44. get {
  45. return list.Count;
  46. }
  47. }
  48. public bool IsReadOnly {
  49. get {
  50. return false;
  51. }
  52. }
  53. public void Add (T item)
  54. {
  55. list.Add (item);
  56. }
  57. public void Clear ()
  58. {
  59. list.Clear ();
  60. }
  61. public bool Contains (T item)
  62. {
  63. return list.Contains (item);
  64. }
  65. public void CopyTo (T[] array, int arrayIndex)
  66. {
  67. list.CopyTo (array, arrayIndex);
  68. }
  69. public void ParseAdd (string input)
  70. {
  71. headers.AddValue (input, headerInfo, false);
  72. }
  73. public bool Remove (T item)
  74. {
  75. return list.Remove (item);
  76. }
  77. public override string ToString ()
  78. {
  79. // This implementation prints different values than
  80. // what .NET does when one of the values is invalid
  81. // But it better represents what is actually hold by
  82. // the collection
  83. return string.Join (", ", list);
  84. }
  85. public bool TryParseAdd (string input)
  86. {
  87. return headers.AddValue (input, headerInfo, true);
  88. }
  89. public IEnumerator<T> GetEnumerator ()
  90. {
  91. return list.GetEnumerator ();
  92. }
  93. IEnumerator IEnumerable.GetEnumerator ()
  94. {
  95. return GetEnumerator ();
  96. }
  97. internal T Find (Predicate<T> predicate)
  98. {
  99. return list.Find (predicate);
  100. }
  101. internal void Remove (Predicate<T> predicate)
  102. {
  103. T item = Find (predicate);
  104. if (item != null)
  105. Remove (item);
  106. }
  107. }
  108. }