001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.site;
017
018import java.util.Map;
019import java.util.concurrent.ConcurrentHashMap;
020
021import org.apache.avalon.framework.component.Component;
022
023/**
024 * Helper for site cache logger
025 */
026public class CacheAccessCounter implements Component
027{
028    /** Avalon Role */
029    public static final String ROLE = CacheAccessCounter.class.getName();
030    
031    private Map<String, Integer> _count = new ConcurrentHashMap<>();
032    
033    /**
034     * Increase the number of asked resources
035     * @param siteName the site name
036     */
037    public void increaseAskedResources (String siteName)
038    {
039        if (!_count.containsKey(siteName))
040        {
041            _count.put(siteName, 0);
042        }
043        
044        _count.put(siteName, _count.get(siteName) + 1);
045    }
046    
047    /**
048     * Get the number of asked resources
049     * @param siteName the site name
050     * @return the number of asked resources
051     */
052    public int getAskedResources (String siteName)
053    {
054        Integer count = _count.get(siteName);
055        return count != null ? count : 0;
056    }
057    
058    /**
059     * Get the number of asked resources
060     * @return the number of asked resources
061     */
062    public int getAskedResources ()
063    {
064        int count = 0;
065        
066        for (String siteName : _count.keySet())
067        {
068            count += _count.get(siteName);
069        }
070        
071        return count;
072    }
073    
074    /**
075     * Reset count
076     * @param siteName the site name
077     */
078    public void resetCount (String siteName)
079    {
080        _count.put(siteName, 0);
081    }
082    
083    /**
084     * Reset all counters
085     */
086    public void resetAllCount ()
087    {
088        _count.clear();
089    }
090}