001package org.apache.commons.jcs3.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.concurrent.ConcurrentHashMap;
023import java.util.concurrent.ConcurrentMap;
024
025import org.apache.commons.jcs3.engine.behavior.ICache;
026import org.apache.commons.jcs3.engine.behavior.ICacheEventQueue;
027
028/**
029 * Used to associates a set of [cache listener to cache event queue] for a
030 * cache.
031 */
032public class CacheListeners<K, V>
033{
034    /** The cache using the queue. */
035    public final ICache<K, V> cache;
036
037    /** Map ICacheListener to ICacheEventQueue */
038    public final ConcurrentMap<Long, ICacheEventQueue<K, V>> eventQMap =
039        new ConcurrentHashMap<>();
040
041    /**
042     * Constructs with the given cache.
043     * <p>
044     * @param cache
045     */
046    public CacheListeners( final ICache<K, V> cache )
047    {
048        if ( cache == null )
049        {
050            throw new IllegalArgumentException( "cache must not be null" );
051        }
052        this.cache = cache;
053    }
054
055    /** @return info on the listeners */
056    @Override
057    public String toString()
058    {
059        final StringBuilder buffer = new StringBuilder();
060        buffer.append( "\n CacheListeners" );
061        buffer.append( "\n Region = " + cache.getCacheName() );
062        buffer.append( "\n Event Queue Map " );
063        buffer.append( "\n size = " + eventQMap.size() );
064        eventQMap.forEach((key, value)
065                -> buffer.append( "\n Entry: key: ").append(key)
066                    .append(", value: ").append(value));
067        return buffer.toString();
068    }
069}