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