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