2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.analytics;
28 * A simple class to model a historical counter. A set of values will be tracked and basic
29 * statistics will be calculated in real time (n, min, max, avg).
33 public class HistoricalCounter {
39 private double totalOfSamples;
41 private long numSamples;
45 private boolean maintainSingleValue;
48 * Instantiates a new historical counter.
50 * @param trackSingleValue the track single value
52 public HistoricalCounter(boolean trackSingleValue) {
58 this.maintainSingleValue = trackSingleValue;
61 public boolean isSingleValue() {
62 return maintainSingleValue;
68 * @param value the value
70 public synchronized void update(double value) {
76 if (maintainSingleValue) {
94 totalOfSamples += value;
99 public double getValue() {
103 public double getMin() {
107 public double getMax() {
111 public long getNumSamples() {
115 public double getAvg() {
116 if (numSamples == 0) {
120 return (totalOfSamples / numSamples);
122 public void setMin(double min) {
126 public void setMax(double max) {
130 public double getTotalOfSamples() {
131 return totalOfSamples;
134 public void setTotalOfSamples(double totalOfSamples) {
135 this.totalOfSamples = totalOfSamples;
138 public void setNumSamples(long numSamples) {
139 this.numSamples = numSamples;
142 public void setMaintainSingleValue(boolean maintainSingleValue) {
143 this.maintainSingleValue = maintainSingleValue;
150 public synchronized void reset() {
159 * @see java.lang.Object#toString()
162 public String toString() {
163 StringBuilder sb = new StringBuilder(32);
165 if (maintainSingleValue) {
166 sb.append("[ Val=").append(value).append(" ]");
168 sb.append("[ NumSamples=").append(numSamples).append(",");
169 sb.append(" Min=").append(min).append(",");
170 sb.append(" Max=").append(max).append(",");
171 sb.append(" Avg=").append(getAvg()).append(" ]");
174 return sb.toString();