2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.analytics;
24 * A simple class to model a historical counter. A set of values will be tracked and basic
25 * statistics will be calculated in real time (n, min, max, avg).
29 public class HistoricalCounter {
35 private double totalOfSamples;
37 private long numSamples;
41 private boolean maintainSingleValue;
44 * Instantiates a new historical counter.
46 * @param trackSingleValue the track single value
48 public HistoricalCounter(boolean trackSingleValue) {
54 this.maintainSingleValue = trackSingleValue;
57 public boolean isSingleValue() {
58 return maintainSingleValue;
64 * @param value the value
66 public synchronized void update(double value) {
72 if (maintainSingleValue) {
78 if (Double.valueOf(min).equals(-1.0)) {
90 totalOfSamples += value;
95 public double getValue() {
99 public double getMin() {
103 public double getMax() {
107 public long getNumSamples() {
111 public double getAvg() {
112 if (numSamples == 0) {
116 return (totalOfSamples / numSamples);
118 public void setMin(double min) {
122 public void setMax(double max) {
126 public double getTotalOfSamples() {
127 return totalOfSamples;
130 public void setTotalOfSamples(double totalOfSamples) {
131 this.totalOfSamples = totalOfSamples;
134 public void setNumSamples(long numSamples) {
135 this.numSamples = numSamples;
138 public void setMaintainSingleValue(boolean maintainSingleValue) {
139 this.maintainSingleValue = maintainSingleValue;
146 public synchronized void reset() {
155 * @see java.lang.Object#toString()
158 public String toString() {
159 StringBuilder sb = new StringBuilder(32);
161 if (maintainSingleValue) {
162 sb.append("[ Val=").append(value).append(" ]");
164 sb.append("[ NumSamples=").append(numSamples).append(",");
165 sb.append(" Min=").append(min).append(",");
166 sb.append(" Max=").append(max).append(",");
167 sb.append(" Avg=").append(getAvg()).append(" ]");
170 return sb.toString();