|
| 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 | +} |
0 commit comments