1 package org.apache.commons.jcs.engine;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.apache.commons.jcs.engine.behavior.ICacheElement;
31 import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
32
33 /**
34 * This is a mock impl of the non local cache service.
35 */
36 public class MockCacheServiceNonLocal<K, V>
37 implements ICacheServiceNonLocal<K, V>
38 {
39 /** The key last passed to get */
40 public K lastGetKey;
41
42 /** The pattern last passed to get */
43 public String lastGetMatchingPattern;
44
45 /** The keya last passed to getMatching */
46 public Set<K> lastGetMultipleKeys;
47
48 /** The object that was last passed to update. */
49 public ICacheElement<K, V> lastUpdate;
50
51 /** List of updates. */
52 public List<ICacheElement<K, V>> updateRequestList = new ArrayList<ICacheElement<K,V>>();
53
54 /** List of request ids. */
55 public List<Long> updateRequestIdList = new ArrayList<Long>();
56
57 /** The key that was last passed to remove. */
58 public K lastRemoveKey;
59
60 /** The cache name that was last passed to removeAll. */
61 public String lastRemoveAllCacheName;
62
63 /**
64 * @param cacheName
65 * @param key
66 * @param requesterId - identity of requester
67 * @return null
68 */
69 @Override
70 public ICacheElement<K, V> get( String cacheName, K key, long requesterId )
71 {
72 lastGetKey = key;
73 return null;
74 }
75
76 /**
77 * @param cacheName
78 * @return empty set
79 */
80 @Override
81 public Set<K> getKeySet( String cacheName )
82 {
83 return new HashSet<K>();
84 }
85
86 /**
87 * Set the last remove key.
88 * <p>
89 * @param cacheName
90 * @param key
91 * @param requesterId - identity of requester
92 */
93 @Override
94 public void remove( String cacheName, K key, long requesterId )
95 {
96 lastRemoveKey = key;
97 }
98
99 /**
100 * Set the lastRemoveAllCacheName to the cacheName.
101 * <p>
102 * @param cacheName - region name
103 * @param requesterId - identity of requester
104 * @throws IOException
105 */
106 @Override
107 public void removeAll( String cacheName, long requesterId )
108 throws IOException
109 {
110 lastRemoveAllCacheName = cacheName;
111 }
112
113 /**
114 * Set the last update item.
115 * <p>
116 * @param item
117 * @param requesterId - identity of requester
118 */
119 @Override
120 public void update( ICacheElement<K, V> item, long requesterId )
121 {
122 lastUpdate = item;
123 updateRequestList.add( item );
124 updateRequestIdList.add( Long.valueOf( requesterId ) );
125 }
126
127 /**
128 * Do nothing.
129 * <p>
130 * @param cacheName
131 */
132 @Override
133 public void dispose( String cacheName )
134 {
135 return;
136 }
137
138 /**
139 * @param cacheName
140 * @param key
141 * @return null
142 */
143 @Override
144 public ICacheElement<K, V> get( String cacheName, K key )
145 {
146 return get( cacheName, key, 0 );
147 }
148
149 /**
150 * Do nothing.
151 */
152 @Override
153 public void release()
154 {
155 return;
156 }
157
158 /**
159 * Set the last remove key.
160 * <p>
161 * @param cacheName
162 * @param key
163 */
164 @Override
165 public void remove( String cacheName, K key )
166 {
167 lastRemoveKey = key;
168 }
169
170 /**
171 * Set the last remove all cache name.
172 * <p>
173 * @param cacheName
174 */
175 @Override
176 public void removeAll( String cacheName )
177 {
178 lastRemoveAllCacheName = cacheName;
179 }
180
181 /**
182 * Set the last update item.
183 * <p>
184 * @param item
185 */
186 @Override
187 public void update( ICacheElement<K, V> item )
188 {
189 lastUpdate = item;
190 }
191
192 /**
193 * @param cacheName
194 * @param keys
195 * @param requesterId - identity of requester
196 * @return empty map
197 */
198 @Override
199 public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys, long requesterId )
200 {
201 lastGetMultipleKeys = keys;
202 return new HashMap<K, ICacheElement<K, V>>();
203 }
204
205 /**
206 * @param cacheName
207 * @param keys
208 * @return empty map
209 */
210 @Override
211 public Map<K, ICacheElement<K, V>> getMultiple( String cacheName, Set<K> keys )
212 {
213 return getMultiple( cacheName, keys, 0 );
214 }
215
216 /**
217 * Returns an empty map. Zombies have no internal data.
218 * <p>
219 * @param cacheName
220 * @param pattern
221 * @return an empty map
222 * @throws IOException
223 */
224 @Override
225 public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern )
226 throws IOException
227 {
228 return getMatching( cacheName, pattern, 0 );
229 }
230
231 /**
232 * @param cacheName
233 * @param pattern
234 * @param requesterId
235 * @return Map
236 * @throws IOException
237 */
238 @Override
239 public Map<K, ICacheElement<K, V>> getMatching( String cacheName, String pattern, long requesterId )
240 throws IOException
241 {
242 lastGetMatchingPattern = pattern;
243 return new HashMap<K, ICacheElement<K, V>>();
244 }
245 }