Skip to content

Commit c233024

Browse files
committed
Merge branch 'master' of https://github.com/AY2021S2-CS2103-T16-2/tp into branch-notes
2 parents d081df7 + 498e171 commit c233024

2 files changed

Lines changed: 135 additions & 41 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package seedu.address.model.schedule;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.LocalTime;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* A class to manipulate schedulables, like shifting a schedulable by a certain offset,
10+
* or finding overlap between schedulable objects.
11+
*/
12+
public class SchedulableUtil {
13+
14+
/**
15+
* Splits a schedulable into multiple schedulables on different days. For example if a schedulable is scheduled
16+
* on friday 2pm to Sunday 2pm, it should split into schedulables
17+
* [fri 2pm- fri 11.59.99999pm, sat 12am - sat 11.59.999999 pm , sunday 12am - 2pm]
18+
* @param schedulable
19+
* @return
20+
*/
21+
22+
public static List<Schedulable> splitSchedulableByDay(Schedulable schedulable) {
23+
24+
LocalDateTime startDateTime = schedulable.getStartLocalDateTime();
25+
LocalDateTime endDateTime = schedulable.getTerminateLocalDateTime();
26+
27+
//same day interval.
28+
if (startDateTime.toLocalDate().isEqual(endDateTime.toLocalDate())) {
29+
return List.of(schedulable);
30+
}
31+
32+
ArrayList<Schedulable> listOfSchedulableUnits = new ArrayList<>();
33+
String name = schedulable.getNameString();
34+
35+
Schedulable firstPeriod = new SimplePeriod(name, startDateTime, getEndOfTheDay(startDateTime));
36+
listOfSchedulableUnits.add(firstPeriod);
37+
38+
//Check if case !endTime == 00:00
39+
if (!endDateTime.isEqual(getStartOfTheDay(endDateTime))) {
40+
Schedulable lastPeriod = new SimplePeriod(name, getStartOfTheDay(endDateTime), endDateTime);
41+
listOfSchedulableUnits.add(lastPeriod);
42+
}
43+
44+
startDateTime = getStartOfTheDay(startDateTime).plusDays(1);
45+
endDateTime = getStartOfTheDay(endDateTime);
46+
47+
//iterate through each day slot in between
48+
while(endDateTime.isAfter(startDateTime)) {
49+
Schedulable toAdd = new SimplePeriod(name, startDateTime, getEndOfTheDay(startDateTime));
50+
listOfSchedulableUnits.add(toAdd);
51+
startDateTime = getStartOfTheDay(startDateTime).plusDays(1);
52+
}
53+
54+
return listOfSchedulableUnits;
55+
}
56+
57+
58+
59+
/**
60+
* Applies a positive offset to a Schedulable object by adding a positive number of hours and minutes to its
61+
* start and end times.
62+
* @param schedulable
63+
* @param hourOffset
64+
* @param minuteOffset
65+
* @return
66+
*/
67+
68+
public static Schedulable applyPositiveOffset(Schedulable schedulable, int hourOffset, int minuteOffset) {
69+
70+
return new SimplePeriod(schedulable.getNameString(),
71+
applyPositiveOffset(schedulable.getStartLocalDateTime(), hourOffset, minuteOffset),
72+
applyPositiveOffset(schedulable.getTerminateLocalDateTime(), hourOffset, minuteOffset));
73+
}
74+
75+
/**
76+
* Applies a negative offset to a Schedulable object by subtracting a positive number of hours and minutes from its
77+
* start and end times.
78+
* @param schedulable
79+
* @param hourOffset
80+
* @param minuteOffset
81+
* @return
82+
*/
83+
84+
public static Schedulable applyNegativeOffset(Schedulable schedulable, int hourOffset, int minuteOffset) {
85+
return new SimplePeriod(
86+
schedulable.getNameString(),
87+
applyNegativeOffset(schedulable.getStartLocalDateTime(), hourOffset, minuteOffset),
88+
applyNegativeOffset(schedulable.getTerminateLocalDateTime(), hourOffset, minuteOffset));
89+
}
90+
91+
/**
92+
* Offsets a local date by a negative number of hours and minutes;
93+
* @return
94+
*/
95+
public static LocalDateTime applyNegativeOffset(LocalDateTime localDateTime, int hourOffset, int minuteOffset) {
96+
return localDateTime.minusHours(hourOffset).minusMinutes(minuteOffset);
97+
}
98+
99+
/**
100+
* Offsets a datetime by a positive number of hours and minutes.
101+
* in @code{applyOffset}
102+
*
103+
* @param offSetDateTime
104+
* @return
105+
*/
106+
public static LocalDateTime applyPositiveOffset(LocalDateTime offSetDateTime, int hourOffset, int minuteOffset) {
107+
return offSetDateTime.plusHours(hourOffset).plusMinutes(minuteOffset);
108+
}
109+
110+
111+
112+
/**
113+
* Gets the local date time of the end of the day, right before 00:00.
114+
* @return
115+
*/
116+
public static LocalDateTime getEndOfTheDay(LocalDateTime localDateTime) {
117+
return localDateTime.toLocalDate().atTime(LocalTime.MAX);
118+
}
119+
120+
/**
121+
* Gets the start of the day at 00:00.
122+
* @param localDateTime
123+
* @return
124+
*/
125+
public static LocalDateTime getStartOfTheDay(LocalDateTime localDateTime) {
126+
return localDateTime.toLocalDate().atTime(0,0);
127+
}
128+
129+
130+
}

src/main/java/seedu/address/ui/TimetablePlacementPolicy.java

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package seedu.address.ui;
22

33
import seedu.address.model.schedule.Schedulable;
4+
import seedu.address.model.schedule.SchedulableUtil;
45
import seedu.address.model.schedule.SimplePeriod;
56

67
import java.time.Duration;
@@ -148,48 +149,11 @@ public double getLengthOfSlot(Schedulable schedulable) {
148149
public Stream<Schedulable> breakIntoDayUnits(Schedulable schedulable) {
149150

150151
assert test(schedulable);
151-
LocalDateTime startDateTime = schedulable.getStartLocalDateTime();
152-
LocalDateTime endDateTime = schedulable.getTerminateLocalDateTime();
153-
154-
//apply offset
155-
LocalDateTime offSetStartTime = applyOffset(startDateTime);
156-
LocalDateTime offSetEndTime = applyOffset(endDateTime);
157-
158-
//same day interval.
159-
if (offSetStartTime.toLocalDate().isEqual( offSetEndTime.toLocalDate())) {
160-
return List.of(schedulable).stream();
161-
}
162-
//case when returns more than one element.
163-
ArrayList<Schedulable> listOfSchedulableUnits = new ArrayList<>();
164-
String name = schedulable.getNameString();
165-
166-
Schedulable firstPeriod = new SimplePeriod(name,
167-
removeOffset(offSetStartTime),
168-
removeOffset(getEndOfTheDay(offSetStartTime)));
169-
listOfSchedulableUnits.add(firstPeriod);
170-
171-
//Check if case !endTime == 00:00
172-
if (!offSetEndTime.isEqual(getStartOfTheDay(offSetEndTime))) {
173-
Schedulable lastPeriod = new SimplePeriod(name,
174-
removeOffset(getStartOfTheDay(offSetEndTime)),
175-
removeOffset(offSetEndTime));
176-
listOfSchedulableUnits.add(lastPeriod);
177-
}
178-
179-
offSetStartTime = getStartOfTheDay(offSetStartTime).plusDays(1);
180-
offSetEndTime = getStartOfTheDay(offSetEndTime);
181-
182-
//iterate through each day slot in between
183-
while(offSetEndTime.isAfter(offSetStartTime)) {
184-
Schedulable toAdd = new SimplePeriod(name,
185-
removeOffset(offSetStartTime),
186-
removeOffset(getEndOfTheDay(offSetStartTime)));
187-
listOfSchedulableUnits.add(toAdd);
188-
offSetStartTime = getStartOfTheDay(offSetStartTime).plusDays(1);
189-
}
190-
191-
return listOfSchedulableUnits
152+
Schedulable offSetSchedule = SchedulableUtil.applyNegativeOffset(schedulable, startHour, startMinute);
153+
List<Schedulable> splittedSchedulables = SchedulableUtil.splitSchedulableByDay(offSetSchedule);
154+
return splittedSchedulables
192155
.stream()
156+
.map(s -> SchedulableUtil.applyPositiveOffset(s, startHour, startMinute))
193157
.filter(this :: test);
194158

195159
}

0 commit comments

Comments
 (0)