Max.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive.Linq
  7. {
  8. partial class AsyncObservable
  9. {
  10. public static IAsyncObservable<TSource> Max<TSource>(IAsyncObservable<TSource> source)
  11. {
  12. if (source == null)
  13. throw new ArgumentNullException(nameof(source));
  14. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Max(observer)));
  15. }
  16. public static IAsyncObservable<TSource> Max<TSource>(IAsyncObservable<TSource> source, IComparer<TSource> comparer)
  17. {
  18. if (source == null)
  19. throw new ArgumentNullException(nameof(source));
  20. if (comparer == null)
  21. throw new ArgumentNullException(nameof(comparer));
  22. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Max(observer, comparer)));
  23. }
  24. }
  25. partial class AsyncObserver
  26. {
  27. public static IAsyncObserver<TSource> Max<TSource>(IAsyncObserver<TSource> observer)
  28. {
  29. if (observer == null)
  30. throw new ArgumentNullException(nameof(observer));
  31. return Max(observer, Comparer<TSource>.Default);
  32. }
  33. public static IAsyncObserver<TSource> Max<TSource>(IAsyncObserver<TSource> observer, IComparer<TSource> comparer)
  34. {
  35. if (observer == null)
  36. throw new ArgumentNullException(nameof(observer));
  37. if (comparer == null)
  38. throw new ArgumentNullException(nameof(comparer));
  39. var max = default(TSource);
  40. var found = false;
  41. return Create<TSource>(
  42. async x =>
  43. {
  44. if (found)
  45. {
  46. bool isGreater;
  47. try
  48. {
  49. isGreater = comparer.Compare(x, max) > 0;
  50. }
  51. catch (Exception ex)
  52. {
  53. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  54. return;
  55. }
  56. if (isGreater)
  57. {
  58. max = x;
  59. }
  60. }
  61. else
  62. {
  63. max = x;
  64. found = true;
  65. }
  66. },
  67. observer.OnErrorAsync,
  68. async () =>
  69. {
  70. if (!found)
  71. {
  72. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  73. }
  74. else
  75. {
  76. await observer.OnNextAsync(max).ConfigureAwait(false);
  77. await observer.OnCompletedAsync().ConfigureAwait(false);
  78. }
  79. }
  80. );
  81. }
  82. public static IAsyncObserver<int> MaxInt32(IAsyncObserver<int> observer)
  83. {
  84. if (observer == null)
  85. throw new ArgumentNullException(nameof(observer));
  86. var max = 0;
  87. var found = false;
  88. return Create<int>(
  89. x =>
  90. {
  91. if (found)
  92. {
  93. if (x > max)
  94. {
  95. max = x;
  96. }
  97. }
  98. else
  99. {
  100. max = x;
  101. found = true;
  102. }
  103. return Task.CompletedTask;
  104. },
  105. observer.OnErrorAsync,
  106. async () =>
  107. {
  108. if (!found)
  109. {
  110. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  111. }
  112. else
  113. {
  114. await observer.OnNextAsync(max).ConfigureAwait(false);
  115. await observer.OnCompletedAsync().ConfigureAwait(false);
  116. }
  117. }
  118. );
  119. }
  120. public static IAsyncObserver<long> MaxInt64(IAsyncObserver<long> observer)
  121. {
  122. if (observer == null)
  123. throw new ArgumentNullException(nameof(observer));
  124. var max = 0L;
  125. var found = false;
  126. return Create<long>(
  127. x =>
  128. {
  129. if (found)
  130. {
  131. if (x > max)
  132. {
  133. max = x;
  134. }
  135. }
  136. else
  137. {
  138. max = x;
  139. found = true;
  140. }
  141. return Task.CompletedTask;
  142. },
  143. observer.OnErrorAsync,
  144. async () =>
  145. {
  146. if (!found)
  147. {
  148. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  149. }
  150. else
  151. {
  152. await observer.OnNextAsync(max).ConfigureAwait(false);
  153. await observer.OnCompletedAsync().ConfigureAwait(false);
  154. }
  155. }
  156. );
  157. }
  158. public static IAsyncObserver<float> MaxSingle(IAsyncObserver<float> observer)
  159. {
  160. if (observer == null)
  161. throw new ArgumentNullException(nameof(observer));
  162. var max = 0.0f;
  163. var found = false;
  164. return Create<float>(
  165. x =>
  166. {
  167. if (found)
  168. {
  169. if (x > max || double.IsNaN(x))
  170. {
  171. max = x;
  172. }
  173. }
  174. else
  175. {
  176. max = x;
  177. found = true;
  178. }
  179. return Task.CompletedTask;
  180. },
  181. observer.OnErrorAsync,
  182. async () =>
  183. {
  184. if (!found)
  185. {
  186. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  187. }
  188. else
  189. {
  190. await observer.OnNextAsync(max).ConfigureAwait(false);
  191. await observer.OnCompletedAsync().ConfigureAwait(false);
  192. }
  193. }
  194. );
  195. }
  196. public static IAsyncObserver<double> MaxDouble(IAsyncObserver<double> observer)
  197. {
  198. if (observer == null)
  199. throw new ArgumentNullException(nameof(observer));
  200. var max = 0.0;
  201. var found = false;
  202. return Create<double>(
  203. x =>
  204. {
  205. if (found)
  206. {
  207. if (x > max || double.IsNaN(x))
  208. {
  209. max = x;
  210. }
  211. }
  212. else
  213. {
  214. max = x;
  215. found = true;
  216. }
  217. return Task.CompletedTask;
  218. },
  219. observer.OnErrorAsync,
  220. async () =>
  221. {
  222. if (!found)
  223. {
  224. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  225. }
  226. else
  227. {
  228. await observer.OnNextAsync(max).ConfigureAwait(false);
  229. await observer.OnCompletedAsync().ConfigureAwait(false);
  230. }
  231. }
  232. );
  233. }
  234. public static IAsyncObserver<decimal> MaxDecimal(IAsyncObserver<decimal> observer)
  235. {
  236. if (observer == null)
  237. throw new ArgumentNullException(nameof(observer));
  238. var max = 0m;
  239. var found = false;
  240. return Create<decimal>(
  241. x =>
  242. {
  243. if (found)
  244. {
  245. if (x > max)
  246. {
  247. max = x;
  248. }
  249. }
  250. else
  251. {
  252. max = x;
  253. found = true;
  254. }
  255. return Task.CompletedTask;
  256. },
  257. observer.OnErrorAsync,
  258. async () =>
  259. {
  260. if (!found)
  261. {
  262. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  263. }
  264. else
  265. {
  266. await observer.OnNextAsync(max).ConfigureAwait(false);
  267. await observer.OnCompletedAsync().ConfigureAwait(false);
  268. }
  269. }
  270. );
  271. }
  272. public static IAsyncObserver<int?> MaxNullableInt32(IAsyncObserver<int?> observer)
  273. {
  274. if (observer == null)
  275. throw new ArgumentNullException(nameof(observer));
  276. var max = default(int?);
  277. return Create<int?>(
  278. x =>
  279. {
  280. if (max == null || x > max)
  281. {
  282. max = x;
  283. }
  284. return Task.CompletedTask;
  285. },
  286. observer.OnErrorAsync,
  287. async () =>
  288. {
  289. await observer.OnNextAsync(max).ConfigureAwait(false);
  290. await observer.OnCompletedAsync().ConfigureAwait(false);
  291. }
  292. );
  293. }
  294. public static IAsyncObserver<long?> MaxNullableInt64(IAsyncObserver<long?> observer)
  295. {
  296. if (observer == null)
  297. throw new ArgumentNullException(nameof(observer));
  298. var max = default(long?);
  299. return Create<long?>(
  300. x =>
  301. {
  302. if (max == null || x > max)
  303. {
  304. max = x;
  305. }
  306. return Task.CompletedTask;
  307. },
  308. observer.OnErrorAsync,
  309. async () =>
  310. {
  311. await observer.OnNextAsync(max).ConfigureAwait(false);
  312. await observer.OnCompletedAsync().ConfigureAwait(false);
  313. }
  314. );
  315. }
  316. public static IAsyncObserver<float?> MaxNullableSingle(IAsyncObserver<float?> observer)
  317. {
  318. if (observer == null)
  319. throw new ArgumentNullException(nameof(observer));
  320. var max = default(float?);
  321. return Create<float?>(
  322. x =>
  323. {
  324. if (x != null && (max == null || x > max || double.IsNaN(x.Value)))
  325. {
  326. max = x;
  327. }
  328. return Task.CompletedTask;
  329. },
  330. observer.OnErrorAsync,
  331. async () =>
  332. {
  333. await observer.OnNextAsync(max).ConfigureAwait(false);
  334. await observer.OnCompletedAsync().ConfigureAwait(false);
  335. }
  336. );
  337. }
  338. public static IAsyncObserver<double?> MaxNullableDouble(IAsyncObserver<double?> observer)
  339. {
  340. if (observer == null)
  341. throw new ArgumentNullException(nameof(observer));
  342. var max = default(double?);
  343. return Create<double?>(
  344. x =>
  345. {
  346. if (x != null && (max == null || x > max || double.IsNaN(x.Value)))
  347. {
  348. max = x;
  349. }
  350. return Task.CompletedTask;
  351. },
  352. observer.OnErrorAsync,
  353. async () =>
  354. {
  355. await observer.OnNextAsync(max).ConfigureAwait(false);
  356. await observer.OnCompletedAsync().ConfigureAwait(false);
  357. }
  358. );
  359. }
  360. public static IAsyncObserver<decimal?> MaxNullableDecimal(IAsyncObserver<decimal?> observer)
  361. {
  362. if (observer == null)
  363. throw new ArgumentNullException(nameof(observer));
  364. var max = default(decimal?);
  365. return Create<decimal?>(
  366. x =>
  367. {
  368. if (max == null || x > max)
  369. {
  370. max = x;
  371. }
  372. return Task.CompletedTask;
  373. },
  374. observer.OnErrorAsync,
  375. async () =>
  376. {
  377. await observer.OnNextAsync(max).ConfigureAwait(false);
  378. await observer.OnCompletedAsync().ConfigureAwait(false);
  379. }
  380. );
  381. }
  382. }
  383. }