Coverage Summary for Class: PunishmentCalculator (co.rsk.scoring)
Class |
Class, %
|
Method, %
|
Line, %
|
PunishmentCalculator |
0%
(0/1)
|
0%
(0/2)
|
0%
(0/14)
|
1 package co.rsk.scoring;
2
3 import static java.lang.Math.multiplyExact;
4
5 /**
6 * PunishmentCalculator calculates the punishment duration
7 * given the punishment parameters (@see PunishmentParameters)
8 * <p>
9 * Created by ajlopez on 10/07/2017.
10 */
11 public class PunishmentCalculator {
12 private final PunishmentParameters parameters;
13
14 public PunishmentCalculator(PunishmentParameters parameters) {
15 this.parameters = parameters;
16 }
17
18 /**
19 * Calculate the punishment duration (in milliseconds)
20 * given the count of previous punishment and current peer score.
21 *
22 * The duration is incremented according the number of previous punishment
23 * using an initial duration and a percentage increment
24 *
25 * The duration cannot be greater than the maximum duration specified in parameters
26 * (0 = no maximum duration)
27 *
28 * @param punishmentCounter the count of previous punishment for a peer
29 * @param score the peer score
30 *
31 * @return the punishment duration in milliseconds
32 */
33 public long calculate(int punishmentCounter, int score) {
34 long result = this.parameters.getDuration();
35 long rate = 100L + this.parameters.getIncrementRate();
36 int counter = punishmentCounter;
37 long maxDuration = this.parameters.getMaximumDuration();
38
39 while (counter-- > 0) {
40 result = multiplyExact(result, rate) / 100;
41 if (maxDuration > 0 && result > maxDuration) {
42 return maxDuration;
43 }
44 }
45
46 if (score < 0) {
47 result *= -score;
48 }
49
50 return maxDuration > 0 ? Math.min(maxDuration, result) : result;
51 }
52 }