Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / analytics / HistoricalCounter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.aai.sparky.analytics;
25
26 /**
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).
29  * 
30  * @author davea
31  */
32 public class HistoricalCounter {
33
34   private double min;
35
36   private double max;
37
38   private double totalOfSamples;
39
40   private long numSamples;
41
42   private double value;
43
44   private boolean maintainSingleValue;
45
46   /**
47    * Instantiates a new historical counter.
48    *
49    * @param trackSingleValue the track single value
50    */
51   public HistoricalCounter(boolean trackSingleValue) {
52     min = -1;
53     max = 0;
54     totalOfSamples = 0;
55     value = 0.0;
56     numSamples = 0;
57     this.maintainSingleValue = trackSingleValue;
58   }
59
60   public boolean isSingleValue() {
61     return maintainSingleValue;
62   }
63
64   /**
65    * Update.
66    *
67    * @param value the value
68    */
69   public synchronized void update(double value) {
70
71     if (value < 0) {
72       return;
73     }
74
75     if (maintainSingleValue) {
76
77       this.value = value;
78
79     } else {
80
81       if (min == -1) {
82         min = value;
83       }
84
85       if (value < min) {
86         min = value;
87       }
88
89       if (value > max) {
90         max = value;
91       }
92
93       totalOfSamples += value;
94       numSamples++;
95     }
96   }
97
98   public double getValue() {
99     return value;
100   }
101
102   public double getMin() {
103     return min;
104   }
105
106   public double getMax() {
107     return max;
108   }
109
110   public long getNumSamples() {
111     return numSamples;
112   }
113
114   public double getAvg() {
115     if (numSamples == 0) {
116       return 0;
117     }
118
119     return (totalOfSamples / numSamples);
120   }
121
122   /**
123    * Reset.
124    */
125   public synchronized void reset() {
126     min = -1;
127     max = 0;
128     numSamples = 0;
129     totalOfSamples = 0;
130     value = 0.0;
131   }
132
133   /* (non-Javadoc)
134    * @see java.lang.Object#toString()
135    */
136   @Override
137   public String toString() {
138     StringBuilder sb = new StringBuilder(32);
139
140     if (maintainSingleValue) {
141       sb.append("[ Val=").append(value).append(" ]");
142     } else {
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(" ]");
147     }
148
149     return sb.toString();
150   }
151
152 }