View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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.html 13146 2011-08-01 17:12:39Z oletizi $
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       *
35       * @param value the factory always creates values equal to this value
36       */
37      public CountingCollectionsCacheEntryFactory(final Object value) {
38          this.value = value;
39      }
40  
41      /***
42       * Fetches an entry.
43       */
44      public Object createEntry(final Object key) {
45          count++;
46          return value;
47      }
48  
49      /***
50       * Perform an incremental update of data within a CacheEntry.
51       * Based on identification of dirty values within a CacheEntry
52       * Insert Update or Delete those entries based on the existing value.
53       * <p/>
54       * This method does not return a modified value, because it modifies the value passed into it, relying
55       * on the pass by reference feature of Java.
56       * <p/>
57       * Implementations of this method must be thread safe.
58       *
59       * @param key   the cache Key
60       * @param value a value copied from the value that belonged to the Element in the cache. Value must be mutable
61       * @throws Exception
62       */
63      public void updateEntryValue(Object key, Object value) throws Exception {
64          count++;
65          //nothing required for this example.
66      }
67  
68      /***
69       * @return number of entries the factory has created.
70       */
71      public int getCount() {
72          return count;
73      }
74  
75  }