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 blocks until signaled.
21 * <p/>
22 * This is useful for writing tests.
23 *
24 * @author Greg Luck
25 * @version $Id: BlockingCacheEntryFactory.java 512 2007-07-10 09:18:45Z gregluck $
26 */
27 public class BlockingCacheEntryFactory implements CacheEntryFactory {
28
29 private final Object value;
30 private int count;
31
32 /**
33 * Constructs a new object
34 * @param value the factory always creates values equal to this value
35 */
36 public BlockingCacheEntryFactory(final Object value) {
37 this.value = value;
38 }
39
40
41 /**
42 * @return number of entries the factory has created.
43 */
44 public int getCount() {
45 return count;
46 }
47
48
49 /**
50 * Signals the factory.
51 */
52 public synchronized void signal(final int count) {
53 this.count += count;
54 notify();
55 }
56
57 /**
58 * Fetches an entry.
59 */
60 public synchronized Object createEntry(final Object key) throws Exception {
61 // Wait until signalled
62 while (count == 0) {
63 wait();
64 }
65 count--;
66 return value;
67 }
68 }