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.
27 package org.openecomp.sparky.analytics;
30 * A simple class to model a historical counter. A set of values will be tracked and basic
31 * statistics will be calculated in real time (n, min, max, avg).
35 public class HistoricalCounter {
41 private double totalOfSamples;
43 private long numSamples;
47 private boolean maintainSingleValue;
50 * Instantiates a new historical counter.
52 * @param trackSingleValue the track single value
54 public HistoricalCounter(boolean trackSingleValue) {
60 this.maintainSingleValue = trackSingleValue;
63 public boolean isSingleValue() {
64 return maintainSingleValue;
70 * @param value the value
72 public synchronized void update(double value) {
78 if (maintainSingleValue) {
96 totalOfSamples += value;
101 public double getValue() {
105 public double getMin() {
109 public double getMax() {
113 public long getNumSamples() {
117 public double getAvg() {
118 if (numSamples == 0) {
122 return (totalOfSamples / numSamples);
128 public synchronized void reset() {
137 * @see java.lang.Object#toString()
140 public String toString() {
141 StringBuilder sb = new StringBuilder(32);
143 if (maintainSingleValue) {
144 sb.append("[ Val=").append(value).append(" ]");
146 sb.append("[ NumSamples=").append(numSamples).append(",");
147 sb.append(" Min=").append(min).append(",");
148 sb.append(" Max=").append(max).append(",");
149 sb.append(" Avg=").append(getAvg()).append(" ]");
152 return sb.toString();