View Javadoc
1   package org.apache.commons.jcs3.engine.memory;
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.HashMap;
24  import java.util.Iterator;
25  import java.util.LinkedHashSet;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.commons.jcs3.engine.behavior.ICacheElement;
30  import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes;
31  import org.apache.commons.jcs3.engine.control.CompositeCache;
32  import org.apache.commons.jcs3.engine.memory.behavior.IMemoryCache;
33  import org.apache.commons.jcs3.engine.stats.behavior.IStats;
34  
35  /**
36   * Mock implementation of a memory cache for testing things like the memory shrinker.
37   */
38  public class MockMemoryCache<K, V>
39      implements IMemoryCache<K, V>
40  {
41      /** Config */
42      private ICompositeCacheAttributes cacheAttr;
43  
44      /** Internal map */
45      private final HashMap<K, ICacheElement<K, V>> map = new HashMap<>();
46  
47      /** The number of times waterfall was called. */
48      public int waterfallCallCount;
49  
50      /** The number passed to the last call of free elements. */
51      public int lastNumberOfFreedElements;
52  
53      /**
54       * Does nothing
55       * @param cache
56       */
57      @Override
58      public void initialize( final CompositeCache<K, V> cache )
59      {
60          // nothing
61      }
62  
63      /**
64       * Destroy the memory cache
65       * <p>
66       * @throws IOException
67       */
68      @Override
69      public void dispose()
70          throws IOException
71      {
72          // nothing
73      }
74  
75      /** @return size */
76      @Override
77      public int getSize()
78      {
79          return map.size();
80      }
81  
82      /** @return stats */
83      @Override
84      public IStats getStatistics()
85      {
86          return null;
87      }
88  
89      /**
90       * @return map.keySet().toArray( */
91      @Override
92      public Set<K> getKeySet()
93      {
94          return new LinkedHashSet<>(map.keySet());
95      }
96  
97      /**
98       * @param key
99       * @return map.remove( key ) != null
100      * @throws IOException
101      */
102     @Override
103     public boolean remove( final K key )
104         throws IOException
105     {
106         return map.remove( key ) != null;
107     }
108 
109     /**
110      * @throws IOException
111      */
112     @Override
113     public void removeAll()
114         throws IOException
115     {
116         map.clear();
117     }
118 
119     /**
120      * @param key
121      * @return (ICacheElement) map.get( key )
122      * @throws IOException
123      */
124     @Override
125     public ICacheElement<K, V> get( final K key )
126         throws IOException
127     {
128         return map.get( key );
129     }
130 
131     /**
132      * @param keys
133      * @return elements
134      * @throws IOException
135      */
136     @Override
137     public Map<K, ICacheElement<K, V>> getMultiple(final Set<K> keys)
138         throws IOException
139     {
140         final Map<K, ICacheElement<K, V>> elements = new HashMap<>();
141 
142         if ( keys != null && !keys.isEmpty() )
143         {
144             final Iterator<K> iterator = keys.iterator();
145 
146             while ( iterator.hasNext() )
147             {
148                 final K key = iterator.next();
149 
150                 final ICacheElement<K, V> element = get( key );
151 
152                 if ( element != null )
153                 {
154                     elements.put( key, element );
155                 }
156             }
157         }
158 
159         return elements;
160     }
161 
162     /**
163      * @param key
164      * @return (ICacheElement) map.get( key )
165      * @throws IOException
166      */
167     @Override
168     public ICacheElement<K, V> getQuiet( final K key )
169         throws IOException
170     {
171         return map.get( key );
172     }
173 
174     /**
175      * @param ce
176      * @throws IOException
177      */
178     @Override
179     public void waterfal( final ICacheElement<K, V> ce )
180         throws IOException
181     {
182         waterfallCallCount++;
183     }
184 
185     /**
186      * @param ce
187      * @throws IOException
188      */
189     @Override
190     public void update( final ICacheElement<K, V> ce )
191         throws IOException
192     {
193         if ( ce != null )
194         {
195             map.put( ce.getKey(), ce );
196         }
197     }
198 
199     /**
200      * @return ICompositeCacheAttributes
201      */
202     @Override
203     public ICompositeCacheAttributes getCacheAttributes()
204     {
205         return cacheAttr;
206     }
207 
208     /**
209      * @param cattr
210      */
211     @Override
212     public void setCacheAttributes( final ICompositeCacheAttributes cattr )
213     {
214         this.cacheAttr = cattr;
215     }
216 
217     /** @return null */
218     @Override
219     public CompositeCache<K, V> getCompositeCache()
220     {
221         return null;
222     }
223 
224     /**
225      * @param group
226      * @return null
227      */
228     public Set<K> getGroupKeys( final String group )
229     {
230         return null;
231     }
232 
233     /**
234      * @return null
235      */
236     public Set<String> getGroupNames()
237     {
238         return null;
239     }
240 
241     /**
242      * @param numberToFree
243      * @return 0
244      * @throws IOException
245      */
246     @Override
247     public int freeElements( final int numberToFree )
248         throws IOException
249     {
250         lastNumberOfFreedElements = numberToFree;
251         return 0;
252     }
253 }