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;
20  
21  import java.util.concurrent.atomic.AtomicLong;
22  
23  public class Statistics
24  {
25      private volatile boolean active = true;
26  
27      private final AtomicLong removals = new AtomicLong();
28      private final AtomicLong expiries = new AtomicLong();
29      private final AtomicLong puts = new AtomicLong();
30      private final AtomicLong hits = new AtomicLong();
31      private final AtomicLong misses = new AtomicLong();
32      private final AtomicLong evictions = new AtomicLong();
33      private final AtomicLong putTimeTaken = new AtomicLong();
34      private final AtomicLong getTimeTaken = new AtomicLong();
35      private final AtomicLong removeTimeTaken = new AtomicLong();
36  
37      public long getHits()
38      {
39          return hits.get();
40      }
41  
42      public long getMisses()
43      {
44          return misses.get();
45      }
46  
47      public long getPuts()
48      {
49          return puts.get();
50      }
51  
52      public long getRemovals()
53      {
54          return removals.get();
55      }
56  
57      public long getEvictions()
58      {
59          return evictions.get();
60      }
61  
62      public long getTimeTakenForGets()
63      {
64          return getTimeTaken.get();
65      }
66  
67      public long getTimeTakenForPuts()
68      {
69          return putTimeTaken.get();
70      }
71  
72      public long getTimeTakenForRemovals()
73      {
74          return removeTimeTaken.get();
75      }
76  
77      public void increaseRemovals(final long number)
78      {
79          increment(removals, number);
80      }
81  
82      public void increaseExpiries(final long number)
83      {
84          increment(expiries, number);
85      }
86  
87      public void increasePuts(final long number)
88      {
89          increment(puts, number);
90      }
91  
92      public void increaseHits(final long number)
93      {
94          increment(hits, number);
95      }
96  
97      public void increaseMisses(final long number)
98      {
99          increment(misses, number);
100     }
101 
102     public void increaseEvictions(final long number)
103     {
104         increment(evictions, number);
105     }
106 
107     public void addGetTime(final long duration)
108     {
109         increment(duration, getTimeTaken);
110     }
111 
112     public void addPutTime(final long duration)
113     {
114         increment(duration, putTimeTaken);
115     }
116 
117     public void addRemoveTime(final long duration)
118     {
119         increment(duration, removeTimeTaken);
120     }
121 
122     private void increment(final AtomicLong counter, final long number)
123     {
124         if (!active)
125         {
126             return;
127         }
128         counter.addAndGet(number);
129     }
130 
131     private void increment(final long duration, final AtomicLong counter)
132     {
133         if (!active)
134         {
135             return;
136         }
137 
138         if (counter.get() < Long.MAX_VALUE - duration)
139         {
140             counter.addAndGet(duration);
141         }
142         else
143         {
144             reset();
145             counter.set(duration);
146         }
147     }
148 
149     public void reset()
150     {
151         puts.set(0);
152         misses.set(0);
153         removals.set(0);
154         expiries.set(0);
155         hits.set(0);
156         evictions.set(0);
157         getTimeTaken.set(0);
158         putTimeTaken.set(0);
159         removeTimeTaken.set(0);
160     }
161 
162     public void setActive(final boolean active)
163     {
164         this.active = active;
165     }
166 }