-
Notifications
You must be signed in to change notification settings - Fork 0
/
NextReminderCalculator.java
138 lines (123 loc) · 2.93 KB
/
NextReminderCalculator.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
package bbtrial.nl.logicgate.ace;
/**
* NextReminderCalculator calculates when the next reminder can be sent to this doctor.
* @author skmedlock
*
*/
public class NextReminderCalculator {
private boolean never;
private int n;
private String unit;
private int m;
private RCalendar previous;
public NextReminderCalculator(){
never = false;
n = 0;
unit = "";
m = 0;
previous = null;
}
public RCalendar getNextReminderDate() {
if(never){
return null;
}
if(unit.equals("d") || unit.equals("w") || unit.equals("m")){
//add n units to the previous reminder date
if(previous==null){
return new RCalendar();
} else {
return new RCalendar(previous, unit, n);
}
}
//set the month
RCalendar next;
if(previous==null){
next = new RCalendar();
} else {
next = new RCalendar(previous, "m", m);
}
if(unit.equals("ofMo")){
//set the day
next.setDayOfMonth(n);
return next;
}
//unit is a day of the week
int days = (Math.abs(n)-1)*7;
if(n>0){
next.setDayOfMonth(1);
int dif = intDOW(unit)-next.getIntDayOfWeek();
if(dif<0){
days = days + (7 + dif); //if dif is neg, add days to make up for week 1
} else {
days = days + dif; //if dif is pos, add dif
}
next.add(0,0,0,days);
}
if(n<0){ //count from end of month
next.add(0,1,0,0); next.setDayOfMonth(-0); //set to last day of month
int dif = next.getIntDayOfWeek()-intDOW(unit);
if(dif<0){
days = days + (7 + dif); //if dif is neg, add days to make up for last week
} else {
days = days - dif; //if dif is pos, subtract dif
}
next.add(0,0,0,-days);
}
if(n==0){
next.setStringDayOfWeek(unit);
}
return next;
}
/**
* Integer values for string days of week
*/
private int intDOW(String strDOW){
if(strDOW.equalsIgnoreCase("sunday")){return 1;}
if(strDOW.equalsIgnoreCase("monday")){return 2;}
if(strDOW.equalsIgnoreCase("tuesday")){return 3;}
if(strDOW.equalsIgnoreCase("wednesday")){return 4;}
if(strDOW.equalsIgnoreCase("thursday")){return 5;}
if(strDOW.equalsIgnoreCase("friday")){return 6;}
if(strDOW.equalsIgnoreCase("saturday")){return 7;}
return 0;
}
/**
* @param never the never to set
*/
public void setNever(boolean never) {
this.never = never;
}
/**
* Returns TRUE if this doctor NEVER gets a reminder
*/
public boolean isNever(){
return never;
}
/**
* @param n the n to set
*/
public void setN(int n) {
this.n = n;
}
/**
* @param unit the unit to set
*/
public void setUnit(String unit) {
this.unit = unit;
}
/**
* @param m the m to set
*/
public void setM(int m) {
this.m = m;
}
/**
* @param previous the previous to set
*/
public void setPrevious(RCalendar previous) {
this.previous = previous;
}
public RCalendar getLastReminderDate() {
return previous;
}
}