~ubuntu-branches/ubuntu/oneiric/ehcache/oneiric

« back to all changes in this revision

Viewing changes to src/test/java/net/sf/ehcache/constructs/blocking/CountingCacheEntryFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2010-06-23 10:35:31 UTC
  • mfrom: (1.1.5 upstream) (2.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100623103531-ra0qdpmotoz6ygct
Tags: 2.1.0-1
Merge changes from Thierry's PPA and upload to Debian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
import net.sf.ehcache.Element;
 
20
 
 
21
import java.util.Random;
 
22
 
 
23
/**
 
24
 * A cache entry factory that counts the number of entries it has created.
 
25
 * <p/>
 
26
 * This is useful for writing tests.
 
27
 *
 
28
 * @author Greg Luck
 
29
 * @version $Id: CountingCacheEntryFactory.java 2154 2010-04-06 02:45:52Z cdennis $
 
30
 */
 
31
public class CountingCacheEntryFactory implements UpdatingCacheEntryFactory {
 
32
 
 
33
    private int count;
 
34
    private final Object value;
 
35
    private Random random;
 
36
 
 
37
    /**
 
38
     * Creates a new instance
 
39
     *
 
40
     * @param value the factory always creates values equal to this value
 
41
     */
 
42
    public CountingCacheEntryFactory(final Object value) {
 
43
        this.value = value;
 
44
        random = new Random();
 
45
    }
 
46
 
 
47
    /**
 
48
     * Fetches an entry.
 
49
     */
 
50
    public Object createEntry(final Object key) {
 
51
        count++;
 
52
        if (random.nextInt(2) == 1) {
 
53
            return value;
 
54
        } else {
 
55
            return new Element(key, value);
 
56
        }
 
57
    }
 
58
 
 
59
    /**
 
60
     * @return number of entries the factory has created.
 
61
     */
 
62
    public int getCount() {
 
63
        return count;
 
64
    }
 
65
 
 
66
    /**
 
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.
 
70
     * <p/>
 
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.
 
73
     * <p/>
 
74
     * Implementations of this method must be thread safe.
 
75
     *
 
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
 
78
     * @throws Exception
 
79
     */
 
80
    public void updateEntryValue(Object key, Object value) throws Exception {
 
81
        count++;
 
82
        if (key.equals("explode") && count > 1) {
 
83
            throw new RuntimeException("EXPLODE!");
 
84
        }
 
85
    }
 
86
 
 
87
}