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.
24 package org.onap.aai.sparky.analytics;
27 * A simple class to model a historical counter. A set of values will be tracked and basic
28 * statistics will be calculated in real time (n, min, max, avg).
32 public class HistoricalCounter {
38 private double totalOfSamples;
40 private long numSamples;
44 private boolean maintainSingleValue;
47 * Instantiates a new historical counter.
49 * @param trackSingleValue the track single value
51 public HistoricalCounter(boolean trackSingleValue) {
57 this.maintainSingleValue = trackSingleValue;
60 public boolean isSingleValue() {
61 return maintainSingleValue;
67 * @param value the value
69 public synchronized void update(double value) {
75 if (maintainSingleValue) {
93 totalOfSamples += value;
98 public double getValue() {
102 public double getMin() {
106 public double getMax() {
110 public long getNumSamples() {
114 public double getAvg() {
115 if (numSamples == 0) {
119 return (totalOfSamples / numSamples);
125 public synchronized void reset() {
134 * @see java.lang.Object#toString()
137 public String toString() {
138 StringBuilder sb = new StringBuilder(32);
140 if (maintainSingleValue) {
141 sb.append("[ Val=").append(value).append(" ]");
143 sb.append("[ NumSamples=").append(numSamples).append(",");
144 sb.append(" Min=").append(min).append(",");
145 sb.append(" Max=").append(max).append(",");
146 sb.append(" Avg=").append(getAvg()).append(" ]");
149 return sb.toString();