View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.monitoring.counters;
18  
19  import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
20  import org.apache.commons.monitoring.store.DataStore;
21  
22  import java.util.concurrent.atomic.AtomicInteger;
23  import java.util.concurrent.locks.Lock;
24  import java.util.concurrent.locks.ReentrantLock;
25  
26  public class DefaultCounter implements Counter {
27      private final AtomicInteger concurrency = new AtomicInteger(0);
28      private final Key key;
29      private final DataStore dataStore;
30      private volatile int maxConcurrency = 0;
31      private SummaryStatistics statistics;
32      private Lock lock = new ReentrantLock();
33  
34      public DefaultCounter(final Key key, final DataStore store) {
35          this.key = key;
36          this.statistics = new SummaryStatistics();
37          this.dataStore = store;
38      }
39  
40      public void addInternal(final double delta) { // should be called from a thread safe environment
41          statistics.addValue(delta);
42      }
43  
44      @Override
45      public void updateConcurrency(final int concurrency) {
46          if (concurrency > maxConcurrency) {
47              maxConcurrency = concurrency;
48          }
49      }
50  
51      @Override
52      public int getMaxConcurrency() {
53          return maxConcurrency;
54      }
55  
56      @Override
57      public AtomicInteger currentConcurrency() {
58          return concurrency;
59      }
60  
61      @Override
62      public Key getKey() {
63          return key;
64      }
65  
66      @Override
67      public void reset() {
68          statistics.clear();
69      }
70  
71      @Override
72      public void add(final double delta) {
73          dataStore.addToCounter(this, delta);
74      }
75  
76      @Override
77      public void add(final double delta, final Unit deltaUnit) {
78          add(key.getRole().getUnit().convert(delta, deltaUnit));
79      }
80  
81      @Override
82      public double getMax() {
83          return statistics.getMax();
84      }
85  
86      @Override
87      public double getMin() {
88          return statistics.getMin();
89      }
90  
91      @Override
92      public double getSum() {
93          return statistics.getSum();
94      }
95  
96      @Override
97      public double getStandardDeviation() {
98          return statistics.getStandardDeviation();
99      }
100 
101     @Override
102     public double getVariance() {
103         return statistics.getVariance();
104     }
105 
106     @Override
107     public double getMean() {
108         return statistics.getMean();
109     }
110 
111     @Override
112     public double getGeometricMean() {
113         return statistics.getGeometricMean();
114     }
115 
116     @Override
117     public double getSumOfLogs() {
118         return statistics.getSumOfLogs();
119     }
120 
121     @Override
122     public double getSumOfSquares() {
123         return statistics.getSumOfLogs();
124     }
125 
126     @Override
127     public long getHits() {
128         return statistics.getN();
129     }
130 
131     public Lock getLock() {
132         return lock;
133     }
134 }