2003-02-04 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System / System.Net / CookieCollection.cs
1 //\r
2 // System.Net.CookieCollection\r
3 //\r
4 // Author:\r
5 //   Lawrence Pit (loz@cable.a2000.nl)\r
6 //\r
7 \r
8 using System;\r
9 using System.Collections;\r
10 using System.Runtime.Serialization;\r
11 \r
12 namespace System.Net \r
13 {\r
14         [Serializable]\r
15         public class CookieCollection : ICollection, IEnumerable\r
16         {\r
17                 private ArrayList list = new ArrayList ();\r
18                 \r
19                 // ctor\r
20                 public CookieCollection () \r
21                 {\r
22                 }\r
23 \r
24                 // ICollection\r
25 \r
26                 public int Count {\r
27                         get { return list.Count; }\r
28                 }\r
29 \r
30                 public bool IsSynchronized {\r
31                         get { return false; }\r
32                 }\r
33 \r
34                 public Object SyncRoot {\r
35                         get { return this; }\r
36                 }\r
37 \r
38                 public void CopyTo (Array array, int arrayIndex)\r
39                 {\r
40                         list.CopyTo (array, arrayIndex);\r
41                 }\r
42 \r
43 \r
44                 // IEnumerable\r
45 \r
46                 public IEnumerator GetEnumerator ()\r
47                 {\r
48                         return list.GetEnumerator ();\r
49                 }\r
50                 \r
51                 \r
52                 // This\r
53                 \r
54                 // LAMESPEC: So how is one supposed to create a writable CookieCollection \r
55                 // instance?? We simply ignore this property, as this collection is always\r
56                 // writable.\r
57                 public bool IsReadOnly {\r
58                         get { return true; }\r
59                 }               \r
60                 \r
61                 // LAMESPEC: Which exception should we throw when the read only \r
62                 // property is set to true??\r
63                 public void Add (Cookie cookie) \r
64                 {\r
65                         if (cookie == null)\r
66                                 throw new ArgumentNullException ("cookie");\r
67                         int pos = list.IndexOf (cookie);\r
68                         if (pos == -1)\r
69                                 list.Add (cookie);\r
70                         else \r
71                                 list [pos] = cookie;\r
72                 }               \r
73                 \r
74                 // LAMESPEC: Which exception should we throw when the read only \r
75                 // property is set to true??\r
76                 public void Add (CookieCollection cookies) \r
77                 {\r
78                         if (cookies == null)\r
79                                 throw new ArgumentNullException ("cookies");\r
80                                 \r
81                         IEnumerator enumerator = cookies.list.GetEnumerator ();\r
82                         while (enumerator.MoveNext ())\r
83                                 Add ((Cookie) enumerator.Current);\r
84                 }\r
85                 \r
86                 public Cookie this [int index] {\r
87                         get {\r
88                                 if (index < 0 || index >= list.Count)\r
89                                         throw new ArgumentOutOfRangeException ("index");\r
90                                 return (Cookie) list [index];\r
91                         }\r
92                 }               \r
93                                 \r
94                 public Cookie this [string name] {\r
95                         get {\r
96                                 lock (this) {\r
97                                         IEnumerator enumerator = list.GetEnumerator ();\r
98                                         while (enumerator.MoveNext ())          \r
99                                                 if (String.Compare (((Cookie) enumerator.Current).Name, name, true) == 0)\r
100                                                         return (Cookie) enumerator.Current;\r
101                                 }\r
102                                 return null;\r
103                         }\r
104                 }               \r
105                 \r
106 \r
107         } // CookieCollection\r
108 \r
109 } // System.Net\r
110 \r