1   package org.apache.jcs.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.io.Serializable;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Set;
27  
28  import org.apache.jcs.engine.behavior.ICacheElement;
29  import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
30  import org.apache.jcs.engine.control.CompositeCache;
31  import org.apache.jcs.engine.stats.behavior.IStats;
32  
33  /***
34   * Mock implementation of a memory cache for testing things like the memory
35   * shrinker.
36   * <p>
37   * @author Aaron Smuts
38   */
39  public class MemoryCacheMockImpl
40      implements MemoryCache
41  {
42      private ICompositeCacheAttributes cacheAttr;
43  
44      private HashMap map = new HashMap();
45  
46      /***
47       * The number of times waterfall was called.
48       */
49      public int waterfallCallCount = 0;
50  
51      /***
52       * The number passed to the last call of free elements.
53       */
54      public int lastNumberOfFreedElements = 0;
55  
56      public void initialize( CompositeCache cache )
57      {
58          // TODO Auto-generated method stub
59      }
60  
61      public void dispose()
62          throws IOException
63      {
64          // TODO Auto-generated method stub
65  
66      }
67  
68      public int getSize()
69      {
70          return map.size();
71      }
72  
73      public IStats getStatistics()
74      {
75          // TODO Auto-generated method stub
76          return null;
77      }
78  
79      public Iterator getIterator()
80      {
81          // return
82          return null;
83      }
84  
85      public Object[] getKeyArray()
86      {
87          return map.keySet().toArray();
88      }
89  
90      public boolean remove( Serializable key )
91          throws IOException
92      {
93          return map.remove( key ) != null;
94      }
95  
96      public void removeAll()
97          throws IOException
98      {
99          map.clear();
100     }
101 
102     public ICacheElement get( Serializable key )
103         throws IOException
104     {
105         return (ICacheElement) map.get( key );
106     }
107 
108     public ICacheElement getQuiet( Serializable key )
109         throws IOException
110     {
111         return (ICacheElement) map.get( key );
112     }
113 
114     public void waterfal( ICacheElement ce )
115         throws IOException
116     {
117         waterfallCallCount++;
118     }
119 
120     public void update( ICacheElement ce )
121         throws IOException
122     {
123         if ( ce != null )
124         {
125             map.put( ce.getKey(), ce );
126         }
127     }
128 
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.apache.jcs.engine.memory.MemoryCache#getCacheAttributes()
133      */
134     public ICompositeCacheAttributes getCacheAttributes()
135     {
136         return cacheAttr;
137     }
138 
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.apache.jcs.engine.memory.MemoryCache#setCacheAttributes(org.apache.jcs.engine.behavior.ICompositeCacheAttributes)
143      */
144     public void setCacheAttributes( ICompositeCacheAttributes cattr )
145     {
146         this.cacheAttr = cattr;
147     }
148 
149     public CompositeCache getCompositeCache()
150     {
151         // TODO Auto-generated method stub
152         return null;
153     }
154 
155     public Set getGroupKeys( String group )
156     {
157         // TODO Auto-generated method stub
158         return null;
159     }
160 
161     /* (non-Javadoc)
162      * @see org.apache.jcs.engine.memory.MemoryCache#freeElements(int)
163      */
164     public int freeElements( int numberToFree )
165         throws IOException
166     {
167         lastNumberOfFreedElements = numberToFree;
168         return 0;
169     }
170 }