QueueExtension.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Copyright (c) 2013, 2014 Paolo Patierno
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License v1.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. http://www.eclipse.org/legal/epl-v10.html
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. Contributors:
  11. Paolo Patierno - initial API and implementation and/or initial documentation
  12. */
  13. using System;
  14. using System.Collections;
  15. namespace uPLibrary.Networking.M2Mqtt.Utility
  16. {
  17. /// <summary>
  18. /// Extension class for a Queue
  19. /// </summary>
  20. internal static class QueueExtension
  21. {
  22. /// <summary>
  23. /// Predicate for searching inside a queue
  24. /// </summary>
  25. /// <param name="item">Item of the queue</param>
  26. /// <returns>Result of predicate</returns>
  27. internal delegate bool QueuePredicate(object item);
  28. /// <summary>
  29. /// Get (without removing) an item from queue based on predicate
  30. /// </summary>
  31. /// <param name="queue">Queue in which to search</param>
  32. /// <param name="predicate">Predicate to verify to get item</param>
  33. /// <returns>Item matches the predicate</returns>
  34. internal static object Get(this Queue queue, QueuePredicate predicate)
  35. {
  36. foreach (var item in queue)
  37. {
  38. if (predicate(item))
  39. return item;
  40. }
  41. return null;
  42. }
  43. }
  44. }