1 package org.apache.jcs.engine;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.Serializable;
23 import java.util.Hashtable;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import org.apache.jcs.engine.behavior.ICache;
28 import org.apache.jcs.engine.behavior.ICacheEventQueue;
29
30 /**
31 * Used to associates a set of [cache listener to cache event queue] for a
32 * cache.
33 */
34 public class CacheListeners<K extends Serializable, V extends Serializable>
35 {
36 /** The cache using the queue. */
37 public final ICache<K, V> cache;
38
39 /** Map ICacheListener to ICacheEventQueue */
40 public final Map<Long, ICacheEventQueue<K, V>> eventQMap =
41 new Hashtable<Long, ICacheEventQueue<K, V>>();
42
43 /**
44 * Constructs with the given cache.
45 * <p>
46 * @param cache
47 */
48 public CacheListeners( ICache<K, V> cache )
49 {
50 if ( cache == null )
51 {
52 throw new IllegalArgumentException( "cache must not be null" );
53 }
54 this.cache = cache;
55 }
56
57 /** @return info on the listeners */
58 @Override
59 public String toString()
60 {
61 StringBuffer buffer = new StringBuffer();
62 buffer.append( "\n CacheListeners" );
63 if ( cache != null )
64 {
65 buffer.append( "\n Region = " + cache.getCacheName() );
66 }
67 if ( eventQMap != null )
68 {
69 buffer.append( "\n Event Queue Map " );
70 buffer.append( "\n size = " + eventQMap.size() );
71 Iterator<Map.Entry<Long, ICacheEventQueue<K, V>>> it = eventQMap.entrySet().iterator();
72 while ( it.hasNext() )
73 {
74 buffer.append( "\n Entry: " + it.next() );
75 }
76 }
77 else
78 {
79 buffer.append( "\n No Listeners. " );
80 }
81 return buffer.toString();
82 }
83 }