Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / analytics / HistoricalCounter.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.analytics;
26
27 /**
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).
30  * 
31  * @author davea
32  */
33 public class HistoricalCounter {
34
35   private double min;
36
37   private double max;
38
39   private double totalOfSamples;
40
41   private long numSamples;
42
43   private double value;
44
45   private boolean maintainSingleValue;
46
47   /**
48    * Instantiates a new historical counter.
49    *
50    * @param trackSingleValue the track single value
51    */
52   public HistoricalCounter(boolean trackSingleValue) {
53     min = -1;
54     max = 0;
55     totalOfSamples = 0;
56     value = 0.0;
57     numSamples = 0;
58     this.maintainSingleValue = trackSingleValue;
59   }
60
61   public boolean isSingleValue() {
62     return maintainSingleValue;
63   }
64
65   /**
66    * Update.
67    *
68    * @param value the value
69    */
70   public synchronized void update(double value) {
71
72     if (value < 0) {
73       return;
74     }
75
76     if (maintainSingleValue) {
77
78       this.value = value;
79
80     } else {
81
82       if (min == -1) {
83         min = value;
84       }
85
86       if (value < min) {
87         min = value;
88       }
89
90       if (value > max) {
91         max = value;
92       }
93
94       totalOfSamples += value;
95       numSamples++;
96     }
97   }
98
99   public double getValue() {
100     return value;
101   }
102
103   public double getMin() {
104     return min;
105   }
106
107   public double getMax() {
108     return max;
109   }
110
111   public long getNumSamples() {
112     return numSamples;
113   }
114
115   public double getAvg() {
116     if (numSamples == 0) {
117       return 0;
118     }
119
120     return (totalOfSamples / numSamples);
121   }
122   public void setMin(double min) {
123                 this.min = min;
124         }
125   
126   public void setMax(double max) {
127                 this.max = max;
128         }
129   
130   public double getTotalOfSamples() {
131                 return totalOfSamples;
132         }
133
134         public void setTotalOfSamples(double totalOfSamples) {
135                 this.totalOfSamples = totalOfSamples;
136         }
137
138         public void setNumSamples(long numSamples) {
139                 this.numSamples = numSamples;
140         }
141         
142         public void setMaintainSingleValue(boolean maintainSingleValue) {
143                 this.maintainSingleValue = maintainSingleValue;
144         }
145
146
147   /**
148    * Reset.
149    */
150   public synchronized void reset() {
151     min = -1;
152     max = 0;
153     numSamples = 0;
154     totalOfSamples = 0;
155     value = 0.0;
156   }
157
158   /* (non-Javadoc)
159    * @see java.lang.Object#toString()
160    */
161   @Override
162   public String toString() {
163     StringBuilder sb = new StringBuilder(32);
164
165     if (maintainSingleValue) {
166       sb.append("[ Val=").append(value).append(" ]");
167     } else {
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(" ]");
172     }
173
174     return sb.toString();
175   }
176
177 }