CollectionUtilities.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Collections
  7. {
  8. public abstract class CollectionUtilities
  9. {
  10. public static void CollectMatches<T>(ICollection<T> matches, ISelector<T> selector,
  11. IEnumerable<IStore<T>> stores)
  12. {
  13. if (matches == null)
  14. throw new ArgumentNullException(nameof(matches));
  15. if (stores == null)
  16. return;
  17. foreach (var store in stores)
  18. {
  19. if (store == null)
  20. continue;
  21. foreach (T match in store.EnumerateMatches(selector))
  22. {
  23. matches.Add(match);
  24. }
  25. }
  26. }
  27. public static IStore<T> CreateStore<T>(IEnumerable<T> contents)
  28. {
  29. return new StoreImpl<T>(contents);
  30. }
  31. public static T GetValueOrKey<T>(IDictionary<T, T> d, T k)
  32. {
  33. return d.TryGetValue(k, out var v) ? v : k;
  34. }
  35. public static V GetValueOrNull<K, V>(IDictionary<K, V> d, K k)
  36. where V : class
  37. {
  38. return d.TryGetValue(k, out var v) ? v : null;
  39. }
  40. public static IEnumerable<T> Proxy<T>(IEnumerable<T> e)
  41. {
  42. return new EnumerableProxy<T>(e);
  43. }
  44. public static ICollection<T> ReadOnly<T>(ICollection<T> c)
  45. {
  46. return new ReadOnlyCollectionProxy<T>(c);
  47. }
  48. public static IDictionary<K, V> ReadOnly<K, V>(IDictionary<K, V> d)
  49. {
  50. return new ReadOnlyDictionaryProxy<K, V>(d);
  51. }
  52. public static IList<T> ReadOnly<T>(IList<T> l)
  53. {
  54. return new ReadOnlyListProxy<T>(l);
  55. }
  56. public static ISet<T> ReadOnly<T>(ISet<T> s)
  57. {
  58. return new ReadOnlySetProxy<T>(s);
  59. }
  60. public static bool Remove<K, V>(IDictionary<K, V> d, K k, out V v)
  61. {
  62. if (!d.TryGetValue(k, out v))
  63. return false;
  64. d.Remove(k);
  65. return true;
  66. }
  67. public static T RequireNext<T>(IEnumerator<T> e)
  68. {
  69. if (!e.MoveNext())
  70. throw new InvalidOperationException();
  71. return e.Current;
  72. }
  73. public static string ToString<T>(IEnumerable<T> c)
  74. {
  75. IEnumerator<T> e = c.GetEnumerator();
  76. if (!e.MoveNext())
  77. return "[]";
  78. StringBuilder sb = new StringBuilder("[");
  79. sb.Append(e.Current);
  80. while (e.MoveNext())
  81. {
  82. sb.Append(", ");
  83. sb.Append(e.Current);
  84. }
  85. sb.Append(']');
  86. return sb.ToString();
  87. }
  88. }
  89. }
  90. #pragma warning restore
  91. #endif