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