-
Notifications
You must be signed in to change notification settings - Fork 11
/
tabAverage.java
233 lines (214 loc) · 5.14 KB
/
tabAverage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package net.freertr.tab;
import java.util.ArrayList;
import java.util.List;
/**
* average calculator
*
* @author matecsaba
*/
public class tabAverage {
/**
* mode, 0=none, 1=min, 2=avg, 3=max, 4=diff
*/
public int algorithm = 1;
/**
* number of bucket
*/
public int buckets = 5;
/**
* echo minimum
*/
public int minimum;
/**
* echo minimum
*/
public int maximum;
/**
* echo divisor
*/
public int divisor = 1;
/**
* echo multiplier
*/
public int multiply = 1;
/**
* echo relaxer
*/
public int ignorer = 0;
private int lastReported;
private final List<Integer> pastValues;
/**
* create instance
*
* @param min minimum
* @param max maximum
*/
public tabAverage(int min, int max) {
pastValues = new ArrayList<Integer>();
minimum = min;
maximum = max;
lastReported = max;
}
public String toString() {
String a = "";
for (int i = 0; i < pastValues.size(); i++) {
a += " " + pastValues.get(i);
}
return a;
}
/**
* get algorithm name
*
* @return name
*/
public String getAlgoName() {
switch (algorithm) {
case 0:
return "none";
case 1:
return "minimum";
case 2:
return "average";
case 3:
return "maximum";
case 4:
return "differs";
default:
return "unknown=" + algorithm;
}
}
/**
* set algorithm name
*
* @param a name
*/
public void string2algo(String a) {
if (a.equals("none")) {
algorithm = 0;
return;
}
if (a.equals("minimum")) {
algorithm = 1;
return;
}
if (a.equals("average")) {
algorithm = 2;
return;
}
if (a.equals("maximum")) {
algorithm = 3;
return;
}
if (a.equals("differs")) {
algorithm = 4;
return;
}
algorithm = 0;
}
/**
* add value
*
* @param val value
*/
public void addValue(int val) {
synchronized (pastValues) {
pastValues.add(val);
for (; pastValues.size() > buckets;) {
pastValues.remove(0);
}
}
}
/**
* update parameters
*
* @param src source
*/
public void updateFrom(tabAverage src) {
algorithm = src.algorithm;
buckets = src.buckets;
minimum = src.minimum;
maximum = src.maximum;
divisor = src.divisor;
multiply = src.multiply;
ignorer = src.ignorer;
}
/**
* get result
*
* @param met default value
* @return result
*/
public int getResult(int met) {
if (pastValues.size() < 1) {
return met;
}
switch (algorithm) {
case 1:
met = Integer.MAX_VALUE;
synchronized (pastValues) {
for (int i = 0; i < pastValues.size(); i++) {
int cur = pastValues.get(i);
if (cur < met) {
met = cur;
}
}
}
break;
case 2:
met = 0;
synchronized (pastValues) {
for (int i = 0; i < pastValues.size(); i++) {
int cur = pastValues.get(i);
met += cur;
}
met /= pastValues.size();
}
break;
case 3:
met = Integer.MIN_VALUE;
synchronized (pastValues) {
for (int i = 0; i < pastValues.size(); i++) {
int cur = pastValues.get(i);
if (cur > met) {
met = cur;
}
}
}
break;
case 4:
synchronized (pastValues) {
met = 0;
int prv = pastValues.get(0);
for (int i = 1; i < pastValues.size(); i++) {
int cur = pastValues.get(i);
int dif = prv - cur;
prv = cur;
if (dif < 0) {
dif = -dif;
}
met += dif;
}
}
break;
default:
return met;
}
met *= multiply;
met /= divisor;
if (met < minimum) {
met = minimum;
}
if (met > maximum) {
met = maximum;
}
int i = met - lastReported;
if (i < 0) {
i = -i;
}
if (i < ignorer) {
return lastReported;
}
lastReported = met;
return met;
}
}