Cleanup sparky-be pom
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / analytics / HistoricalCounter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.sparky.analytics;
22
23 /**
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).
26  * 
27  * @author davea
28  */
29 public class HistoricalCounter {
30
31   private double min;
32
33   private double max;
34
35   private double totalOfSamples;
36
37   private long numSamples;
38
39   private double value;
40
41   private boolean maintainSingleValue;
42
43   /**
44    * Instantiates a new historical counter.
45    *
46    * @param trackSingleValue the track single value
47    */
48   public HistoricalCounter(boolean trackSingleValue) {
49     min = -1;
50     max = 0;
51     totalOfSamples = 0;
52     value = 0.0;
53     numSamples = 0;
54     this.maintainSingleValue = trackSingleValue;
55   }
56
57   public boolean isSingleValue() {
58     return maintainSingleValue;
59   }
60
61   /**
62    * Update.
63    *
64    * @param value the value
65    */
66   public synchronized void update(double value) {
67
68     if (value < 0) {
69       return;
70     }
71
72     if (maintainSingleValue) {
73
74       this.value = value;
75
76     } else {
77
78       if (Double.valueOf(min).equals(-1.0)) {
79         min = value;
80       }
81
82       if (value < min) {
83         min = value;
84       }
85
86       if (value > max) {
87         max = value;
88       }
89
90       totalOfSamples += value;
91       numSamples++;
92     }
93   }
94
95   public double getValue() {
96     return value;
97   }
98
99   public double getMin() {
100     return min;
101   }
102
103   public double getMax() {
104     return max;
105   }
106
107   public long getNumSamples() {
108     return numSamples;
109   }
110
111   public double getAvg() {
112     if (numSamples == 0) {
113       return 0;
114     }
115
116     return (totalOfSamples / numSamples);
117   }
118   public void setMin(double min) {
119                 this.min = min;
120         }
121   
122   public void setMax(double max) {
123                 this.max = max;
124         }
125   
126   public double getTotalOfSamples() {
127                 return totalOfSamples;
128         }
129
130         public void setTotalOfSamples(double totalOfSamples) {
131                 this.totalOfSamples = totalOfSamples;
132         }
133
134         public void setNumSamples(long numSamples) {
135                 this.numSamples = numSamples;
136         }
137         
138         public void setMaintainSingleValue(boolean maintainSingleValue) {
139                 this.maintainSingleValue = maintainSingleValue;
140         }
141
142
143   /**
144    * Reset.
145    */
146   public synchronized void reset() {
147     min = -1;
148     max = 0;
149     numSamples = 0;
150     totalOfSamples = 0;
151     value = 0.0;
152   }
153
154   /* (non-Javadoc)
155    * @see java.lang.Object#toString()
156    */
157   @Override
158   public String toString() {
159     StringBuilder sb = new StringBuilder(32);
160
161     if (maintainSingleValue) {
162       sb.append("[ Val=").append(value).append(" ]");
163     } else {
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(" ]");
168     }
169
170     return sb.toString();
171   }
172
173 }