forked from nus-cs2103-AY1920S1/addressbook-level3
/
EventDateTimeMap.java
218 lines (190 loc) · 6.8 KB
/
EventDateTimeMap.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
/*
@@author DivineDX
*/
package seedu.address.model.event;
import static seedu.address.logic.parser.ParserUtil.parseEventDate;
import static seedu.address.logic.parser.ParserUtil.parseTimePeriod;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
/**
* Represents a TreeMap of the EventDate to EventDayTime.
* Used to represent the schedule of an Event on all valid, mapped dates.
*/
public class EventDateTimeMap {
public static final String MESSAGE_CONSTRAINTS =
"EventDateTimeMap is of the format DDMMYYYY:HHMM-HHMM, delimited by commas";
private static final String DELIMITER = ",";
private final Map<EventDate, EventDayTime> dateTimeMap;
/**
* Default Constructor, initializes a new TreeMap.
*/
public EventDateTimeMap() {
dateTimeMap = new TreeMap<>();
}
public EventDateTimeMap(Map<EventDate, EventDayTime> map) {
this();
dateTimeMap.putAll(map);
}
/**
* Create a new, immutable copy of the EventDateTimeMap
*/
public EventDateTimeMap(EventDateTimeMap other) {
this(other.getDateTimeMap());
}
/**
* Used when constructing EventDateTimeMap object from JsonStorage.
*
* @param stringMap Stored String in JSON.
* @throws IllegalArgumentException Error in Parsing.
*/
public EventDateTimeMap(String stringMap) throws IllegalArgumentException {
this();
if (stringMap.isEmpty()) {
return;
}
try {
String[] eachDateTime = stringMap.split(DELIMITER); //delimiter
for (String dateTime : eachDateTime) {
String[] dateTimeSplit = dateTime.split(":"); //[0] is date, [1] is time-period
this.mapDateTime(parseEventDate(dateTimeSplit[0]), parseTimePeriod(dateTimeSplit[1]));
}
} catch (ArrayIndexOutOfBoundsException | ParseException e) {
throw new IllegalArgumentException(EventDateTimeMap.MESSAGE_CONSTRAINTS);
}
return;
}
/**
* When Event object is first created, auto-initialize DateTime mapping for Start&End Date.
*/
public void initalizeDateTime(EventDate startDate, EventDate endDate) {
EventDayTime defaultEventDayTime = EventDayTime.defaultEventDayTime();
mapDateTime(startDate, defaultEventDayTime);
mapDateTime(endDate, defaultEventDayTime);
}
/**
* Returns true if the given string represents a valid DateTime Mapping.
*
* @param test String to be parsed
* @return boolean to indicate if the parse is successful
*/
public static boolean isValidEventDateTimeMap(String test) {
if (test.isEmpty()) {
return true;
}
try {
return ParserUtil.parseEventDateTimeMap(test) instanceof EventDateTimeMap;
} catch (ParseException e) {
return false;
}
}
public Map<EventDate, EventDayTime> getDateTimeMap() {
return dateTimeMap;
}
/**
* Returns a Sorted List of EventDates that are currently mapped
*/
public List<EventDate> getDateMappedList() {
List<EventDate> uniqueEventDates = new ArrayList<>(dateTimeMap.keySet());
Collections.sort(uniqueEventDates);
return uniqueEventDates;
}
/**
* Add/Update the Key-Value pair of an EventDate to a EventDayTime.
*
* @param date EventDate object representing a Date of an Event
* @param dayTime EventDayTime object representing the time period on a date in the Event's Schedule.
*/
public void mapDateTime(EventDate date, EventDayTime dayTime) {
dateTimeMap.put(date, dayTime);
}
/**
* Clears the entire DateTime Mapping from an Event.
*/
public void clearMapping() {
dateTimeMap.clear();
}
/**
* Boolean that checks whether an EventDate is within
*
* @param date A valid {@code EventDate} object
*/
public boolean containsDateKey(EventDate date) {
return dateTimeMap.containsKey(date);
}
/**
* Calculates the Event's total number of hours based on the sum
* of the duration of the mapped {@EventDayTime}.
*/
public double totalHours() {
double totalMinutes = 0;
for (EventDate date : dateTimeMap.keySet()) {
totalMinutes += dateTimeMap.get(date).numMinutes();
}
return totalMinutes / 60.0;
}
/**
* Deletes a Date from the Date-Time Mapping.
*
* @param date EventDate Object
*/
public void deleteDateKey(EventDate date) {
dateTimeMap.remove(date);
}
/**
* Called when the Event's Start/End Date has changed. EventDateTime will flush {@code EventDate} keys
* from the Mapping which falls out of the new range.
* Then, the default DateTime values will be inserted for the start and end dates keys if not found
*/
public void flushEventDates(EventDate newStartDate, EventDate newEndDate) {
dateTimeMap.entrySet().removeIf(event -> {
EventDate eventDate = event.getKey();
return eventDate.isBefore(newStartDate) || eventDate.isAfter(newEndDate);
});
if (!dateTimeMap.containsKey(newStartDate)) {
mapDateTime(newStartDate, EventDayTime.defaultEventDayTime());
}
if (!dateTimeMap.containsKey(newEndDate)) {
mapDateTime(newEndDate, EventDayTime.defaultEventDayTime());
}
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof EventDateTimeMap // instanceof handles nulls
&& dateTimeMap.equals(((EventDateTimeMap) other).getDateTimeMap())); // state check
}
@Override
public int hashCode() {
return dateTimeMap.hashCode();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (EventDate keyEventDate : dateTimeMap.keySet()) {
if (sb.length() > 0) {
sb.append(DELIMITER);
}
sb.append(keyEventDate.toString()); //Event Date
sb.append(":");
sb.append(dateTimeMap.get(keyEventDate).toString());
}
return sb.toString();
}
/**
* A similar variation of toString() for GUI display.
*/
public String toStringWithNewLine() {
StringBuilder sb = new StringBuilder();
for (EventDate keyEventDate : dateTimeMap.keySet()) {
sb.append(keyEventDate.toString()); //Event Date
sb.append(" : ");
sb.append(dateTimeMap.get(keyEventDate).toString() + "\n");
}
return sb.toString().trim();
}
}