1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.constructs.blocking;
18  
19  /**
20   * A cache entry factory that counts the number of entries it has created.
21   * <p/>
22   * This is useful for writing tests.
23   *
24   * @author Greg Luck
25   * @version $Id: CountingCacheEntryFactory.java 512 2007-07-10 09:18:45Z gregluck $
26   */
27  public class CountingCacheEntryFactory implements UpdatingCacheEntryFactory {
28  
29      private int count;
30      private final Object value;
31  
32      /**
33       * Creates a new instance
34       * @param value the factory always creates values equal to this value
35       */
36      public CountingCacheEntryFactory(final Object value) {
37          this.value = value;
38      }
39  
40      /**
41       * Fetches an entry.
42       */
43      public Object createEntry(final Object key) {
44          count++;
45          return value;
46      }
47  
48      /**
49       * @return number of entries the factory has created.
50       */
51      public int getCount() {
52          return count;
53      }
54  
55      /**
56       * Perform an incremental update of data within a CacheEntry.
57       * Based on identification of dirty values within a CacheEntry
58       * Insert Update or Delete those entries based on the existing value.
59       * <p/>
60       * This method does not return a modified value, because it modifies the value passed into it, relying
61       * on the pass by reference feature of Java.
62       * <p/>
63       * Implementations of this method must be thread safe.
64       *
65       * @param key   the cache Key
66       * @param value a value copied from the value that belonged to the Element in the cache. Value must be mutable
67       * @throws Exception
68       */
69      public void updateEntryValue(Object key, Object value) throws Exception {
70          count++;
71      }
72  
73  }