2
* Copyright 2003-2010 Terracotta, Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package net.sf.ehcache.constructs.blocking;
19
import net.sf.ehcache.Element;
21
import java.util.Random;
24
* A cache entry factory that counts the number of entries it has created.
26
* This is useful for writing tests.
29
* @version $Id: CountingCacheEntryFactory.java 2154 2010-04-06 02:45:52Z cdennis $
31
public class CountingCacheEntryFactory implements UpdatingCacheEntryFactory {
34
private final Object value;
35
private Random random;
38
* Creates a new instance
40
* @param value the factory always creates values equal to this value
42
public CountingCacheEntryFactory(final Object value) {
44
random = new Random();
50
public Object createEntry(final Object key) {
52
if (random.nextInt(2) == 1) {
55
return new Element(key, value);
60
* @return number of entries the factory has created.
62
public int getCount() {
67
* Perform an incremental update of data within a CacheEntry.
68
* Based on identification of dirty values within a CacheEntry
69
* Insert Update or Delete those entries based on the existing value.
71
* This method does not return a modified value, because it modifies the value passed into it, relying
72
* on the pass by reference feature of Java.
74
* Implementations of this method must be thread safe.
76
* @param key the cache Key
77
* @param value a value copied from the value that belonged to the Element in the cache. Value must be mutable
80
public void updateEntryValue(Object key, Object value) throws Exception {
82
if (key.equals("explode") && count > 1) {
83
throw new RuntimeException("EXPLODE!");