refactoring : long function - preserve whole object
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.example.refactoring._03_long_function._09_preserve_whole_object;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public record Participant(String username, Map<Integer, Boolean> homework) {
|
||||
public Participant(String username) {
|
||||
this(username, new HashMap<>());
|
||||
}
|
||||
|
||||
public void setHomeworkDone(int index) {
|
||||
this.homework.put(index, true);
|
||||
}
|
||||
|
||||
double getRate(int totalNumberOfEvents) {
|
||||
long count = homework().values().stream()
|
||||
.filter(v -> v == true)
|
||||
.count();
|
||||
return (double) (count * 100 / totalNumberOfEvents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.example.refactoring._03_long_function._09_preserve_whole_object;
|
||||
|
||||
import org.kohsuke.github.GHIssue;
|
||||
import org.kohsuke.github.GHIssueComment;
|
||||
import org.kohsuke.github.GHRepository;
|
||||
import org.kohsuke.github.GitHub;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class StudyDashboard {
|
||||
|
||||
private final int totalNumberOfEvents;
|
||||
|
||||
public StudyDashboard(int totalNumberOfEvents) {
|
||||
this.totalNumberOfEvents = totalNumberOfEvents;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
StudyDashboard studyDashboard = new StudyDashboard(15);
|
||||
studyDashboard.print();
|
||||
}
|
||||
|
||||
private void print() throws IOException, InterruptedException {
|
||||
GitHub gitHub = GitHub.connect();
|
||||
GHRepository repository = gitHub.getRepository("whiteship/live-study");
|
||||
List<Participant> participants = new CopyOnWriteArrayList<>();
|
||||
|
||||
ExecutorService service = Executors.newFixedThreadPool(8);
|
||||
CountDownLatch latch = new CountDownLatch(totalNumberOfEvents);
|
||||
|
||||
for (int index = 1 ; index <= totalNumberOfEvents ; index++) {
|
||||
int eventId = index;
|
||||
service.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
GHIssue issue = repository.getIssue(eventId);
|
||||
List<GHIssueComment> comments = issue.getComments();
|
||||
|
||||
for (GHIssueComment comment : comments) {
|
||||
String username = comment.getUserName();
|
||||
boolean isNewUser = participants.stream().noneMatch(p -> p.username().equals(username));
|
||||
Participant participant = null;
|
||||
if (isNewUser) {
|
||||
participant = new Participant(username);
|
||||
participants.add(participant);
|
||||
} else {
|
||||
participant = participants.stream().filter(p -> p.username().equals(username)).findFirst().orElseThrow();
|
||||
}
|
||||
|
||||
participant.setHomeworkDone(eventId);
|
||||
}
|
||||
|
||||
latch.countDown();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
latch.await();
|
||||
service.shutdown();
|
||||
|
||||
try (FileWriter fileWriter = new FileWriter("participants.md");
|
||||
PrintWriter writer = new PrintWriter(fileWriter)) {
|
||||
participants.sort(Comparator.comparing(Participant::username));
|
||||
|
||||
writer.print(header(participants.size()));
|
||||
|
||||
participants.forEach(p -> {
|
||||
|
||||
// 파라미터 각각이 아닌 전체 객체를 전달, 메소드의 의존성을 잘 고려하여 리팩토링 해야한다.
|
||||
String markdownForHomework = getMarkdownForParticipant(p);
|
||||
|
||||
writer.print(markdownForHomework);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private String getMarkdownForParticipant(Participant participant) {
|
||||
return String.format("| %s %s | %.2f%% |\n", participant.username(),
|
||||
checkMark(participant, this.totalNumberOfEvents),
|
||||
participant.getRate(this.totalNumberOfEvents));
|
||||
}
|
||||
|
||||
/**
|
||||
* | 참여자 (420) | 1주차 | 2주차 | 3주차 | 참석율 |
|
||||
* | --- | --- | --- | --- | --- |
|
||||
*/
|
||||
private String header(int totalNumberOfParticipants) {
|
||||
StringBuilder header = new StringBuilder(String.format("| 참여자 (%d) |", totalNumberOfParticipants));
|
||||
|
||||
for (int index = 1; index <= this.totalNumberOfEvents; index++) {
|
||||
header.append(String.format(" %d주차 |", index));
|
||||
}
|
||||
header.append(" 참석율 |\n");
|
||||
|
||||
header.append("| --- ".repeat(Math.max(0, this.totalNumberOfEvents + 2)));
|
||||
header.append("|\n");
|
||||
|
||||
return header.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* |:white_check_mark:|:white_check_mark:|:white_check_mark:|:x:|
|
||||
*/
|
||||
private String checkMark(Participant participant, int totalEvents) {
|
||||
StringBuilder line = new StringBuilder();
|
||||
for (int i = 1 ; i <= totalEvents ; i++) {
|
||||
if(participant.homework().containsKey(i) && participant.homework().get(i)) {
|
||||
line.append("|:white_check_mark:");
|
||||
} else {
|
||||
line.append("|:x:");
|
||||
}
|
||||
}
|
||||
return line.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user