style
[mono.git] / mcs / class / System.Web / System.Web / HttpCookieCollection.cs
1 // 
2 // System.Web.HttpCookieCollection
3 //
4 // Author:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   (First impl Bob Smith <bob@thestuff.net>)
7 //
8
9 using System;
10 using System.Web;
11 using System.Collections.Specialized;
12
13 namespace System.Web
14 {
15         public sealed class HttpCookieCollection : NameObjectCollectionBase
16         {
17                 HttpCookie [] _AllCookies;
18                 string [] _AllKeys;
19
20                 HttpResponse _Response;
21
22                 internal HttpCookieCollection(HttpResponse Response, bool ReadOnly) : base()
23                 {
24                         _Response = Response;
25                         IsReadOnly = ReadOnly;
26                 }
27
28                 public HttpCookieCollection () { }
29
30                 public string [] AllKeys
31                 {
32                         get {
33                                 if (null == _AllKeys)
34                                         _AllKeys = BaseGetAllKeys ();
35
36                                 return _AllKeys;
37                         }
38                 }
39
40                 public HttpCookie this [int index]
41                 {
42                         get { return Get (index); }
43                 }
44
45                 public HttpCookie this [string name]
46                 {
47                         get { return Get (name); }
48                 }
49
50                 public void Add (HttpCookie cookie)
51                 {
52                         if (null != _Response)
53                                 _Response.GoingToChangeCookieColl ();
54
55                         // empy performance cache
56                         _AllCookies = null;
57                         _AllKeys = null;
58
59                         BaseAdd (cookie.Name, cookie);
60
61                         if (null != _Response)
62                                 _Response.OnCookieAdd (cookie);
63                 }
64
65                 public void Clear ()
66                 {
67                         _AllCookies = null;
68                         _AllKeys = null;
69                         BaseClear ();
70                 }
71
72                 public void CopyTo (Array dest, int index)
73                 {
74                         if (null == _AllCookies) {
75                                 _AllCookies = new HttpCookie [Count];
76
77                                 for (int i = 0; i != Count; i++)
78                                         _AllCookies [i] = Get (i);
79                         }
80
81                         _AllCookies.CopyTo (dest, index);
82                 }
83
84                 public HttpCookie Get (int index)
85                 {
86                         return BaseGet (index) as HttpCookie;
87                 }
88
89                 public HttpCookie Get (string name)
90                 {
91                         HttpCookie oRet = BaseGet (name) as HttpCookie;
92                         if (null == oRet && _Response != null) {
93                                 _AllCookies = null;
94                                 _AllKeys = null;
95
96                                 _Response.GoingToChangeCookieColl ();
97
98                                 oRet = new HttpCookie (name);
99                                 BaseAdd (name, oRet);
100
101                                 _Response.OnCookieAdd (oRet);
102                         }
103
104                         return oRet;
105                 }
106
107                 public string GetKey (int index)
108                 {
109                         return BaseGetKey (index);
110                 }
111
112                 public void Remove (string name)
113                 {
114                         if (null != _Response)
115                                 _Response.GoingToChangeCookieColl ();
116
117                         _AllCookies = null;
118                         _AllKeys = null;
119                         BaseRemove (name);
120
121                         if (null != _Response)
122                                 _Response.ChangedCookieColl ();
123                 }
124
125                 public void Set (HttpCookie cookie)
126                 {
127                         if (null != _Response)
128                                 _Response.GoingToChangeCookieColl ();
129
130                         _AllCookies = null;
131                         _AllKeys = null;
132                         BaseSet (cookie.Name, cookie);
133
134                         if (null != _Response)
135                                 _Response.ChangedCookieColl();
136                 }
137         }
138 }
139