d433b00baba9f848f57e0c5dbb2bea9aa3decf93
[pithos-ms-client] / trunk%2FPithos.Core%2FAgents%2FCollectionExtensions.cs
1 // -----------------------------------------------------------------------\r
2 // <copyright file="CollectionExtensions.cs" company="GRNET">\r
3 // Copyright 2011-2012 GRNET S.A. All rights reserved.\r
4 // \r
5 // Redistribution and use in source and binary forms, with or\r
6 // without modification, are permitted provided that the following\r
7 // conditions are met:\r
8 // \r
9 //   1. Redistributions of source code must retain the above\r
10 //      copyright notice, this list of conditions and the following\r
11 //      disclaimer.\r
12 // \r
13 //   2. Redistributions in binary form must reproduce the above\r
14 //      copyright notice, this list of conditions and the following\r
15 //      disclaimer in the documentation and/or other materials\r
16 //      provided with the distribution.\r
17 // \r
18 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
19 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
21 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
25 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
26 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
28 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
29 // POSSIBILITY OF SUCH DAMAGE.\r
30 // \r
31 // The views and conclusions contained in the software and\r
32 // documentation are those of the authors and should not be\r
33 // interpreted as representing official policies, either expressed\r
34 // or implied, of GRNET S.A.\r
35 // </copyright>\r
36 // -----------------------------------------------------------------------\r
37 \r
38 using System.Collections.Concurrent;\r
39 \r
40 namespace Pithos.Core.Agents\r
41 {\r
42     using System;\r
43     using System.Collections.Generic;\r
44     using System.Linq;\r
45     using System.Text;\r
46 \r
47     /// <summary>\r
48     /// Extension methods for collections\r
49     /// </summary>\r
50     public static class CollectionExtensions\r
51     {\r
52         /// <summary>\r
53         /// Remove the first message in a queue that matches the predicate\r
54         /// </summary>\r
55         /// <param name="predicate">The condition to match</param>\r
56         /// <remarks>Removes the first message that matches the predicate by dequeing all \r
57         /// messages and re-enqueing all except the first matching message</remarks>\r
58         public static void RemoveFirst<TMessage>(this ConcurrentQueue<TMessage> queue, Func<TMessage, bool> predicate)\r
59         {\r
60             //Can this work? Dequeue all items \r
61             //and then enqueue everything except the filtered items\r
62 \r
63             //Possible problems: \r
64             //* A matching item may be dequeued between one TryDequeue and the next\r
65             var temp = new Queue<TMessage>();\r
66             TMessage message;\r
67             var alreadyFound = false;\r
68             while (queue.TryDequeue(out message))\r
69             {\r
70                 if (!predicate(message) || alreadyFound)\r
71                     temp.Enqueue(message);\r
72                 else\r
73                 {\r
74                     alreadyFound = true;\r
75                 }\r
76             }\r
77 \r
78             queue.AddFromEnumerable(temp);\r
79         }\r
80     }\r
81 }\r