New tests, message update
[mono.git] / mcs / class / System.Web / System.Web.SessionState_2.0 / SessionStateItemCollection.cs
1 //
2 // System.Web.Compilation.SessionStateItemCollection
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_2_0
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
34 using System.IO;
35 using System.Web.Util;
36
37 namespace System.Web.SessionState 
38 {
39         public sealed class SessionStateItemCollection : NameObjectCollectionBase, ISessionStateItemCollection, ICollection, IEnumerable
40         {
41                 bool is_dirty;
42
43                 static bool IsMutable (object o)
44                 {
45                         return (o != null && Type.GetTypeCode(o.GetType()) == TypeCode.Object);
46                 }
47                 
48                 public SessionStateItemCollection ()
49                 {
50                 }
51
52                 internal SessionStateItemCollection (int capacity)
53                         : base (capacity)
54                 {
55                 }
56                 
57                 public bool Dirty {
58                         get { return is_dirty; }
59                         set { is_dirty = value; }
60                 }
61
62                 public object this [int index] {
63                         get {
64                                 object o = BaseGet (index);
65                                 if (IsMutable (o))
66                                         is_dirty = true;
67                                 return o;
68                         }
69                         
70                         set {
71                                 BaseSet (index, value);
72                                 is_dirty = true;
73                         }
74                 }
75                 
76                 public object this [string name] {
77                         get {
78                                 object o = BaseGet (name);
79                                 if (IsMutable (o))
80                                         is_dirty = true;
81                                 return o;
82                         }
83                         
84                         set {
85                                 BaseSet (name, value);
86                                 is_dirty = true;
87                         }
88                 }
89
90                 // Todo: why override this?
91                 public override KeysCollection Keys {
92                         get { return base.Keys; }
93                 }
94
95                 public void Clear ()
96                 {\r
97                         if (Count > 0) {\r
98                                 BaseClear ();\r
99                                 is_dirty = true;\r
100                         }
101                 }
102
103                 public static SessionStateItemCollection Deserialize (BinaryReader reader)
104                 {
105                         int i = reader.ReadInt32 ();
106                         SessionStateItemCollection ret = new SessionStateItemCollection (i);\r
107                         for (; i > 0; i--)\r
108                                 ret [reader.ReadString ()] =\r
109                                         System.Web.Util.AltSerialization.Deserialize (reader);
110
111                         return ret;
112                 }\r
113 \r
114                 public void Serialize (BinaryWriter writer) {\r
115                         writer.Write (Count);\r
116                         foreach (string key in base.Keys) {\r
117                                 writer.Write (key);\r
118                                 System.Web.Util.AltSerialization.Serialize (writer, BaseGet (key));\r
119                         }\r
120                 }
121                 
122                 // Todo: why override this?
123                 public override IEnumerator GetEnumerator ()
124                 {
125                         return base.GetEnumerator ();
126                 }
127
128                 public void Remove (string name)
129                 {
130                         BaseRemove (name);
131                         is_dirty = true;
132                 }
133
134                 public void RemoveAt (int index)
135                 {
136                         BaseRemoveAt (index);
137                         is_dirty = true;
138                 }
139         }
140 }
141 #endif