View Javadoc
1   package org.apache.commons.jcs3.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.jcs3.engine.behavior.ICacheElement;
31  import org.apache.commons.jcs3.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<>();
53  
54      /** List of request ids. */
55      public List<Long> updateRequestIdList = new ArrayList<>();
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( final String cacheName, final K key, final 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( final String cacheName )
82      {
83          return new HashSet<>();
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( final String cacheName, final K key, final 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( final String cacheName, final 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( final ICacheElement<K, V> item, final 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( final String cacheName )
134     {
135     }
136 
137     /**
138      * @param cacheName
139      * @param key
140      * @return null
141      */
142     @Override
143     public ICacheElement<K, V> get( final String cacheName, final K key )
144     {
145         return get( cacheName, key, 0 );
146     }
147 
148     /**
149      * Do nothing.
150      */
151     @Override
152     public void release()
153     {
154     }
155 
156     /**
157      * Set the last remove key.
158      * <p>
159      * @param cacheName
160      * @param key
161      */
162     @Override
163     public void remove( final String cacheName, final K key )
164     {
165         lastRemoveKey = key;
166     }
167 
168     /**
169      * Set the last remove all cache name.
170      * <p>
171      * @param cacheName
172      */
173     @Override
174     public void removeAll( final String cacheName )
175     {
176         lastRemoveAllCacheName = cacheName;
177     }
178 
179     /**
180      * Set the last update item.
181      * <p>
182      * @param item
183      */
184     @Override
185     public void update( final ICacheElement<K, V> item )
186     {
187         lastUpdate = item;
188     }
189 
190     /**
191      * @param cacheName
192      * @param keys
193      * @param requesterId - identity of requester
194      * @return empty map
195      */
196     @Override
197     public Map<K, ICacheElement<K, V>> getMultiple( final String cacheName, final Set<K> keys, final long requesterId )
198     {
199         lastGetMultipleKeys = keys;
200         return new HashMap<>();
201     }
202 
203     /**
204      * @param cacheName
205      * @param keys
206      * @return empty map
207      */
208     @Override
209     public Map<K, ICacheElement<K, V>> getMultiple( final String cacheName, final Set<K> keys )
210     {
211         return getMultiple( cacheName, keys, 0 );
212     }
213 
214     /**
215      * Returns an empty map. Zombies have no internal data.
216      * <p>
217      * @param cacheName
218      * @param pattern
219      * @return an empty map
220      * @throws IOException
221      */
222     @Override
223     public Map<K, ICacheElement<K, V>> getMatching( final String cacheName, final String pattern )
224         throws IOException
225     {
226         return getMatching( cacheName, pattern, 0 );
227     }
228 
229     /**
230      * @param cacheName
231      * @param pattern
232      * @param requesterId
233      * @return Map
234      * @throws IOException
235      */
236     @Override
237     public Map<K, ICacheElement<K, V>> getMatching( final String cacheName, final String pattern, final long requesterId )
238         throws IOException
239     {
240         lastGetMatchingPattern = pattern;
241         return new HashMap<>();
242     }
243 }