Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / analytics / AveragingRingBuffer.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 import java.util.concurrent.atomic.AtomicInteger;
28
29 /**
30  * TODO: Fill in description.
31  * 
32  * @author davea
33  */
34 public class AveragingRingBuffer {
35
36   private int numElements;
37
38   private long[] data;
39
40   private AtomicInteger index;
41
42   private long average;
43
44   private boolean initialAverageCalculated;
45
46   /**
47    * Instantiates a new averaging ring buffer.
48    *
49    * @param size the size
50    */
51   public AveragingRingBuffer(int size) {
52
53     if (size == 0) {
54       throw new IllegalArgumentException("Size must be greater than zero");
55     }
56
57     this.initialAverageCalculated = false;
58     this.numElements = size;
59     this.data = new long[this.numElements];
60     this.index = new AtomicInteger(-1);
61   }
62
63   /**
64    * Calculate average.
65    *
66    * @param maxArrayIndex the max array index
67    */
68   private void calculateAverage(int maxArrayIndex) {
69
70     long sum = 0;
71
72     for (int i = 0; i <= maxArrayIndex; i++) {
73       sum += data[i];
74     }
75
76     average = (sum / (maxArrayIndex + 1));
77
78   }
79
80   public long getAvg() {
81
82     if (!initialAverageCalculated) {
83       /*
84        * until the index rolls once we will calculate the average from the data that has been added
85        * to the array, not including the zero elements
86        */
87       if (index.get() < 0) {
88         calculateAverage(0);
89       } else {
90         calculateAverage(index.get());
91       }
92
93     }
94
95     return average;
96   }
97
98   /**
99    * Adds the sample.
100    *
101    * @param value the value
102    */
103   public synchronized void addSample(long value) {
104
105     index.incrementAndGet();
106
107     data[index.get()] = value;
108
109     if (index.get() == (numElements - 1)) {
110       calculateAverage(numElements - 1);
111
112       if (!initialAverageCalculated) {
113         initialAverageCalculated = true;
114       }
115
116       index.set(-1);
117     }
118
119   }
120
121 }