001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.monitoring.counters; 018 019 import org.apache.commons.math3.stat.descriptive.SummaryStatistics; 020 import org.apache.commons.monitoring.store.DataStore; 021 022 import java.util.concurrent.atomic.AtomicInteger; 023 import java.util.concurrent.locks.Lock; 024 import java.util.concurrent.locks.ReentrantLock; 025 026 public class DefaultCounter implements Counter { 027 private final AtomicInteger concurrency = new AtomicInteger(0); 028 private final Key key; 029 private final DataStore dataStore; 030 private volatile int maxConcurrency = 0; 031 private SummaryStatistics statistics; 032 private Lock lock = new ReentrantLock(); 033 034 public DefaultCounter(final Key key, final DataStore store) { 035 this.key = key; 036 this.statistics = new SummaryStatistics(); 037 this.dataStore = store; 038 } 039 040 public void addInternal(final double delta) { // should be called from a thread safe environment 041 statistics.addValue(delta); 042 } 043 044 @Override 045 public void updateConcurrency(final int concurrency) { 046 if (concurrency > maxConcurrency) { 047 maxConcurrency = concurrency; 048 } 049 } 050 051 @Override 052 public int getMaxConcurrency() { 053 return maxConcurrency; 054 } 055 056 @Override 057 public AtomicInteger currentConcurrency() { 058 return concurrency; 059 } 060 061 @Override 062 public Key getKey() { 063 return key; 064 } 065 066 @Override 067 public void reset() { 068 statistics.clear(); 069 } 070 071 @Override 072 public void add(final double delta) { 073 dataStore.addToCounter(this, delta); 074 } 075 076 @Override 077 public void add(final double delta, final Unit deltaUnit) { 078 add(key.getRole().getUnit().convert(delta, deltaUnit)); 079 } 080 081 @Override 082 public double getMax() { 083 return statistics.getMax(); 084 } 085 086 @Override 087 public double getMin() { 088 return statistics.getMin(); 089 } 090 091 @Override 092 public double getSum() { 093 return statistics.getSum(); 094 } 095 096 @Override 097 public double getStandardDeviation() { 098 return statistics.getStandardDeviation(); 099 } 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 }