Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Util / Cache / SimpleMapCache.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 namespace Mono.Lucene.Net.Util.Cache
21 {
22         
23         /// <summary> Simple cache implementation that uses a HashMap to store (key, value) pairs.
24         /// This cache is not synchronized, use {@link Cache#SynchronizedCache(Cache)}
25         /// if needed.
26         /// </summary>
27         public class SimpleMapCache:Cache
28         {
29                 internal System.Collections.IDictionary map;
30                 
31                 public SimpleMapCache():this(new System.Collections.Hashtable())
32                 {
33                 }
34                 
35                 public SimpleMapCache(System.Collections.IDictionary map)
36                 {
37                         this.map = map;
38                 }
39                 
40                 public override System.Object Get(System.Object key)
41                 {
42                         return map[key];
43                 }
44                 
45                 public override void  Put(System.Object key, System.Object value_Renamed)
46                 {
47                         map[key] = value_Renamed;
48                 }
49                 
50                 public override void  Close()
51                 {
52                         // NOOP
53                 }
54                 
55                 public override bool ContainsKey(System.Object key)
56                 {
57                         return map.Contains(key);
58                 }
59                 
60                 /// <summary> Returns a Set containing all keys in this cache.</summary>
61                 public virtual System.Collections.ICollection KeySet()
62                 {
63                         return map.Keys;
64                 }
65                 
66                 internal override Cache GetSynchronizedCache()
67                 {
68                         return new SynchronizedSimpleMapCache(this);
69                 }
70                 
71                 private class SynchronizedSimpleMapCache:SimpleMapCache
72                 {
73                         internal System.Object mutex;
74                         internal SimpleMapCache cache;
75                         
76                         internal SynchronizedSimpleMapCache(SimpleMapCache cache)
77                         {
78                                 this.cache = cache;
79                                 this.mutex = this;
80                         }
81                         
82                         public override void  Put(System.Object key, System.Object value_Renamed)
83                         {
84                                 lock (mutex)
85                                 {
86                                         cache.Put(key, value_Renamed);
87                                 }
88                         }
89                         
90                         public override System.Object Get(System.Object key)
91                         {
92                                 lock (mutex)
93                                 {
94                                         return cache.Get(key);
95                                 }
96                         }
97                         
98                         public override bool ContainsKey(System.Object key)
99                         {
100                                 lock (mutex)
101                                 {
102                                         return cache.ContainsKey(key);
103                                 }
104                         }
105                         
106                         public override void  Close()
107                         {
108                                 lock (mutex)
109                                 {
110                                         cache.Close();
111                                 }
112                         }
113                         
114                         public override System.Collections.ICollection KeySet()
115                         {
116                                 lock (mutex)
117                                 {
118                                         return cache.KeySet();
119                                 }
120                         }
121                         
122                         internal override Cache GetSynchronizedCache()
123                         {
124                                 return this;
125                         }
126                 }
127         }
128 }