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: CountingCollectionsCacheEntryFactory.java 512 2007-07-10 09:18:45Z gregluck $
26   */
27  public class CountingCollectionsCacheEntryFactory 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 CountingCollectionsCacheEntryFactory(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       * Perform an incremental update of data within a CacheEntry.
50       * Based on identification of dirty values within a CacheEntry
51       * Insert Update or Delete those entries based on the existing value.
52       * <p/>
53       * This method does not return a modified value, because it modifies the value passed into it, relying
54       * on the pass by reference feature of Java.
55       * <p/>
56       * Implementations of this method must be thread safe.
57       *
58       * @param key   the cache Key
59       * @param value a value copied from the value that belonged to the Element in the cache. Value must be mutable
60       * @throws Exception
61       */
62      public void updateEntryValue(Object key, Object value) throws Exception {
63          count++;
64          //nothing required for this example.
65      }
66  
67      /**
68       * @return number of entries the factory has created.
69       */
70      public int getCount() {
71          return count;
72      }
73  
74  }