View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs.jcache.jmx;
20  
21  import org.apache.commons.jcs.jcache.Statistics;
22  
23  import javax.cache.management.CacheStatisticsMXBean;
24  
25  public class JCSCacheStatisticsMXBean implements CacheStatisticsMXBean
26  {
27      private final Statistics statistics;
28  
29      public JCSCacheStatisticsMXBean(final Statistics stats)
30      {
31          this.statistics = stats;
32      }
33  
34      @Override
35      public void clear()
36      {
37          statistics.reset();
38      }
39  
40      @Override
41      public long getCacheHits()
42      {
43          return statistics.getHits();
44      }
45  
46      @Override
47      public float getCacheHitPercentage()
48      {
49          final long hits = getCacheHits();
50          if (hits == 0)
51          {
52              return 0;
53          }
54          return (float) hits / getCacheGets() * 100.0f;
55      }
56  
57      @Override
58      public long getCacheMisses()
59      {
60          return statistics.getMisses();
61      }
62  
63      @Override
64      public float getCacheMissPercentage()
65      {
66          final long misses = getCacheMisses();
67          if (misses == 0)
68          {
69              return 0;
70          }
71          return (float) misses / getCacheGets() * 100.0f;
72      }
73  
74      @Override
75      public long getCacheGets()
76      {
77          return getCacheHits() + getCacheMisses();
78      }
79  
80      @Override
81      public long getCachePuts()
82      {
83          return statistics.getPuts();
84      }
85  
86      @Override
87      public long getCacheRemovals()
88      {
89          return statistics.getRemovals();
90      }
91  
92      @Override
93      public long getCacheEvictions()
94      {
95          return statistics.getEvictions();
96      }
97  
98      @Override
99      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 }