New tests.
[mono.git] / mcs / class / System.Runtime.Caching / System.Runtime.Caching.Configuration / MemoryCacheSettingsCollection.cs
1 //
2 // MemoryCacheSettingsCollection.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Configuration;
30
31 namespace System.Runtime.Caching.Configuration
32 {
33         [ConfigurationCollection (typeof(MemoryCacheElement), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
34         public sealed class MemoryCacheSettingsCollection : ConfigurationElementCollection
35         {
36                 static ConfigurationPropertyCollection properties;
37                 
38                 public override ConfigurationElementCollectionType CollectionType {
39                         get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
40                 }
41
42                 public MemoryCacheElement this[int index] {
43                         get { return BaseGet (index) as MemoryCacheElement; }
44                         set {
45                                 if (BaseGet (index) != null)
46                                         BaseRemoveAt (index);
47                                 BaseAdd (index, value);
48                         }
49                 }
50
51                 public new MemoryCacheElement this[string key] {
52                         get {
53                                 foreach (MemoryCacheElement mce in this) {
54                                         if (String.Compare (key, mce.Name, StringComparison.Ordinal) == 0)
55                                                 return mce;
56                                 }
57
58                                 return null;
59                         }
60                 }
61
62                 protected override ConfigurationPropertyCollection Properties {
63                         get { return properties; }
64                 }
65                 
66                 static MemoryCacheSettingsCollection ()
67                 {
68                         properties = new ConfigurationPropertyCollection ();
69                 }
70                 
71                 public MemoryCacheSettingsCollection ()
72                 {
73                 }
74
75                 public void Add (MemoryCacheElement cache)
76                 {
77                         BaseAdd (cache);
78                 }
79
80                 public void Clear ()
81                 {
82                         BaseClear ();
83                 }
84                 
85                 protected override ConfigurationElement CreateNewElement ()
86                 {
87                         return new MemoryCacheElement ();
88                 }
89
90                 protected override ConfigurationElement CreateNewElement (string elementName)
91                 {
92                         return new MemoryCacheElement (elementName);
93                 }
94                 
95                 protected override object GetElementKey (ConfigurationElement element)
96                 {
97                         if (element == null)
98                                 return null;
99
100                         return ((MemoryCacheElement)element).Name;
101                 }
102
103                 public int IndexOf (MemoryCacheElement cache)
104                 {
105                         if (cache == null)
106                                 return -1;
107
108                         return BaseIndexOf (cache);
109                 }
110
111                 public void Remove (MemoryCacheElement cache)
112                 {
113                         if (cache == null)
114                                 return;
115
116                         BaseRemove (GetElementKey (cache));
117                 }
118
119                 public void RemoveAt (int index)
120                 {
121                         BaseRemoveAt (index);
122                 }
123         }
124 }