This repository was archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReasonRankingAccumulator.java
More file actions
89 lines (74 loc) · 2.19 KB
/
ReasonRankingAccumulator.java
File metadata and controls
89 lines (74 loc) · 2.19 KB
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
package utility.accumulators;
import java.util.*;
/**
* Scope: Global - Query 2
* Accumulator used on time windows for delay reason ranking
*/
@SuppressWarnings("unused")
public class ReasonRankingAccumulator {
// ranking of morning hours as couples [reason - occurrences]
private final HashMap<String, Long> amRanking;
// ranking of afternoon hours as couples [reason - occurrences]
private final HashMap<String, Long> pmRanking;
/**
* No arguments constructor
*/
public ReasonRankingAccumulator() {
// initialize structures
this.amRanking = new HashMap<>();
this.pmRanking = new HashMap<>();
}
/**
* Adds new info to the current delay reason ranking
* @param date event date (with hours and minutes too)
* @param reason of the delay
*/
public void add(Date date, String reason) {
// threshold setup
Calendar threshold = Calendar.getInstance(Locale.US);
threshold.setTime(date);
// current event date setup
Calendar elem = Calendar.getInstance(Locale.US);
elem.setTime(date);
threshold.set(Calendar.MINUTE, 0);
threshold.set(Calendar.SECOND, 0);
threshold.set(Calendar.MILLISECOND, 0);
// check if before 05:00
threshold.set(Calendar.HOUR_OF_DAY, 5);
// out of both time intervals
if (elem.before(threshold)) {
return;
}
// check if after 19:00
threshold.set(Calendar.HOUR_OF_DAY, 19);
// out of both time intervals
if (elem.after(threshold)) {
return;
}
// set threshold at 12:00 of the same day
threshold.set(Calendar.HOUR_OF_DAY, 12);
//check if it falls in am or pm
if (elem.before(threshold)) {
// add to morning ranking
this.amRanking.merge(reason, 1L, Long::sum);
} else {
// add to afternoon ranking
this.pmRanking.merge(reason, 1L, Long::sum);
}
}
/**
* Merge ranking maps to the current ones
* @param am morning ranking map
* @param pm afternoon ranking map
*/
public void mergeRankings(HashMap<String, Long> am, HashMap<String, Long> pm) {
am.forEach((k, v) -> this.amRanking.merge(k, v, Long::sum));
pm.forEach((k, v) -> this.pmRanking.merge(k, v, Long::sum));
}
public HashMap<String, Long> getAmRanking() {
return amRanking;
}
public HashMap<String, Long> getPmRanking() {
return pmRanking;
}
}