001/*
002 *  Copyright 2019 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 */
016
017package org.ametys.plugins.core.impl.cache;
018
019import org.ametys.core.cache.CacheStats;
020
021/**
022 * Guava implementation of CacheStats
023 */
024public class GuavaCacheStats implements CacheStats
025{
026    private com.google.common.cache.CacheStats _cacheStats;
027
028    /**
029     * GuavaCacheStats implementation of CacheStats
030     */
031    public GuavaCacheStats()
032    {
033        this._cacheStats = new com.google.common.cache.CacheStats(0, 0, 0, 0, 0, 0);
034    }
035    
036    /**
037     * GuavaCacheStats implementation of CacheStats
038     * @param cacheStats stats of Guava cache
039     */
040    public GuavaCacheStats(com.google.common.cache.CacheStats cacheStats)
041    {
042        this._cacheStats = cacheStats;
043    }
044
045    public long requestCount()
046    {
047        return _cacheStats.requestCount();
048    }
049
050    public long hitCount()
051    {
052        return _cacheStats.hitCount();
053    }
054    
055    public double hitRate()
056    {
057        return _cacheStats.hitRate();
058    }
059
060    public long missCount()
061    {
062        return _cacheStats.missCount();
063    }
064
065    public double missRate()
066    {
067        return _cacheStats.missRate();
068    }
069
070    public long evictionCount()
071    {
072        return _cacheStats.evictionCount();
073    }
074
075    public double evictionRate()
076    {
077        long requestCount = requestCount();
078        return (requestCount == 0) ? 0.0 : (double) evictionCount() / requestCount;
079    }
080
081    public CacheStats minus(CacheStats cacheStats)
082    {
083        com.google.common.cache.CacheStats guavaCacheStats = new com.google.common.cache.CacheStats(cacheStats.hitCount(), cacheStats.missCount(), cacheStats.missCount(), 0, 0, cacheStats.evictionCount());
084        return new GuavaCacheStats(_cacheStats.minus(guavaCacheStats)); 
085    }
086
087    public CacheStats plus(CacheStats cacheStats)
088    {
089        com.google.common.cache.CacheStats guavaCacheStats = new com.google.common.cache.CacheStats(cacheStats.hitCount(), cacheStats.missCount(), cacheStats.missCount(), 0, 0, cacheStats.evictionCount());
090        return new GuavaCacheStats(_cacheStats.plus(guavaCacheStats)); 
091    }
092}