Skip to content

Commit

Permalink
[refactor] 요일 별 최적의 회의 시간 도출 알고리즘 함수화
Browse files Browse the repository at this point in the history
  • Loading branch information
KWY0218 committed Nov 8, 2023
1 parent fc7ddc7 commit 354024c
Showing 1 changed file with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.asap.server.service.vo.BestMeetingTimeVo;
import com.asap.server.service.vo.TimeBlockVo;
import com.asap.server.service.vo.TimeBlocksByDateVo;
import com.asap.server.service.vo.UserVo;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -16,29 +18,29 @@ public class FindBestMeetingTimeStrategyImpl implements FindBestMeetingTimeStrat
@Override
public List<BestMeetingTimeVo> find(final TimeBlocksByDateVo timeBlocksByDate, final int needTimeBlockCount, final int userCount) {
List<TimeBlockVo> sortedTimeBlocks = filterByUserCountAndSortByTime(timeBlocksByDate.getTimeBlocks(), userCount);
return findBestMeetingTime(sortedTimeBlocks, timeBlocksByDate.getDate(), needTimeBlockCount);
}

private List<BestMeetingTimeVo> findBestMeetingTime(final List<TimeBlockVo> timeBlocks, final LocalDate date, final int needTimeBlockCount) {
List<BestMeetingTimeVo> bestMeetingTimes = new ArrayList<>();
int endIndex = sortedTimeBlocks.size() - needTimeBlockCount + 1;

int endIndex = timeBlocks.size() - needTimeBlockCount + 1;
for (int timeBlockIdx = 0; timeBlockIdx < endIndex; timeBlockIdx++) {
if (!isBestMeetingTime(sortedTimeBlocks, timeBlockIdx, needTimeBlockCount)) continue;
int endIdx = timeBlockIdx + needTimeBlockCount;
if (!isBestMeetingTime(timeBlocks, timeBlockIdx, endIdx)) continue;

int sumWeight = sortedTimeBlocks
.subList(timeBlockIdx, timeBlockIdx + needTimeBlockCount)
.stream()
.map(TimeBlockVo::getWeight)
.reduce(0, Integer::sum);
int sumWeight = sumTimeBlocksWeight(timeBlocks, timeBlockIdx, endIdx);

TimeSlot startTime = sortedTimeBlocks.get(timeBlockIdx).getTimeSlot();
BestMeetingTimeVo bestMeetingTime = new BestMeetingTimeVo(
timeBlocksByDate.getDate(),
startTime,
TimeSlot.getTimeSlot(startTime.ordinal() + needTimeBlockCount),
sortedTimeBlocks.get(timeBlockIdx).getUsers(),
BestMeetingTimeVo bestMeetingTime = getBestMeetingTime(
timeBlocks,
date,
timeBlockIdx,
needTimeBlockCount,
sumWeight
);

bestMeetingTimes.add(bestMeetingTime);
}

return bestMeetingTimes;
}

Expand All @@ -49,16 +51,42 @@ private List<TimeBlockVo> filterByUserCountAndSortByTime(final List<TimeBlockVo>
.collect(Collectors.toList());
}

private boolean isBestMeetingTime(final List<TimeBlockVo> timeBlocks, final int timeBlockIdx, final int needTimeBlockCount) {
private boolean isBestMeetingTime(final List<TimeBlockVo> timeBlocks, final int timeBlockIdx, final int endIdx) {
boolean isBestMeetingTime = true;
TimeSlot nextTime = timeBlocks.get(timeBlockIdx).getTimeSlot();
for (int i = timeBlockIdx + 1; i < timeBlockIdx + needTimeBlockCount; i++) {
if (nextTime.ordinal() + 1 != timeBlocks.get(i).getTimeSlot().ordinal()) {
isBestMeetingTime = false;
break;
}
for (int i = timeBlockIdx + 1; i < endIdx; i++) {
if (nextTime.ordinal() + 1 != timeBlocks.get(i).getTimeSlot().ordinal()) return false;

nextTime = timeBlocks.get(i).getTimeSlot();
}
return isBestMeetingTime;
}

private int sumTimeBlocksWeight(final List<TimeBlockVo> timeBlocks, final int startIdx, final int endIdx) {
return timeBlocks
.subList(startIdx, endIdx)
.stream()
.map(TimeBlockVo::getWeight)
.reduce(0, Integer::sum);
}

private BestMeetingTimeVo getBestMeetingTime(
final List<TimeBlockVo> timeBlocks,
final LocalDate date,
final int timeBlockIdx,
final int needTimeBlockCount,
final int sumWeight
) {
TimeSlot startTime = timeBlocks.get(timeBlockIdx).getTimeSlot();
TimeSlot endTime = TimeSlot.getTimeSlot(startTime.ordinal() + needTimeBlockCount);
List<UserVo> users = timeBlocks.get(timeBlockIdx).getUsers();

return new BestMeetingTimeVo(
date,
startTime,
endTime,
users,
sumWeight
);
}
}

0 comments on commit 354024c

Please sign in to comment.