001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.jcs3.jcache.jmx; 020 021import javax.cache.management.CacheStatisticsMXBean; 022 023import org.apache.commons.jcs3.jcache.Statistics; 024 025public class JCSCacheStatisticsMXBean implements CacheStatisticsMXBean 026{ 027 private final Statistics statistics; 028 029 public JCSCacheStatisticsMXBean(final Statistics stats) 030 { 031 this.statistics = stats; 032 } 033 034 @Override 035 public void clear() 036 { 037 statistics.reset(); 038 } 039 040 @Override 041 public long getCacheHits() 042 { 043 return statistics.getHits(); 044 } 045 046 @Override 047 public float getCacheHitPercentage() 048 { 049 final long hits = getCacheHits(); 050 if (hits == 0) 051 { 052 return 0; 053 } 054 return (float) hits / getCacheGets() * 100.0f; 055 } 056 057 @Override 058 public long getCacheMisses() 059 { 060 return statistics.getMisses(); 061 } 062 063 @Override 064 public float getCacheMissPercentage() 065 { 066 final long misses = getCacheMisses(); 067 if (misses == 0) 068 { 069 return 0; 070 } 071 return (float) misses / getCacheGets() * 100.0f; 072 } 073 074 @Override 075 public long getCacheGets() 076 { 077 return getCacheHits() + getCacheMisses(); 078 } 079 080 @Override 081 public long getCachePuts() 082 { 083 return statistics.getPuts(); 084 } 085 086 @Override 087 public long getCacheRemovals() 088 { 089 return statistics.getRemovals(); 090 } 091 092 @Override 093 public long getCacheEvictions() 094 { 095 return statistics.getEvictions(); 096 } 097 098 @Override 099 public float getAverageGetTime() 100 { 101 return averageTime(statistics.getTimeTakenForGets()); 102 } 103 104 @Override 105 public float getAveragePutTime() 106 { 107 return averageTime(statistics.getTimeTakenForPuts()); 108 } 109 110 @Override 111 public float getAverageRemoveTime() 112 { 113 return averageTime(statistics.getTimeTakenForRemovals()); 114 } 115 116 private float averageTime(final long timeTaken) 117 { 118 final long gets = getCacheGets(); 119 if (timeTaken == 0 || gets == 0) 120 { 121 return 0; 122 } 123 return timeTaken / gets; 124 } 125}