2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.analytics;
26 * A simple class to model a historical counter. A set of values will be tracked and basic
27 * statistics will be calculated in real time (n, min, max, avg).
31 public class HistoricalCounter {
37 private double totalOfSamples;
39 private long numSamples;
43 private boolean maintainSingleValue;
46 * Instantiates a new historical counter.
48 * @param trackSingleValue the track single value
50 public HistoricalCounter(boolean trackSingleValue) {
56 this.maintainSingleValue = trackSingleValue;
59 public boolean isSingleValue() {
60 return maintainSingleValue;
66 * @param value the value
68 public synchronized void update(double value) {
74 if (maintainSingleValue) {
92 totalOfSamples += value;
97 public double getValue() {
101 public double getMin() {
105 public double getMax() {
109 public long getNumSamples() {
113 public double getAvg() {
114 if (numSamples == 0) {
118 return (totalOfSamples / numSamples);
121 public void setMin(double min) {
125 public void setMax(double max) {
129 public double getTotalOfSamples() {
130 return totalOfSamples;
133 public void setTotalOfSamples(double totalOfSamples) {
134 this.totalOfSamples = totalOfSamples;
137 public void setNumSamples(long numSamples) {
138 this.numSamples = numSamples;
141 public void setMaintainSingleValue(boolean maintainSingleValue) {
142 this.maintainSingleValue = maintainSingleValue;
149 public synchronized void reset() {
160 * @see java.lang.Object#toString()
163 public String toString() {
164 StringBuilder sb = new StringBuilder(32);
166 if (maintainSingleValue) {
167 sb.append("[ Val=").append(value).append(" ]");
169 sb.append("[ NumSamples=").append(numSamples).append(",");
170 sb.append(" Min=").append(min).append(",");
171 sb.append(" Max=").append(max).append(",");
172 sb.append(" Avg=").append(getAvg()).append(" ]");
175 return sb.toString();