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 blocks until signaled.
21 * <p/>
22 * This is useful for writing tests.
23 *
24 * @author Greg Luck
25 * @version $Id: BlockingCacheEntryFactory.html 13146 2011-08-01 17:12:39Z oletizi $
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 *
35 * @param value the factory always creates values equal to this value
36 */
37 public BlockingCacheEntryFactory(final Object value) {
38 this.value = value;
39 }
40
41
42 /***
43 * @return number of entries the factory has created.
44 */
45 public int getCount() {
46 return count;
47 }
48
49
50 /***
51 * Signals the factory.
52 */
53 public synchronized void signal(final int count) {
54 this.count += count;
55 notify();
56 }
57
58 /***
59 * Fetches an entry.
60 */
61 public synchronized Object createEntry(final Object key) throws Exception {
62
63 while (count == 0) {
64 wait();
65 }
66 count--;
67 return value;
68 }
69 }