001package org.apache.commons.jcs.engine;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Iterator;
023import java.util.Map;
024import java.util.concurrent.ConcurrentHashMap;
025import java.util.concurrent.ConcurrentMap;
026
027import org.apache.commons.jcs.engine.behavior.ICache;
028import org.apache.commons.jcs.engine.behavior.ICacheEventQueue;
029
030/**
031 * Used to associates a set of [cache listener to cache event queue] for a
032 * cache.
033 */
034public class CacheListeners<K, V>
035{
036    /** The cache using the queue. */
037    public final ICache<K, V> cache;
038
039    /** Map ICacheListener to ICacheEventQueue */
040    public final ConcurrentMap<Long, ICacheEventQueue<K, V>> eventQMap =
041        new ConcurrentHashMap<Long, ICacheEventQueue<K, V>>();
042
043    /**
044     * Constructs with the given cache.
045     * <p>
046     * @param cache
047     */
048    public CacheListeners( ICache<K, V> cache )
049    {
050        if ( cache == null )
051        {
052            throw new IllegalArgumentException( "cache must not be null" );
053        }
054        this.cache = cache;
055    }
056
057    /** @return info on the listeners */
058    @Override
059    public String toString()
060    {
061        StringBuilder buffer = new StringBuilder();
062        buffer.append( "\n CacheListeners" );
063        if ( cache != null )
064        {
065            buffer.append( "\n Region = " + cache.getCacheName() );
066        }
067        if ( eventQMap != null )
068        {
069            buffer.append( "\n Event Queue Map " );
070            buffer.append( "\n size = " + eventQMap.size() );
071            Iterator<Map.Entry<Long, ICacheEventQueue<K, V>>> it = eventQMap.entrySet().iterator();
072            while ( it.hasNext() )
073            {
074                buffer.append( "\n Entry: " + it.next() );
075            }
076        }
077        else
078        {
079            buffer.append( "\n No Listeners. " );
080        }
081        return buffer.toString();
082    }
083}