2010-04-24 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Runtime.Caching / Test / Common / PokerObjectCache.cs
1 //
2 // PokerObjectCache.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.Collections.Generic;
30 using System.Reflection;
31 using System.Runtime.Caching;
32
33 namespace MonoTests.Common
34 {
35         class PokerObjectCache : ObjectCache
36         {
37                 Dictionary <string, object> cache;
38
39                 Dictionary<string, object> Cache {
40                         get
41                         {
42                                 if (cache == null)
43                                         cache = new Dictionary<string, object> ();
44                                 return cache;
45                         }
46                 }
47
48                 public string MethodCalled { get; private set; }
49
50                 public override object AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)
51                 {
52                         MethodCalled = "AddOrGetExisting (string key, object value, CacheItemPolicy policy, string regionName = null)";
53                         if (String.IsNullOrEmpty (key) || value == null)
54                                 return null;
55
56                         object item;
57                         if (Cache.TryGetValue (key, out item))
58                                 return item;
59
60                         Cache.Add (key, value);
61                         return null;
62                 }
63
64                 public override CacheItem AddOrGetExisting (CacheItem value, CacheItemPolicy policy)
65                 {
66                         MethodCalled = "AddOrGetExisting (CacheItem value, CacheItemPolicy policy)";
67                         if (value == null)
68                                 return null;
69
70                         object item;
71                         if (Cache.TryGetValue (value.Key, out item))
72                                 return item as CacheItem;
73
74                         Cache.Add (value.Key, value);
75                         return null;
76                 }
77
78                 public override object AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
79                 {
80                         MethodCalled = "AddOrGetExisting (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)";
81                         if (String.IsNullOrEmpty (key) || value == null)
82                                 return null;
83
84                         object item;
85                         if (Cache.TryGetValue (key, out item))
86                                 return item;
87
88                         Cache.Add (key, value);
89                         return null;
90                 }
91
92                 public override bool Contains (string key, string regionName = null)
93                 {
94                         throw new NotImplementedException ();
95                 }
96
97                 public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor (IEnumerable<string> keys, string regionName = null)
98                 {
99                         throw new NotImplementedException ();
100                 }
101
102                 public override DefaultCacheCapabilities DefaultCacheCapabilities
103                 {
104                         get { throw new NotImplementedException (); }
105                 }
106
107                 public override object Get (string key, string regionName = null)
108                 {
109                         throw new NotImplementedException ();
110                 }
111
112                 public override CacheItem GetCacheItem (string key, string regionName = null)
113                 {
114                         throw new NotImplementedException ();
115                 }
116
117                 public override long GetCount (string regionName = null)
118                 {
119                         throw new NotImplementedException ();
120                 }
121
122                 protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator ()
123                 {
124                         throw new NotImplementedException ();
125                 }
126
127                 public override IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)
128                 {
129                         MethodCalled = "IDictionary<string, object> GetValues (IEnumerable<string> keys, string regionName = null)";
130                         var ret = new Dictionary<string, object> ();
131                         if (keys == null)
132                                 return ret;
133                         
134                         Dictionary <string, object> cache = Cache;
135                         if (cache.Count == 0)
136                                 return ret;
137
138                         object value;
139                         foreach (string key in keys) {
140                                 if (!cache.TryGetValue (key, out value))
141                                         continue;
142
143                                 ret.Add (key, value);
144                         }
145
146                         return ret;
147                 }
148
149                 public override string Name
150                 {
151                         get { throw new NotImplementedException (); }
152                 }
153
154                 public override object Remove (string key, string regionName = null)
155                 {
156                         throw new NotImplementedException ();
157                 }
158
159                 public override void Set (string key, object value, CacheItemPolicy policy, string regionName = null)
160                 {
161                         throw new NotImplementedException ();
162                 }
163
164                 public override void Set (CacheItem item, CacheItemPolicy policy)
165                 {
166                         throw new NotImplementedException ();
167                 }
168
169                 public override void Set (string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
170                 {
171                         throw new NotImplementedException ();
172                 }
173
174                 public override object this [string key]
175                 {
176                         get
177                         {
178                                 throw new NotImplementedException ();
179                         }
180                         set
181                         {
182                                 throw new NotImplementedException ();
183                         }
184                 }
185         }
186 }